[jboss-dev-forums] [EJB3 Development] - Mini (Stateful) EJB 3 Example using JBoss and Eclipse

Phi GF do-not-reply at jboss.com
Mon Oct 10 09:10:09 EDT 2011


Phi GF [http://community.jboss.org/people/phiboss] created the discussion

"Mini (Stateful) EJB 3 Example using JBoss and Eclipse"

To view the discussion, visit: http://community.jboss.org/message/630901#630901

--------------------------------------------------------------
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.
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/630901#630901]

Start a new discussion in EJB3 Development at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2030]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20111010/a1e205cc/attachment.html 


More information about the jboss-dev-forums mailing list