Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Calling Out to a Web Service

From within a stateless session bean, you can obtain a web service and invoke its methods.

You must use the initial context (see "Using Initial Context") and you must create an environment reference for the web service (see "Configuring an Environment Reference to a Web Service") before you can look it up.

## Using Initial Context

After you define an environment reference to a web service (see "Configuring an Environment Reference to a Web Service"), you can use the initial context to look up the web service and invoke its methods from within your stateless session bean as Example 30-1 shows.

Example 30-1 Calling Out to a Web Service Obtained from the Initial Context

    @Stateless public class InvestmentBean implements Investment
    {
        public void checkPortfolio(...)
        {
            ...
            // Obtain the default initial JNDI context.
            Context initCtx = new InitialContext();
            // Look up the stock quote service in the environment.
            com.example.StockQuoteService sqs = (com.example.StockQuoteService)initCtx.lookup(
                "java:comp/env/service/StockQuoteService"
            );
            // Get the stub for the service endpoint
            com.example.StockQuoteProvider sqp = sqs.getStockQuoteProviderPort();
            // Get a quote
            float quotePrice = sqp.getLastTradePrice(...);
            ...
        }
    }