Problem invoking method on remote EJB from another EJB

Discussions

EJB programming & troubleshooting: Problem invoking method on remote EJB from another EJB

  1. I have a problem in trying to invoke a method on another EJB which is in another server from an EJB on one server. The 2 EJBs are stateless session beans. I am using 2 instances of JBoss for the 2 EJBs. When I try to invoke the create method on EJB 2 from EJB 1, I get a RemoteException indicating an rmi.MarshallException. In detail, it gives me a NotSerializableException. I find out I do get back a reference to the home interface of EJB 2. I don't know why this is the case. If I invoke an EJB in the same server then it works. I would be grateful for any help anyone can provide. Thanks.

    Here's the code I've written for EJB A and EJB B, I include the bean's implementation classes: EJB A:
    package A;

    import java.rmi.RemoteException;
    import javax.rmi.PortableRemoteObject;
    import javax.naming.InitialContext;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.ejb.EJBException;
    import java.net.MalformedURLException;
    import java.rmi.NotBoundException;
    import A.*;
    import java.util.Properties;
    import java.io.*;

    public class Bean implements javax.ejb.SessionBean
    {

       public javax.ejb.SessionContext ejbContext;
       public javax.naming.Context jndiContext;
       
       public void ejbCreate()
       {
       
        System.out.println("ABean: create()");

       }

       public String Method1(String user)
            throws RemoteException {
        System.out.println("ABean: method1");
    return user;
       }
            
       public void sendToB(String user)
            throws RemoteException {
          System.out.println("ABean: Sending to B ");

          try {
    Context jndiContext = getInitialContext();
    System.out.println("Got context");
    Object ref = jndiContext.lookup("B");
    System.out.println("Found B in JNDI context");

    B.BeanHomeRemote home = (B.BeanHomeRemote)
                  PortableRemoteObject.narrow (ref,B.BeanHomeRemote.class);
         System.out.println("Starting to invoke on B ");
    B.BeanRemote b = home.create();
    //b.Method1(user);

          }
          catch (MalformedURLException murle) {
             System.out.println();
             System.out.println(
                "MalformedURLException");
             System.out.println(murle);
          }
          catch (RemoteException re) { System.out.println();
             System.out.println(
                "RemoteException");
             System.out.println(re);
          }
          catch (Throwable t) { t.printStackTrace();}
          
        
        }

        public void ejbRemove(){}
        public void ejbActivate(){}
        public void ejbPassivate(){}

        public void setSessionContext(javax.ejb.SessionContext cntx){
    ejbContext = cntx;
    try {
    jndiContext = new InitialContext();
    }
    catch (NamingException ne) {
    throw new EJBException(ne);
    }
         }

        protected Object getHome(String name, Class type) {
    try {
    Object ref = jndiContext.lookup(name);
    return PortableRemoteObject.narrow(ref, type);
    }
    catch (NamingException ne) {
    throw new EJBException(ne);
    }
        }

        static public Context getInitialContext() throws Exception
        {
          //context initialized by jndi.properties file
          
          String jndiFile = "";
      
          java.util.Properties p = new java.util.Properties();
          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
          p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
          
          jndiFile = "E:\\Code\\B\\jndi
    jndi.properties";

          java.util.Properties jndi_prop = new Properties();
          jndi_prop.load(new FileInputStream(jndiFile));
          // look for Context.PROVIDER_URL in jndi file
    String jndi_url = jndi_prop.getProperty("java.naming.provider.url");
    //p.put(Context.PROVIDER_URL, "localhost:1099");
          p.put(Context.PROVIDER_URL, jndi_url);
          
          return new javax.naming.InitialContext(p);

       }

    }

    Here's EJB B:
    package B;

    import java.rmi.RemoteException;
    import javax.rmi.PortableRemoteObject;
    import javax.naming.InitialContext;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.ejb.EJBException;
    import java.net.MalformedURLException;
    import java.rmi.NotBoundException;
    import java.util.Properties;
    import java.io.*;

    public class Bean implements javax.ejb.SessionBean
    {

       public javax.ejb.SessionContext ejbContext;
       public javax.naming.Context jndiContext;

       public void ejbCreate()
       {
       
        //System.out.println("BBean: create()");

       }

       public String Method1(String user)
            throws RemoteException {
        System.out.println("BBean: method1");
    return user;
       }
            
       public void ejbRemove(){}
       public void ejbActivate(){}
       public void ejbPassivate(){}

       public void setSessionContext(javax.ejb.SessionContext cntx){
    ejbContext = cntx;
    try {
    jndiContext = new InitialContext();
    }
    catch (NamingException ne) {
    throw new EJBException(ne);
    }
        }

        protected Object getHome(String name, Class type) {
    try {
    Object ref = jndiContext.lookup(name);
    return PortableRemoteObject.narrow(ref, type);
    }
    catch (NamingException ne) {
    throw new EJBException(ne);
    }
        }

        static public Context getInitialContext() throws Exception
       {
          //context initialized by jndi.properties file
          
    String jndiFile = "";
      
          java.util.Properties p = new java.util.Properties();
          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
          p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
          
          jndiFile = "E:\\Code\\B\\jndi
    jndi.properties";

          java.util.Properties jndi_prop = new Properties();
          jndi_prop.load(new FileInputStream(jndiFile));
          // look for Context.PROVIDER_URL in jndi file
          String jndi_url = jndi_prop.getProperty("java.naming.provider.url");
    //p.put(Context.PROVIDER_URL, "localhost:1099");
          p.put(Context.PROVIDER_URL, jndi_url);
          
          return new javax.naming.InitialContext(p);

       }


    }

    hope that helps.
  2. hmm is nobody reading this?[ Go to top ]

    Just wondering if anybody has read my message cause I haven't heard any response back.

    Thanks for any help.