Using Web-services references

EJB components of any type can declare Web service references to alias a Web service proxy to a JNDI name.

In the EJB-JAR file, declare Web service references using the service-ref element as shown in the following example:

<service-ref>
  <description>
  This is a reference to the stock quote
  service used to estimate portfolio value.
  </description>
  <service-ref-name>service/StockQuoteService</service-ref-name>
  <service-interface>com.example.StockQuoteService
  </service-interface>
</service-ref>

In the corresponding implementation code, the Web service proxy is bound to the name java:comp/env/service/StockQuoteService and can be retrieved as shown in the Java code below:

// 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(...);

After deploying an EJB-JAR that contains Web service references, you must configure the references to specify a local implementation of the Web service proxy class and other details required to connect to the Web service’s host server.