Recently, working on a customer project, needs to call a Custom EJB by a common java class. Both (java class and ejb) are deploied on weblogic into a jar shared library that represents the ejb module to include into an application.
Referencing the jar shared library into an EAR (adding reference on application-weblogic.xml file), the EJB declared into the shared library is referenceable by application servlet, or ejb and goes on... but the java class is not able to reference the ejb into the same module (jar shared library).
The java class, is in the same ejb jar but does not reference the stateless bean using @EJB annotation, apparently the java class is out of the ejb context.
To resolve is necessary lookup the EJB from the Java class as remote.
For remote lookup on WebLogic you should use this string:
<EJB-mappedName>#<bean_remote_interface_package>.<bean_remote_interface_name>
Follows an example of the shared library:
Here the EJB into the module (shared library)
@Remote public interface LibrarySessionEJB { public String test(); } ------------ @Stateless(name = "LibrarySessionEJB", mappedName = "MyLibApplication-Model-LibrarySessionEJB" ) public class LibrarySessionEJBBean implements LibrarySessionEJB, LibrarySessionEJBLocal { @Resource SessionContext sessionContext; public LibrarySessionEJBBean() { } public String test(){ return "I'm LibrarySessionEJBBean test method"; } }
public class TestClass { /* //it does not work @EJB LibrarySessionEJBLocal librarySessionEJB; */ public TestClass() { super(); } public String test(){ Context ctx=null; String var =""; try { ctx = new InitialContext(); LibrarySessionEJB ejb = (LibrarySessionEJB)ctx.lookup("MyLibApplication-Model-LibrarySessionEJB#example.ejb.LibrarySessionEJB"); var=ejb.test(); System.out.println(var); } catch (NamingException e) { e.printStackTrace(); var=e.getMessage(); } return "TestClass call: "+var; } }
No comments:
Post a Comment