Sunday, August 20, 2017

Implement an Enterprise Session Bean

This post complies JEE 7, and test in weblogic 12c2.

How does container know a class is Session Bean?

The container regards a class as enterprise session bean if this class is annotated with either @Stateless, @Stateful or @Singleton annotation and no matter it's annotated with @Local, @Remote or @LocalBean annotation or it implements any interface which is annotated with @Local or @Remote annotation.
Now we know the key to make class an EJB is to annotate it with @Stateless, @Stateful or @Singleton regardless it has business interface or not.
There 2 styles of session bean, one is having business interface, another one is no-interface.
Clients access enterprise beans either through a no-interface view or through a business interface. A no-interface view of an enterprise bean exposes the public methods of the enterprise bean implementation class to clients.
Session beans can have more than one business interface. Session beans should, but are
not required to, implement their business interface or interfaces.

The ways to create EJB

We always take Local Stateless EJB as examples in below sections. 

1. Implement an interface which is annotated with @Local or @Remote

public interface Hello {
    void say(String msg);
}

/* Most of time in practice we also create a RemoteHello interface and implementation class implements these 2 interfaces meanwhile to let this bean have remote and local bean characters at the same time.*/
@Local
public interface LocalHello extends Hello{
    
}

/* EJB */
@Stateless(name="ejb/LocalHelloBean")
public class LocalHelloBean implements LocalHello{

    @Override
    public void say(String msg) {
        ...
    }
    
}
LocalHelloBean is a local ejb and has business interface LocalHello .

2. Annotate class with @Local or @Remote

public interface Hello {
    void say(String msg);
}

/* EJB */
@Local(com.nwb.jpractice.jee7.helloworld.ejb.api.Hello.class)
@Stateless(name="ejb/HelloBean")
public class HelloBean implements Hello{

    @Override
    public void say(String msg) {
        ...
    }
 
}
HelloBean is local ejb and has business interface Hello.

3. Annotate class with @LocalBean

No interface local view EJB
@LocalBean
@Stateless(name="ejb/LocalBeanAnnotatedHelloBean")
public class LocalBeanAnnotatedHelloBean {
 
    public void say(String msg) {
        ...
    }
 
}
LocalBeanAnnotatedHelloBean is a local ejb and has no-interface view.

4. No @Local, @Remote or @LocalBean annotation and implement an interface without @Local or @Remote annotation

This is the most magical case.
public interface Hello {
    void say(String msg);
}

@Stateless(name="ejb/NoLocalAnnotatedHelloBean")
public class NoLocalAnnotatedHelloBean implements Hello{
 
    public void say(String msg) {
        ...
    }
 
}
Interface Hello is not annotated with Local or Remove but container will automatically regard it as business interface. So NoLocalAnnotatedHelloBean is a local ejb and business interface Hello.

5. No @Local, @Remote or @LocalBean annotation and No implement any interface

@Stateless(name="ejb/NoLocalAnnotationNoInterfaceHelloBean")
public class NoLocalAnnotationNoInterfaceHelloBean {
 
    public void say(String msg) {
        ...
    }
 
}

NoLocalAnnotationNoInterfaceHelloBean is local ejb and has no-interface view. This case is completely as same as @LocalBean case at section 3.

Now we have been able to create our own session bean. After we have some beans how do we get the reference of the ejb?

No comments:

Post a Comment