EJB design: call ejb from java client application

  1. call ejb from java client application (6 messages)

    can someone tell me how to call EJB from a java client application?
  2. call ejb from java client application[ Go to top ]

    I know to many people this sounds simple, but frankly the examples out there suck.

    Luckily it's fairly simple.

    First you must put the Home and Remote implementations as well as any utility classes in their own jar. The EJB 2.0 spec allows for automatic creations (WebLogic 6.1's ejbc tool does this for you) by specifying the ejb-client-jar tag like this:
    <ejb-client-jar>client.jar</ejb-client-jar> in the ejb.xml file.

    Next put this jar file in your client applications classpath.

    Import all of the required classes into your application (J2EE stuff as well as your client side stuff):
    Then to actually retrieve it:
    Context ctx = new InitialContext();

    // name is whatever JNDI name you gave it
    Object o = ctx.lookup("home name");

    EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(o,EJBHome.class);

    // This is userID should be the one passed.
    EJB ejb = ejbHome.create(..);

  3. call ejb from java client application[ Go to top ]

    Thank you very much.and...
     [Next put this jar file in your client applications classpath] means not only deploy this jar in web application server but also copy this jar file to my client machine,doesn't it?
    and would you like to show me the whole sample about it?
  4. call ejb from java client application[ Go to top ]

    Yes , even the client program would need the jar.
    Some containers like PowerTier also provides a
    client application jar which is much thinner and
    just carries all the required home and transportation

    You need to set your context factory as well as provider
    as follows and pass it to initial context.

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    env.put(Context.PROVIDER_URL, "t3://localhost:7001");

    InitialContext ic = new InitialContext(env);System.out.println("Got Initial context");

    EjbTestHome testhome = (EjbTestHome) PortableRemoteObject.narrow(ic.lookup("MyPackage.MyServerName"), EjbTestHome.class);

  5. I also had a trouble when try to compile stand alone client application to access EJB, i already added the jars on the classpath j2ee.jar, HelloWorldJAR.jar(EJB Jar) and HelloWorldEARClient.jar(Stuff return when i deployed the EBJ). And i allways got like this :

    HelloWorldClient.java:32: '.' expected
    import HelloWorld;
    HelloWorldClient.java:33: '.' expected
    import HelloWorldHome;
    2 errors

    anybody can tell me what i have to do .....
  6. This is happening because probably your class HelloWorld is in some package(not in an unnamed package). To verify this, extract your client jar and see whether stub/ EJBObject classes generate for HelloWorld component interface are in some package. Then modify your import statement. it should work. Thanks Amresh
  7. Hi, can somebody tell me, i am invoking the remote object from the java application & it gives me error: "java.lang.ClassCastException: Could not load a proxy stub. Possible reason is that the context classloader do not has reference to p4 service." at com.sap.engine.services.rmi_p4.P4ObjectBroker.narrow(P4ObjectBroker.java:607) at com.sap.engine.services.rmi_p4.P4ObjectBroker.narrow(P4ObjectBroker.java:453) at com.sap.engine.services.rmi_p4.P4ObjectBroker.narrow(P4ObjectBroker.java:449) at com.sap.engine.services.rmi_p4.reflect.P4InvocationHandler.invokeInternal(P4InvocationHandler.java:171) at com.sap.engine.services.rmi_p4.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:50) at $Proxy0.create(Unknown Source) but if i package & the same java class in the same package with ejb . it works fine. i have checked the ejb is in the class path of my java application thanks