O'Reilly logo

Java Web Services: Up and Running by Martin Kalin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A @WebService As an EJB

The EJB container is programmer-friendly in handling issues such as thread safety, instance pooling, and transaction-guarded persistence. This section illustrates how a @WebService can take advantage of these benefits by being implemented as a stateless Session EJB. The example is in two steps. The first step quickly covers the details of deploying a @WebService as a stateless Session EJB. The second step adds database persistence by introducing an @Entity into the application. The example is kept simple so that the EJB and @Entity details stand out.

Implementation As a Stateless Session EJB

Here is the SEI for the FibEJB service to be implemented as a stateless Session EJB:

package ch06.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.jws.WebMethod;

@Stateless
@WebService
public interface Fib {
    @WebMethod int fib(int n);
    @WebMethod List getFibs();
}

The main change is that the annotation @Stateless occurs. The same annotation occurs in the SIB:

package ch06.ejb;

import java.util.List;
import java.util.ArrayList;
import javax.ejb.Stateless;
import javax.jws.WebService;

@Stateless
@WebService(endpointInterface = "ch06.ejb.Fib")
public class FibEJB implements Fib {
    public int fib(int n) {
        int fib = 1, prev = 0;
        for (int i = 2; i <= n; i++) {
            int temp = fib;
            fib += prev;
            prev = temp;
        }
        return fib;
    }
    public List getFibs() { return new ArrayList(); } // for now, empty list
}

In the spirit of a stateless Session EJB, the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required