Sometimes you may have a need to invoke a LOCAL EJB session beans in a normal java class, for example, Business Delegate class, you can use ServiceLocator to locate a local EJB session bean proxy by JNDI name. Even though it is relatively easy to do so for a REMOTE EJB session bean by using the value of 'name' or 'mappedName' in the bean class definition, it is a little tricky for LOCAL session beans.
Here is what you need to do.
For exampe:
Here is an interface:
package com.play;
@Local
public interface PlayFacadeInf {
public void play(String var);
}
Here is the implementation bean class.
package com.play;
@Stateless
public class PlayFacadeImpl implements PlayFacadeInf {
public void play(String var) {
//...do somthing
}
}
Here is the part of the ejb-jar.xml
display-name>myEJB </display-name>
<enterprise-beans>
<session>
<ejb-name> PlayFacadeImpl</ejb-name>
<ejb-class>com.play.PlayFacadeImpl</ejb-class>
<ejb-local-ref>
<ejb-ref-name>ejb/PlayFacadeInf</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.play.PlayFacadeInf</local>
</ejb-local-ref>
</session>
</enterprise-beans>
Here is part of web.xml
<ejb-local-ref>
<ejb-ref-name>ejb/PlayFacadeInf</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.play.PlayFacadeInf</local>
</ejb-local-ref>
Here is part of the ServiceLocator.java
private static InitialContext ctx = null;
static {
try {
ctx = new InitialContext();
}
catch (NamingException e) {
//... throw some exception
}
}
private static InitialContext getInitialContext() throws NamingException{
return ctx;
}
public static PlayFacadeInf getPlayFacade() throws NamingException {
PlayFacadeInf playFacadeInf = null;
playFacadeInf = ( PlayFacadeInf )
ServiceLocator.getInitialContext().lookup("java:/comp/env/ejb/PlayFacadeInf");
return playFacadeInf;
}
Then any normal java class can use the ServiceLocator to get hold of the local ejb session bean proxy.
I am using weblogic 10.3.5 and I don't have web.xml file. Where should I add the web.xml part.
ReplyDeleteThanks a lot!
ReplyDelete