2 Replies Latest reply on Sep 5, 2012 7:18 AM by jaikiran

    how to call a local ejb bean in Jboss 7

    tony.peng

      i have a ejb project  include two class

       

      public interface TestLocal {

          public void sayHello(String name);

      }

       

      @Stateless

      @Local

      public class TestLocalBean implements TestLocal {

          public void sayHello(String name) {

              System.out.println("Hello " + name + " welcome to EJB 3!");

          }

      }

      it success deployed to jboss as 7 like

       

      18:58:02,144 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-4) JNDI bindings for session bean named TestLocalBean in deployment unit deployment "EJBTrainDemo.jar" are as follows:

       

          java:global/EJBTrainDemo/TestLocalBean!com.apj.demo1.TestLocal

          java:app/EJBTrainDemo/TestLocalBean!com.apj.demo1.TestLocal

          java:module/TestLocalBean!com.apj.demo1.TestLocal

          java:global/EJBTrainDemo/TestLocalBean

          java:app/EJBTrainDemo/TestLocalBean

          java:module/TestLocalBean

       

       

      my qustion: how to call a local bean .i try to run below code .it can't not run .anyone can help me? very thx.

       

      public class Demo1Test {

          protected static HelloWorld helloworld;

          protected static TestLocal localInstance;

       

          @BeforeClass

          public static void setUpBeforeClass() throws Exception {

              localInstance = (TestLocal) EJBFactory.getEJB("");

          }

       

          @Test

          public void testSayHello() {

       

              for (int i = 0; i < 10000; i++) {

                  localInstance.sayHello("----------tony loading ejb Test=:"+ i);

              }               

              

          }

       

      }

       

      public class EJBFactory {

          public static Object getEJB(String jndipath) {

              try {

       

                  InitialContext ctx = new InitialContext();

                  Object obj = ctx.lookup("java:app/EJBTrainDemo/TestLocalBean!com.apj.demo1.TestLocal");

                  return obj;

              } catch (NamingException e) {

                  e.printStackTrace();

              }

              return null;

          }

      }

        • 1. Re: how to call a local ejb bean in Jboss 7
          tony.peng

          the error message like

           

          javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

              at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)

              at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)

              at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)

              at javax.naming.InitialContext.lookup(InitialContext.java:392)

              at com.apj.client.EJBFactory.getEJB(EJBFactory.java:34)

              at com.apj.client.Demo1Test.setUpBeforeClass(Demo1Test.java:18)

              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

              at java.lang.reflect.Method.invoke(Method.java:597)

              at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

              at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

              at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

              at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)

              at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

              at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

              at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

              at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

              at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

              at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

              at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

          • 2. Re: how to call a local ejb bean in Jboss 7
            jaikiran

            You cannot use a local interface from a remote client (the JUnit class you posted is a remote client running in its own JVM). Take a look at Arquillian http://arquillian.org/ which allows in-container testing which will allow you to use the local interfaces in the testcases.