8 Replies Latest reply on Oct 12, 2011 3:41 AM by jaikiran pai Branched to a new discussion.

    Mini (Stateful) EJB 3 Example using JBoss and Eclipse

    Phi GF Newbie

      Hello Community

       

      (appologise I am new to JBoss 3.0)

       

      I am looking for a simple EJB 3.0 Example using a Stateful Bean to demonstrate the components and concepts of an application server. I have searched several forums and googled a lot, but I have found mostly solutions to Problems I did not encounter. (I have an EJB 2 Example, which is not applicable, because my trainees will work on EJB 3.0 or 3.1 later.)

       

      Here my configuration:

       

       

      JBoss Application Server 7.0.1 final

      OS: Linux Ubuntu 64 Bit (10.10)

      Eclipse 64 Bit J2EE (indigo)

      Java 1.6.0_26

       

       

      I have 2 Projects in my Eclipse Workbench (a Server + a Client). The Server contains 2 classes:

       

      MyTimerRemote.java:

       

      package ch.my.timer.server;

      import javax.ejb.Remote;

       

      @Remote

      public interface MyTimerRemote {

        void  setRemainingSecs(int seconds);

        float getRemainingSecs();

      }

       

      and MyTimer.java (also on the server):

       

      package ch.my.timer.server;

       

      import javax.ejb.Stateful;

       

       

      /**

      * Session Bean implementation class MyTimer

      */

      @Stateful

      public class MyTimer implements MyTimerRemote {

       

          public MyTimer() {

              this.endTimePoint = System.currentTimeMillis();

          }

       

          private long endTimePoint; // timestamp in milliseconds

         

          @Override

          public void setRemainingSecs(int seconds) {

              long millis = seconds * 1000;

              long now    = System.currentTimeMillis();

              this.endTimePoint = now + millis;

          }

       

          @Override

          public float getRemainingSecs() {

              long now   = System.currentTimeMillis();

              long diff  = this.endTimePoint - now;

              float secs = (float) (diff / 1000.0);

              return secs;

          }

      }

       

       

      The Client Project contains a simple Test class (MyTimerTest.java):

       

       

      package ch.my.timer.client;

       

      import java.util.Hashtable;

       

      import javax.naming.Context;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

       

      import  ch.my.timer.server.*;

       

      public class MyTimerTest {

        public static void main(String[] args) {

           try {

              new MyTimerTest().top();

           } catch (NamingException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

         }

       

         void top() throws NamingException {

            Hashtable<String, String> env = new Hashtable<String, String>();     

            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");    

            env.put(Context.PROVIDER_URL, "localhost:1099");     

            env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );

            InitialContext ctx = new InitialContext(env);

       

            MyTimerRemote str = (MyTimerRemote) ctx.lookup("java:module/SantisTimer");

          

            str.setRemainingSecs(3);

            float rem = str.getRemainingSecs();

            System.out.println("Remaining: " + rem);

         }

      }

       

      Starting the server (using "run on server -> JBoss 7.0 Runtime Server, i get the following output:

      15:05:06,995 INFO  [org.jboss.modules] JBoss Modules version 1.0.1.GA

      15:05:07,394 INFO  [org.jboss.msc] JBoss MSC version 1.0.0.GA

        ...

      15:05:11,026 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) Starting deployment of "EJB_Servenull"

      15:05:11,146 INFO  [org.jboss.as.jpa] (MSC service thread 1-1) added javax.persistence.api dependency to EJB_Servenull

      15:05:11,237 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-4) JNDI bindings for session bean named MyTimer in deployment unit deployment "EJB_Servenull" are as follows:

       

          java:global/EJB_Servenull/MyTimer!ch.my.timer.server.MyTimerRemote

          java:app/EJB_Servenull/MyTimer!ch.my.timer.server.MyTimerRemote

          java:module/MyTimer!ch.my.timer.server.MyTimerRemote

          java:global/EJB_Servenull/MyTimer

          java:app/EJB_Servenull/MyTimer

          java:module/MyTimer

       

      15:05:11,434 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "EJB_Servenull"

       

      Starting the client (run as Java Application) I get:

       

      javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]

          at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1302)

          at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1382)

          at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:579)

          at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)

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

          at ch.my.timer.client.MyTimerTest.top(MyTimerTest.java:28)

          at ch.my.timer.client.MyTimerTest.main(MyTimerTest.java:14)

      Caused by: java.net.SocketTimeoutException: Receive timed out

          at java.net.PlainDatagramSocketImpl.receive0(Native Method)

          at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:145)

          at java.net.DatagramSocket.receive(DatagramSocket.java:725)

          at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1272)

          ... 6 more

       

      Any Ideas? What am I missing?

       

      Thanks in advance.

        • 1. Re: Mini (Stateful) EJB 3 Example using JBoss and Eclipse
          Phi GF Newbie

          I have just updated the lookup() command (I used a "SantisTimer.java" earlier), but the same error:

           

            MyTimerRemote str = (MyTimerRemote) ctx.lookup("java:module/MyTimer");
          • 2. Re: Mini (Stateful) EJB 3 Example using JBoss and Eclipse
            jaikiran pai Master

            EJB access from a remote client is not yet implemented http://community.jboss.org/thread/173385?tstart=0. It will be available very soon in the upcoming weeks.

            1 of 1 people found this helpful
            • 3. Re: Mini (Stateful) EJB 3 Example using JBoss and Eclipse
              Phi GF Newbie

              Is there another version (eg. JBoss Application-Server/Client 6.0, 5.0) which is able for EJB 3.0?

              • 4. Re: Mini (Stateful) EJB 3 Example using JBoss and Eclipse
                jaikiran pai Master

                JBoss AS 6.1.0 has remote EJB access.

                1 of 1 people found this helpful
                • 6. Re: Mini (Stateful) EJB 3 Example using JBoss and Eclipse
                  Phi GF Newbie

                  Hi all

                   

                  I made another try using JBoss 6.1.0.

                  It did not realy work :-(

                   

                   

                  Any Ideas?

                   

                   

                  Here my interface (server & client):

                   

                  package ch.my.timer.server;

                  import javax.ejb.Remote;

                   

                  @Remote

                  public interface MyTimerRemote {

                    void  setRemainingSecs(int seconds);

                    float getRemainingSecs();

                  }

                   

                   

                  Implementaiton and statefull Session Bean:

                   

                  package ch.my.timer.server;

                   

                  import javax.ejb.Stateful;

                   

                  /**

                  * Session Bean implementation class MyTimer

                  */

                  @Stateful(mappedName="MyTimer")

                  public class MyTimer implements MyTimerRemote {

                   

                      public MyTimer() {

                          this.endTimePoint = System.currentTimeMillis();

                      }

                   

                      private long endTimePoint; // timestamp in milliseconds

                   

                      @Override

                      public void setRemainingSecs(int seconds) {

                          long millis = seconds * 1000;

                          long now    = System.currentTimeMillis();

                          this.endTimePoint = now + millis;

                      }

                   

                      @Override

                      public float getRemainingSecs() {

                          long now  = System.currentTimeMillis();

                          long diff = this.endTimePoint - now;

                          float secs = (float) (diff / 1000.0);

                          return secs;

                      }

                   

                  }

                   

                   

                  Server starts, but does not seam to deploy:

                   

                   

                   

                  08:24:25,718 INFO  [AbstractJBossASServerBase] Server Configuration:

                   

                      JBOSS_HOME URL: file:/usr/share/jboss-6.1.0.Final/

                      Bootstrap: $JBOSS_HOME/server/default/conf/bootstrap.xml

                      Common Base: $JBOSS_HOME/common/

                      Common Library: $JBOSS_HOME/common/lib/

                      Server Name: default

                      Server Base: $JBOSS_HOME/server/

                      Server Library: $JBOSS_HOME/server/default/lib/

                      Server Config: $JBOSS_HOME/server/default/conf/

                      Server Home: $JBOSS_HOME/server/default/

                      Server Data: $JBOSS_HOME/server/default/data/

                      Server Log: $JBOSS_HOME/server/default/log/

                      Server Temp: $JBOSS_HOME/server/default/tmp/

                   

                  08:24:25,750 INFO  [AbstractServer] Starting: JBossAS [6.1.0.Final "Neo"]

                  08:24:31,797 INFO  [ServerInfo] Java version: 1.6.0_26,Sun Microsystems Inc.

                  08:24:31,805 INFO  [ServerInfo] Java Runtime: Java(TM) SE Runtime Environment (build 1.6.0_26-b03)

                  08:24:31,806 INFO  [ServerInfo] Java VM: Java HotSpot(TM) 64-Bit Server VM 20.1-b02,Sun Microsystems Inc.

                  08:24:31,806 INFO  [ServerInfo] OS-System: Linux 2.6.38-11-generic,amd64

                  08:24:31,806 INFO  [ServerInfo] VM arguments: -Dprogram.name=JBossTools: JBoss 6.x Runtime -Xms256m -Xmx768m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.endorsed.dirs=/usr/share/jboss-6.1.0.Final/lib/endorsed -Djava.library.path=/usr/share/jboss-6.1.0.Final/bin/native -Dlogging.configuration=file:/usr/share/jboss-6.1.0.Final/bin/logging.properties -Dfile.encoding=UTF-8

                  08:24:32,012 INFO  [JMXKernel] Legacy JMX core initialized

                  08:24:47,919 INFO  [AbstractServerConfig] JBoss Web Services - Stack CXF Server 3.4.1.GA

                  08:24:49,246 INFO  [JSFImplManagementDeployer] Initialized 3 JSF configurations: [Mojarra-1.2, MyFaces-2.0, Mojarra-2.0]

                  08:25:05,427 WARNING [FileConfigurationParser] AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal

                  08:25:05,924 INFO  [JMXConnector] starting JMXConnector on host localhost:1090

                  08:25:06,185 INFO  [MailService] Mail Service bound to java:/Mail

                  08:25:08,150 INFO  [HornetQServerImpl] live server is starting with configuration HornetQ Configuration (clustered=false,backup=false,sharedStore=true,journalDirectory=/usr/share/jboss-6.1.0.Final/server/default/data/hornetq/journal,bindingsDirectory=/usr/share/jboss-6.1.0.Final/server/default/data/hornetq/bindings,largeMessagesDirectory=/usr/share/jboss-6.1.0.Final/server/default/data/hornetq/largemessages,pagingDirectory=/usr/share/jboss-6.1.0.Final/server/default/data/hornetq/paging)

                  08:25:08,153 INFO  [HornetQServerImpl] Waiting to obtain live lock

                  08:25:08,406 INFO  [JournalStorageManager] Using NIO Journal

                  08:25:08,478 WARNING [HornetQServerImpl] Security risk! It has been detected that the cluster admin user and password have not been changed from the installation default. Please see the HornetQ user guide, cluster chapter, for instructions on how to do this.

                  08:25:08,835 INFO  [FileLockNodeManager] Waiting to obtain live lock

                  08:25:08,836 INFO  [FileLockNodeManager] Live Server Obtained live lock

                  08:25:10,601 INFO  [NettyAcceptor] Started Netty Acceptor version 3.2.3.Final-r${buildNumber} localhost:5455 for CORE protocol

                  08:25:10,603 INFO  [NettyAcceptor] Started Netty Acceptor version 3.2.3.Final-r${buildNumber} localhost:5445 for CORE protocol

                  08:25:10,608 INFO  [HornetQServerImpl] Server is now live

                  08:25:10,609 INFO  [HornetQServerImpl] HornetQ Server version 2.2.5.Final (HQ_2_2_5_FINAL_AS7, 121) [ae730dd9-f3e9-11e0-86b0-001f3b82ac6b] started

                  08:25:10,690 INFO  [WebService] Using RMI server codebase: http://localhost:8083/

                  08:25:11,057 INFO  [jbossatx] ARJUNA-32010 JBossTS Recovery Service (tag: JBOSSTS_4_14_0_Final) - JBoss Inc.

                  08:25:11,065 INFO  [arjuna] ARJUNA-12324 Start RecoveryActivators

                  08:25:11,133 INFO  [arjuna] ARJUNA-12296 ExpiredEntryMonitor running at Wed, 12 Oct 2011 08:25:11

                  08:25:11,235 INFO  [arjuna] ARJUNA-12310 Recovery manager listening on endpoint 127.0.0.1:4712

                  08:25:11,243 INFO  [arjuna] ARJUNA-12344 RecoveryManagerImple is ready on port 4712

                  08:25:11,243 INFO  [jbossatx] ARJUNA-32013 Starting transaction recovery manager

                  08:25:11,273 INFO  [arjuna] ARJUNA-12163 Starting service com.arjuna.ats.arjuna.recovery.ActionStatusService on port 4713

                  08:25:11,274 INFO  [arjuna] ARJUNA-12337 TransactionStatusManagerItem host: 127.0.0.1 port: 4713

                  08:25:11,340 INFO  [arjuna] ARJUNA-12170 TransactionStatusManager started on port 4713 and host 127.0.0.1 with service com.arjuna.ats.arjuna.recovery.ActionStatusService

                  08:25:11,468 INFO  [jbossatx] ARJUNA-32017 JBossTS Transaction Service (JTA version - tag: JBOSSTS_4_14_0_Final) - JBoss Inc.

                  08:25:11,606 INFO  [arjuna] ARJUNA-12202 registering bean jboss.jta:type=ObjectStore.

                  08:25:12,323 INFO  [AprLifecycleListener] The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/share/jboss-6.1.0.Final/bin/native

                  08:25:12,640 INFO  [ModClusterService] Initializing mod_cluster 1.1.0.Final

                  08:25:12,661 INFO  [TomcatDeployment] deploy, ctxPath=/invoker

                  08:25:13,538 INFO  [RARDeployment] Required license terms exist, view vfs:/usr/share/jboss-6.1.0.Final/server/default/deploy/jboss-local-jdbc.rar/META-INF/ra.xml

                  08:25:13,576 INFO  [RARDeployment] Required license terms exist, view vfs:/usr/share/jboss-6.1.0.Final/server/default/deploy/jboss-xa-jdbc.rar/META-INF/ra.xml

                  08:25:13,591 INFO  [RARDeployment] Required license terms exist, view vfs:/usr/share/jboss-6.1.0.Final/server/default/deploy/jms-ra.rar/META-INF/ra.xml

                  08:25:13,616 INFO  [HornetQResourceAdapter] HornetQ resource adaptor started

                  08:25:13,635 INFO  [RARDeployment] Required license terms exist, view vfs:/usr/share/jboss-6.1.0.Final/server/default/deploy/mail-ra.rar/META-INF/ra.xml

                  08:25:13,672 INFO  [RARDeployment] Required license terms exist, view vfs:/usr/share/jboss-6.1.0.Final/server/default/deploy/quartz-ra.rar/META-INF/ra.xml

                  08:25:13,930 INFO  [SimpleThreadPool] Job execution threads will use class loader of thread: Thread-2

                  08:25:13,991 INFO  [SchedulerSignalerImpl] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

                  08:25:13,991 INFO  [QuartzScheduler] Quartz Scheduler v.1.8.3 created.

                  08:25:13,995 INFO  [RAMJobStore] RAMJobStore initialized.

                  08:25:13,998 INFO  [QuartzScheduler] Scheduler meta-data: Quartz Scheduler (v1.8.3) 'JBossQuartzScheduler' with instanceId 'NON_CLUSTERED'

                    Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.

                    NOT STARTED.

                    Currently in standby mode.

                    Number of jobs executed: 0

                    Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.

                    Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

                   

                  08:25:13,999 INFO  [StdSchedulerFactory] Quartz scheduler 'JBossQuartzScheduler' initialized from an externally opened InputStream.

                  08:25:13,999 INFO  [StdSchedulerFactory] Quartz scheduler version: 1.8.3

                  08:25:14,000 INFO  [QuartzScheduler] Scheduler JBossQuartzScheduler_$_NON_CLUSTERED started.

                  08:25:14,636 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=DefaultDS' to JNDI name 'java:DefaultDS'

                  08:25:15,092 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=ConnectionFactoryBinding,name=JmsXA' to JNDI name 'java:JmsXA'

                  08:25:15,220 INFO  [xnio] XNIO Version 2.1.0.CR2

                  08:25:15,232 INFO  [nio] XNIO NIO Implementation Version 2.1.0.CR2

                  08:25:15,639 INFO  [remoting] JBoss Remoting version 3.1.0.Beta2

                  08:25:15,776 INFO  [TomcatDeployment] deploy, ctxPath=/

                  08:25:15,890 INFO  [HornetQServerImpl] trying to deploy queue jms.queue.DLQ

                  08:25:15,932 INFO  [HornetQServerImpl] trying to deploy queue jms.queue.ExpiryQueue

                  08:25:15,999 INFO  [service] Removing bootstrap log handlers

                  08:25:16,129 INFO  [org.apache.coyote.http11.Http11Protocol] Starting Coyote HTTP/1.1 on http-localhost%2F127.0.0.1-8080

                  08:25:16,170 INFO  [org.apache.coyote.ajp.AjpProtocol] Starting Coyote AJP/1.3 on ajp-localhost%2F127.0.0.1-8009

                  08:25:16,172 INFO  [org.jboss.bootstrap.impl.base.server.AbstractServer] JBossAS [6.1.0.Final "Neo"] Started in 50s:406ms

                   

                   

                  Client Code:

                   

                   

                  package ch.my.timer.client;

                   

                  import java.util.Hashtable;

                   

                  import javax.naming.Context;

                  import javax.naming.InitialContext;

                  import javax.naming.NamingException;

                   

                  import  ch.my.timer.server.*;

                   

                  public class MyTimerTest {

                    public static void main(String[] args) {

                       try {

                          new MyTimerTest().top();

                       } catch (NamingException e) {

                          // TODO Auto-generated catch block

                          e.printStackTrace();

                       }

                     }

                   

                     void top() throws NamingException {

                        Hashtable<String, String> env = new Hashtable<String, String>();     

                        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");    

                        env.put(Context.PROVIDER_URL, "localhost:1099");     

                        env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );

                        InitialContext ctx = new InitialContext(env);

                   

                        MyTimerRemote str = (MyTimerRemote) ctx.lookup("/MyTimer");

                   

                        str.setRemainingSecs(3);

                        float rem = str.getRemainingSecs();

                        System.out.println("Remaining: " + rem);

                     }

                  }

                   

                  Ouptut of the client code:

                   

                   

                  javax.naming.NameNotFoundException: MyTimer not bound

                      at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)

                      at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)

                      at org.jnp.server.NamingServer.getObject(NamingServer.java:785)

                      at org.jnp.server.NamingServer.lookup(NamingServer.java:443)

                      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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)

                      at sun.rmi.transport.Transport$1.run(Transport.java:159)

                      at java.security.AccessController.doPrivileged(Native Method)

                      at sun.rmi.transport.Transport.serviceCall(Transport.java:155)

                      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)

                      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)

                      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)

                      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

                      at java.lang.Thread.run(Thread.java:662)

                      at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)

                      at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)

                      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)

                      at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)

                      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)

                      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)

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

                      at ch.my.timer.client.MyTimerTest.top(MyTimerTest.java:28)

                      at ch.my.timer.client.MyTimerTest.main(MyTimerTest.java:14)

                   

                   

                   

                  Any Ideas? Thanks in advance.

                  • 7. Re: Mini (Stateful) EJB 3 Example using JBoss and Eclipse
                    jaikiran pai Master

                    The important thing at this point is - how are you packaging and deploying the beans?