Access EJB:s from an other app
Hello Mates!I guess that I suck at EJB developing and should need some help.
I got 2 web apps at a orion server, main.ear and admin.ear. What I would like to do is to use some EJB:s that could performe tasks for booth of the two webb apps. So my attempt is to develop a ejb-module that is standalone on the orion server and the idea is that it will work as a shared resource for the apps main.ear and admin.ear.
Booth of the web-apps works fine but I can't get access to the ejb-module from them.
In the webapps a got a Servlet (just for testing right now) that tries to access a EJB.class in the web module but I only get this exception:
Communication error: MyCacheManagerHandler not found in admin, there are no bound values
javax.naming.NameNotFoundException: MyCacheManagerHandler not found in admin, there are no bound values
at com.evermind._jj.lookup(.:78)
at com.evermind._bq._ex(.:121)
at com.evermind._bq.lookup(.:63)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at admin.servlets.CacheManagerHandlerServlet.start(CacheManager
HandlerServlet.java:23)
at admin.servlets.GeneralServlet.doGet(GeneralServlet.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:195)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:309)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
at com.evermind._efb._plc(.:518)
at com.evermind._efb._adc(.:174)
at com.evermind._cs._yqb(.:614)
at com.evermind._cs._yrb(.:189)
at com.evermind._bx.run(.:62)
The Servlet looks like:
package admin.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import tools.*;
import my_ejb.ejb.*;
public class CacheManagerHandlerServlet extends GeneralServlet
{
//Called bu doPost()
public void start(HttpServletRequest request, HttpServletResponse response) throws IOException
{
try
{
//Create access to the naming context.
Context context = new InitialContext();
Object object = context.lookup("java:comp/env/MyCacheManagerHandler");
//Object object = context.lookup("ormi:localhost/my_ejb/MyCacheManagerHandler");
CacheManagerHandlerHome cacheManagerHandlerHome = (CacheManagerHandlerHome)PortableRemoteObject.narrow(object, CacheManagerHandlerHome.class);
CacheManagerHandler cacheManagerHandler = (CacheManagerHandler)PortableRemoteObject.narrow(cacheManagerHandlerHome.create(), CacheManagerHandler.class);
cacheManagerHandler.list();
}
catch(RemoteException e)
{
System.err.println("System/communication error: " + e.getMessage());
e.printStackTrace();
}
catch(NamingException e)
{
System.err.println("Communication error: " + e.getMessage());
e.printStackTrace();
}
catch(CreateException e)
{
System.err.println("Error creating cart: " + e.getMessage());
e.printStackTrace();
}
}
}
My 3 EJB classes that I guess is correct looks like (they is packed into a jar and seems to get deployed by the server):
package my_ejb.ejb;
import java.util.*;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface CacheManagerHandler extends EJBObject
{
public void list() throws RemoteException;
public void clear() throws RemoteException;
}
package my_ejb.ejb;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface CacheManagerHandlerHome extends EJBHome
{
public CacheManagerHandler create() throws CreateException, RemoteException;
}
package my_ejb.ejb;
import java.util.*;
import javax.ejb.*;
import java.rmi.RemoteException;
public class CacheManagerHandlerEJB implements SessionBean
{
public void ejbCreate(){}
public void list()
{
System.out.println("EJB LIST");
}
public void clear()
{
System.out.println("EJB CLEAR");
}
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove(){}
public void setSessionContext(SessionContext context){}
}
...Not so much code as you see. Just for testing right now.
To deploy this I also got a ejb-jar.xml:
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.2//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_2.dtd">
<ejb-jar>
<display-name>My EJB</display-name>
<description>My ejb module</description>
<enterprise-beans>
<session>
<display-name>MyCacheManagerHandler</display-name>
<description>MyCacheManagerHandler</description>
<ejb-name>MyCacheManagerHandler</ejb-name>
<home>my_ejb.ejb.CacheManagerHandlerHome</home>
<remote>my_ejb.ejb.CacheManagerHandler</remote>
<ejb-class>my_ejb.ejb.CacheManagerHandlerEJB</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
</assembly-descriptor>
</ejb-jar>
The orion server also creates a xml-file for me when I deploy this orion-ejb-jar.xml:
<?xml version="1.0"?>
<!DOCTYPE orion-ejb-jar PUBLIC "-//Evermind//DTD Enterprise JavaBeans 1.1 runtime//EN" "http://www.orionserver.com/dtds/orion-ejb-jar.dtd">
<orion-ejb-jar deployment-version="1.6.0" deployment-time="ff025f6e89">
<enterprise-beans>
<session-deployment name="MyCacheManagerHandler" location="MyCacheManagerHandler" wrapper="CacheManagerHandlerHome_StatefulSessionHomeWrapper1" persistence-filename="MyCacheManagerHandler" />
</enterprise-beans>
<assembly-descriptor>
<default-method-access>
<security-role-mapping name="<default-ejb-caller-role>" impliesAll="true" />
</default-method-access>
</assembly-descriptor>
</orion-ejb-jar>
I know that this was much fact for one question but perhaps I miss something that you could see. Perhaps I'm heading the wrong way and this is not possible or a good idea at all. I have spent the day reading the orinserver doc and I think it should look like this but again I guess that I have miss something.
Hopefully you could help me with this.
Best regards
Fredrik
