Discussions

EJB programming & troubleshooting: Tomcat accessing EJB's deployed in Jboss

  1. Hi all,

    I have a big question, hope can you help me:

    i have one server with tomcat 5 jdk(jre) 1.4.x
    other server with JBoss 4.0.0 same jdk(jre)

    i want to have web applications in tomcat server and access to EJB container in Remote machine with JBoss, can someone explain to me how can i do that? because Tomcat has it owns JNDI service, how can i use that service to discover EJB's in JBoss remote or communicate JNDI's

         Machine A Machine B
     /--------------\
     | Tomcat |
     |--------------|
     | JSP + Servlet|
     | Access |
     | Remote EJB's |
     \--------------/
         |
      Ethernet
         |
        \ /
         .
     /-------\
     | Jboss |
     |-------|
     | EJB's |
     \-------/
    Thanks a lot,

    Greetings

    Martin Hermosilla

    Threaded Messages (9)

  2. URLs[ Go to top ]

    JNDI has URLs. Much like you use your web browser to key in different URLs to different web servers, JNDIs uses provider urls to point it to different JNDI providers to lookup objects.

    You'd need to configure the JNDI provider url and initial factory and pass it to your InitialContext and use that to lookup the EJBs in the remote JBoss server.

    Cheers.
  3. Hi all[ Go to top ]

    Well i found some code that migth be usefull but it seems to be context dependient in tomcat. all this building my own EjbRefFactory that returns home interface in lookups, the problem it that is dependent of context and i want to declare globally in some way to allow all web context access to Session Ejb's deployed in JBoss

    Or the other solution is to hardcode the remote reference.. but.. i think is not the best way to do that.. for code maintainment.

    Thanks for the help if you know how to deal with Tomcat's 5 server.xml to setup`a global Ejb Referene with Custom Factory cool

    Bye
  4. It is much easier if your tomcat container is jboss too.
    Assuming you don't use jboss as tomcat container, use steps below.

    your servlet is an application-client to the ejb you want to call.
    add file jndi.properties in your servlet archive, containing this entries:

    java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
    java.naming.provider.url=jnp://localhost:1099,jnp://yourremoteejbcontainerhost:1099
    java.naming.factory.url.pkgs=org.jboss.naming.client
    j2ee.clientName=TheUpToYouClientName

    Install 1 deployment descriptor file named TheUpToYouClientName.jar to your remote jboss directory.
    The TheUpToYouClientName.jar META-FILE directory contains:
    - jboss-client.xml
    - application-client.xml

    you should have known how to fill those two files.
    In the case you don't know how, here is the examples, assuming you will call an mdb:

    jboss-client.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE jboss-client PUBLIC
              "-//JBoss//DTD Application Client 4.0//EN"
              "http://www.jboss.org/j2ee/dtd/jboss-client_4_0.dtd">
    <jboss-client>
        <jndi-name>TheUpToYouClientName</jndi-name>
        <resource-ref>
            <res-ref-name>jms/XAConnectionFactory</res-ref-name>
            <jndi-name>XAConnectionFactory</jndi-name>
        </resource-ref>
        <resource-env-ref>
            <resource-env-ref-name>jms/YourQueue</resource-env-ref-name>
    <jndi-name>queue/YourQueue</jndi-name>
        </resource-env-ref>
    </jboss-client>


    application-client.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <application-client version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd">
    <display-name>TheUpToYouClientName</display-name>
    <resource-ref>
    <res-ref-name>jms/XAConnectionFactory</res-ref-name>
    <res-type>javax.jms.ConnectionFactory</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <message-destination-ref>
    <message-destination-ref-name>jms/YourQueue</message-destination-ref-name>
    <message-destination-type>javax.jms.Queue</message-destination-type>
    <message-destination-usage>Produces</message-destination-usage>
    </message-destination-ref>
    </application-client>

    add these jboss client jars to your servlet execution classpath:
    /jboss-4.0.2/client/activation.jar
      /jboss-4.0.2/client/avalon-framework.jar
      /jboss-4.0.2/client/axis-ws4ee.jar
      /jboss-4.0.2/client/commons-discovery.jar
      /jboss-4.0.2/client/commons-logging.jar
      /jboss-4.0.2/client/concurrent.jar
      /jboss-4.0.2/client/getopt.jar
      /jboss-4.0.2/client/jacorb.jar
      /jboss-4.0.2/client/jboss-client.jar
      /jboss-4.0.2/client/jboss-common-client.jar
      /jboss-4.0.2/client/jboss-deployment.jar
      /jboss-4.0.2/client/jboss-iiop-client.jar
      /jboss-4.0.2/client/jboss-j2ee.jar
      /jboss-4.0.2/client/jboss-jaxrpc.jar
      /jboss-4.0.2/client/jboss-jsr77-client.jar
      /jboss-4.0.2/client/jboss-juddiaxis.jar
      /jboss-4.0.2/client/jboss-saaj.jar
      /jboss-4.0.2/client/jboss-system-client.jar
      /jboss-4.0.2/client/jboss-transaction-client.jar
      /jboss-4.0.2/client/jboss-ws4ee-client.jar
      /jboss-4.0.2/client/jbossall-client.jar
      /jboss-4.0.2/client/jbosscx-client.jar
      /jboss-4.0.2/client/jbossha-client.jar
      /jboss-4.0.2/client/jbossjmx-ant.jar
      /jboss-4.0.2/client/jbossmq-client.jar
      /jboss-4.0.2/client/jbosssx-client.jar
      /jboss-4.0.2/client/jmx-client.jar
      /jboss-4.0.2/client/jmx-invoker-adaptor-client.jar
      /jboss-4.0.2/client/jnp-client.jar
      /jboss-4.0.2/client/log4j.jar
      /jboss-4.0.2/client/logkit.jar
      /jboss-4.0.2/client/mail.jar
      /jboss-4.0.2/client/namespace.jar
      /jboss-4.0.2/client/scout.jar
      /jboss-4.0.2/client/wsdl4j.jar

    hope this can help

    regards,

    Aldian
  5. Ok, it is working:

    I've used the context.xml file and a custom Naming Reference Factory that uses Ejb Tag and ResourceParams Tag in context.xml for tomcat web container. then i can use org.jboss.naming.HttpNamingContextFactory or org.jnp.interfaces.NamingContextFactory and cool got my references to from Tomcat to my remote JBoss Server.
    But i have another problem...
    this is Context dependient. i have to install in every context where i want to use my Ejb References this file, does anyone knows how to create a global Reference to objects something like context.xml file but for all tomcat and all context can access???
  6. tomcat 5.* , JSF and Jboss[ Go to top ]

    I want to do same thing in JSF, Tomcat 5.* and Jboss 4.*,
    why can not talk between tomcat and jboss. I use context.xml
    with ejb tag and resourceParameters tag.

    Martin, can you explain more, such as context.xml, jboss.xml, ejb-jar.xml and web.xml, jboss-web.xml, so on.

    Thanks
  7. Re: tomcat 5.* , JSF and Jboss[ Go to top ]

    Martin, can you explain more, such as context.xml, jboss.xml, ejb-jar.xml and web.xml, jboss-web.xml, so on.

    Thanks
    RTFM
  8. I've used a sample application (JSE 1.5) to get proxy from Jboss 4.0.4GA ejbhome and it's functional. When I tryed to use a servlet from Tomcat 5.5 happens a erros: "javax.naming.NameNotFoundException: Name LDCourseManager is not bound in this Context". How the steps, I'm beginer in J2EE. Thank for all!. ------------------------------------------------------ My sample Application: import java.rmi.RemoteException; import java.util.Collection; import java.util.Iterator; import org.coppercore.common.Util; import org.coppercore.exceptions.ConfigurationException; import org.coppercore.exceptions.ConnectionException; import org.coppercore.exceptions.CopperCoreException; import org.coppercore.interfaces.LDCourseManager; import org.coppercore.interfaces.LDCourseManagerHome; public class ExemploCliente { /** * @param args */ public static void main(String[] args) { Boolean validateOnly = true; String contentUriOffset = "http://localhost:8080/"; String uploadLocation = "/opt/coppercore/ccrt/data/upload"; String schemaLocation = "/opt/coppercore/ccrt/data/schemas"; String webroot = "jboss-4.0.4.GA/server/default/deploy/jbossweb-tomcat55.sar/ROOT.war"; String packageFileName = null; LDCourseManager courseManager = null; try { LDCourseManagerHome cmHome = null; String FACADE_NAME = "LDCourseManager"; Class FACADE_CLASS = LDCourseManagerHome.class; try { ServiceLocator locator = ServiceLocator.getInstance(); cmHome = (LDCourseManagerHome) locator.getEjbHome(FACADE_NAME, FACADE_CLASS); if (cmHome == null) { throw new ConfigurationException("Did not get home for " + FACADE_NAME); } courseManager = cmHome.create(); } catch (Exception e) { throw new ConnectionException(e); } try { System.out.println(courseManager.getVersion()); Collection users = courseManager.listUsers(); StringBuffer data = new StringBuffer(); data.append(""); Iterator iter = users.iterator(); while (iter.hasNext()) { String user = (String) iter.next(); data.append("<user id="\""" +="" user="" "\"="" />"); } data.append(""); System.out.println(Util.escapeSingleQuote(data.toString())); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (CopperCoreException e) { e.printStackTrace(); } } } ----------------------------------------------- My Servlet: package org.sakaiproject.player.tool; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Collection; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.coppercore.business.User; import org.coppercore.common.Util; import org.coppercore.delegate.LDCourseManagerDelegate; import org.coppercore.dto.RunDto; import org.coppercore.dto.RunParticipationDTO; import org.coppercore.dto.UolDto; import org.coppercore.exceptions.NotFoundException; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... Collection users = getCourseManagerDelegate().listUsers(); ... } } ... ------------------------------ My Delegate: package org.sakaiproject.player.tool; import java.io.File; import java.rmi.RemoteException; import java.util.Collection; import org.apache.log4j.Logger; import org.coppercore.common.MessageList; import org.coppercore.common.ServiceLocator; import org.coppercore.common.Util; import org.coppercore.dto.PublicationResult; import org.coppercore.dto.RunDto; import org.coppercore.dto.UolDto; import org.coppercore.dto.ValidationResult; import org.coppercore.exceptions.ConfigurationException; import org.coppercore.exceptions.ConnectionException; import org.coppercore.exceptions.CopperCoreException; import org.coppercore.exceptions.ValidationException; import org.coppercore.interfaces.LDCourseManager; import org.coppercore.interfaces.LDCourseManagerHome; import org.coppercore.validator.IMSCPFactory; import org.coppercore.validator.IMSPackage; public class LDCourseManagerDelegate { private LDCourseManager courseManager = null; ... private LDCourseManager getCourseManager() throws ConnectionException { if (courseManager == null) { LDCourseManagerHome cmHome = null; String FACADE_NAME = "LDCourseManager"; Class FACADE_CLASS = LDCourseManagerHome.class; try { ServiceLocator locator = ServiceLocator.getInstance(); cmHome = (LDCourseManagerHome) locator.getEjbHome(FACADE_NAME, FACADE_CLASS); if (cmHome == null) { throw new ConfigurationException("Did not get home for " + FACADE_NAME); } courseManager = cmHome.create(); } catch (Exception e) { throw new ConnectionException(e); } } return courseManager; } } ---------------------------------- My serviceLocator: package org.sakaiproject.player.tool; import java.util.Hashtable; import javax.ejb.EJBHome; import javax.ejb.EJBLocalHome; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import org.coppercore.exceptions.ServiceLocatorException; public class ServiceLocator { private static ServiceLocator serviceLocator; private static Context context; private ServiceLocator() throws ServiceLocatorException { context = getInitialContext(); } /** * Returns an new default initial context. * * @throws ServiceLocatorException * when the initial context could not be retrieved * @return Context the created initial context */ private Context getInitialContext() throws ServiceLocatorException { try { Hashtable environment = new Hashtable(); environment.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.HttpNamingContextFactory"); //$NON-NLS-1$ environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); //$NON-NLS-1$ environment.put(Context.PROVIDER_URL, Messages.getString("ServiceLocator.jnpNamedServer")); //$NON-NLS-1$ return new InitialContext(environment); } catch (NamingException e) { throw new ServiceLocatorException(e); } } /** * Retrieves the remote EJB home interface for the given name and class. * * @param ejbName * String the name of the ejb bean to create the home interface for * @param ejbClass * Class the class of the ejb to create the home interface for * @throws ServiceLocatorException * when there is an error retrieving the interface * @return EJBHome the home interface for the specified bean */ public EJBHome getEjbHome(String ejbName, Class ejbClass) throws ServiceLocatorException { try { Object object = context.lookup("WebPlayer/"+ejbName); EJBHome ejbHome = null; ejbHome = (EJBHome) PortableRemoteObject.narrow(object, ejbClass); if (ejbHome == null) { throw new ServiceLocatorException("Could not get home for " + ejbName); //$NON-NLS-1$ } return ejbHome; } catch (NamingException ne) { throw new ServiceLocatorException(ne); } } /** * Returns the local home interface for the specified bean. * * @param ejbName * String the name of the ejb to return the interface of * @throws ServiceLocatorException * when the interface could not be created * @return EJBLocalHome the local home interface for the specified bean */ public EJBLocalHome getEjbLocalHome(String ejbName) throws ServiceLocatorException { try { Object object = context.lookup(ejbName); EJBLocalHome ejbLocalHome = null; ejbLocalHome = (EJBLocalHome) object; if (ejbLocalHome == null) { throw new ServiceLocatorException("Could not get local home for " + ejbName); //$NON-NLS-1$ } return ejbLocalHome; } catch (NamingException ne) { throw new ServiceLocatorException(ne); } } /** * Returns a single ServiceLocator instance. * *

    * Requesting a ServiceLocator is an expensive operation, therefor this class * implements a singleton pattern to retrieve only one instance of the * ServiceLoactor and to re-use this instance later on. * * @throws ServiceLocatorException * when the ServiceLocator could not be created * @return ServiceLocator the singleton ServiceLocator */ public static synchronized ServiceLocator getInstance() throws ServiceLocatorException { if (serviceLocator == null) { serviceLocator = new ServiceLocator(); } return serviceLocator; } ----------------------------------- My Web.xml <!--?xml version="1.0" encoding="UTF-8"?--> <!-- This file generated by Sakai App Builder -AZ --> sakai-player Sakai Player LDCourseManager Session org.coppercore.LDCourseManagerHome org.coppercore.LDCourseManager <!-- Sakai request filter --> sakai.request org.sakaiproject.util.RequestFilter sakai.request sakai.player REQUEST FORWARD INCLUDE <!-- Tool Servlet --> sakai.player /runswitch.jsp 1 WebPlayer org.sakaiproject.player.tool.WebPlayer WebPlayer /WebPlayer Download org.sakaiproject.player.tool.Download Download /Download Administration org.sakaiproject.player.tool.Administration Administration /Administration <!-- Sakai listeners --> org.sakaiproject.util.ToolListener <!-- define the starting page for this app --> runswitch.jsp ---------------------- My context.xml in {apache-tomcat-5.5.25-homefolder}/conf factory com.amity.utils.tomcat.EJBRefFactory java.naming.provider.url jnp://jboss-server:1099 jndi-name LDCourseManager <!-- Default set of monitored resources --> WEB-INF/web.xml <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> -->

  9. Wow, its been a Year[ Go to top ]

    ok, I know it's been a year but... you may see this old post where I have implemented some clases to help access session beans: http://www.theserverside.com/discussions/thread.tss?thread_id=35811
  10. For a full summary check my blog: http://devgrok.blogspot.com/2009/06/injecting-remote-jboss-ejb-3-beans-into.html The gist of it is, if you use the @EJB annotation to use tomcat to do dependency injection, your bean can be mapped to your context via this: <!-- Makes Tomcat manage the ejb for you. -->