Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB @Local and @Stateless together

I got my work rejected at uni because of using @Local and @Stateless on one EJB. It is a helper bean to validate/fix passed objects. I think it is completely legal to annotate my ejb's with both local and stateless. Can someone explain to me why can this be an issue?

like image 817
Tomas Avatar asked Jan 26 '15 13:01

Tomas


2 Answers

From javax.ejb.Local javadoc:

When used on the bean class, declares the local business interface(s) for a session bean. ... Use of the Local annotation is only required when the bean class does not implement only a single interface other than any of the following: java.io.Serializable; java.io.Externalizable; any of the interfaces defined in javax.ejb.

So when you use this annotation on the bean class, you need to pass local interface' class as parameter to this annotation. If your bean exposes a no-interface view you should annotate it with @LocalBean. From javax.ejb.LocalBean javadoc:

Designates that a session bean exposes a no-interface view. This annotation is required if a session bean exposes any other client views (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, Web Service) in addition to the no-interface view or its implements clause contains an interface other than java.io.Serializable; java.io.Externalizable; or any of the interfaces defined by the javax.ejb package.

So, if your bean doesn't implement any interfaces, you may just annotate it with @Stateless:

@Stateless
public class MyEJB {
    public void localMethod() {}
}
like image 52
Ivan Nikolaev Avatar answered Oct 23 '22 21:10

Ivan Nikolaev


If your EJB methods are made to be used by a local client (i.e. if ejb client is in same environment where ejb session bean is to be deployed. ) you have to extract an interface annotated by @Local in order to expose your business methods.

Example :

@Stateless
public class MyEJB implements MyEJBLocal{
    public void myBusinessMethod(){
        //Implementation
    }
}

@Local
public interface MyEJBLocal{
    public void myBusinessMethod();
}
like image 29
Naili Avatar answered Oct 23 '22 22:10

Naili