Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3)
B14428-02
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
 
Next
Next
 

30 Using EJBs and Web Services

This section describes:

For more information, see the Oracle Application Server Web Services Developer's Guide.

Exposing a Stateless Session Bean as a Web Service

The client of a stateless session bean may be a Web service client. Only a stateless session bean may provide a Web service client view. A Web service client makes use of the enterprise bean's Web service client view, as described by a WSDL document. The bean's client view Web service endpoint interface is a JAX-RPC interface.

Using EJB 3.0, you can use annotations to easily expose a stateless session bean as a Web service (see "Using Annotations").

Using EJB 2.1, you can also expose a stateless session bean as a Web service (see "Assembling a Web Service with EJBs" in the Oracle Application Server Web Services Developer's Guide).

Using Annotations

Using the @WebService and @WebMethod annotations, you can define a Web service endpoint interface as Example 30-1 shows and implement the Web service as a stateless session bean as Example 30-2 shows.

Example 30-1 Annotated Web Service Endpoint Interface

package oracle.ejb30.ws;
 
import javax.ejb.Remote;
import javax.jws.WebService;
import javax.jws.WebMethod; 
 
@WebService
/**
* This is an Enterprise Java Bean Service Endpoint Interface
*/
public interface HelloServiceInf extends java.rmi.Remote
{
    /**
    * @param phrase java.lang.String
    * @return java.lang.String
    * @throws String The exception description.
    */
    @WebMethod
    java.lang.String sayHello(java.lang.String name) throws java.rmi.RemoteException;
}

Example 30-2 Implementing the Web Service as a Stateless Session Bean

package oracle.ejb30.ws;
 
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.Stateless;
 
/**
* This is a Session Bean Class.
*/
@Stateless(name="HelloServiceEJB")
public class HelloServiceBean implements HelloServiceInf
{
    public String sayHello(String name)
    {
        return("Hello "+name +" from first EJB3.0 Web Service");
    }
}

OC4J supports J2SE 5.0 Web Service annotations (also known as the Web Services Metadata for the Java Platform JSR-181) specification). The specification defines an annotated Java syntax for programming Web services.

For more information on using Web service annotations including Oracle extensions, see "Assembling Web Services with Annotations" in the Oracle Application Server Web Services Developer's Guide.

For other EJB Web service examples see the stateless session EJB Web service how-to or Adventure Builder how-to at http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3.

Accessing a Web Service from an EJB

From within an EJB, you can obtain a Web service and invoke its methods.

Using EJB 3.0, you can use annotations and resource injection (see "Using Annotations") without having to create an environment reference for the Web service.

Using EJB 2.1, 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.

For more information, see "Assembling a J2EE Web Service Client " in the Oracle Application Server Web Services Developer's Guide.

Using Annotations

Given the Web service that Example 30-3 shows, you can access the Web service from an EJB 3.0 stateless session bean using resource injection as Example 30-4 shows.

Example 30-3 Annotating a Web Service

import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService
public class StockQuoteProvider
{
    @WebMethod
    public Float getLastTradePrice()
    {
        ...
    }
}

Example 30-4 Calling Out to a Web Service Obtained by Resource Injection

@Stateless public class InvestmentBean implements Investment
{
    public void checkPortfolio(...)
    {
        ...
        @Resource StockQuoteProvider sqp;
        // Get a quote
        Float quotePrice = sqp.getLastTradePrice(...);
        ...
    }
}

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-5 shows.

Example 30-5 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(...);
        ...
    }
}