<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Mini (Stateful) EJB 3 Example using JBoss and Eclipse
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/phiboss">Phi GF</a> in <i>EJB3 Development</i> - <a href="http://community.jboss.org/message/630901#630901">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hello Community </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>(appologise I am new to JBoss 3.0)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>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.)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Here my configuration:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><strong>JBoss Application Server 7.0.1 final</strong></p><p><strong>OS: Linux Ubuntu 64 Bit (10.10)</strong></p><p><strong>Eclipse 64 Bit J2EE (indigo)</strong></p><p><strong>Java 1.6.0_26</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I have 2 Projects in my Eclipse Workbench (a Server + a Client). The Server contains 2 classes:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>MyTimerRemote.java:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">package ch.my.timer.server;</span></p><p><span style="font-family: courier new,courier;">import javax.ejb.Remote;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">@Remote</span></p><p><span style="font-family: courier new,courier;">public interface MyTimerRemote {</span></p><p><span style="font-family: courier new,courier;">  void  setRemainingSecs(int seconds);</span></p><p><span style="font-family: courier new,courier;">  float getRemainingSecs();</span></p><p><span style="font-family: courier new,courier;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>and MyTimer.java (also on the server):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">package ch.my.timer.server;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">import javax.ejb.Stateful;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">/**</span></p><p><span style="font-family: courier new,courier;"> * Session Bean implementation class MyTimer</span></p><p><span style="font-family: courier new,courier;"> */</span></p><p><span style="font-family: courier new,courier;">@Stateful</span></p><p><span style="font-family: courier new,courier;">public class MyTimer implements MyTimerRemote {</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    public MyTimer() {</span></p><p><span style="font-family: courier new,courier;">        this.endTimePoint = System.currentTimeMillis();</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    private long endTimePoint; // timestamp in milliseconds</span></p><p>    </p><p><span style="font-family: courier new,courier;">    @Override</span></p><p><span style="font-family: courier new,courier;">    public void setRemainingSecs(int seconds) {</span></p><p><span style="font-family: courier new,courier;">        long millis = seconds * 1000;</span></p><p><span style="font-family: courier new,courier;">        long now    = System.currentTimeMillis();</span></p><p><span style="font-family: courier new,courier;">        this.endTimePoint = now + millis;</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    @Override</span></p><p><span style="font-family: courier new,courier;">    public float getRemainingSecs() {</span></p><p><span style="font-family: courier new,courier;">        long now   = System.currentTimeMillis();</span></p><p><span style="font-family: courier new,courier;">        long diff  = this.endTimePoint - now;</span></p><p><span style="font-family: courier new,courier;">        float secs = (float) (diff / 1000.0);</span></p><p><span style="font-family: courier new,courier;">        return secs;</span></p><p><span style="font-family: courier new,courier;">    } </span></p><p><span style="font-family: courier new,courier;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The Client Project contains a simple Test class (MyTimerTest.java): </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">package ch.my.timer.client;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">import java.util.Hashtable;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">import javax.naming.Context;</span></p><p><span style="font-family: courier new,courier;">import javax.naming.InitialContext;</span></p><p><span style="font-family: courier new,courier;">import javax.naming.NamingException;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">import  ch.my.timer.server.*;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">public class MyTimerTest {</span></p><p><span style="font-family: courier new,courier;">  public static void main(String[] args) {</span></p><p><span style="font-family: courier new,courier;">     try {</span></p><p><span style="font-family: courier new,courier;">        new MyTimerTest().top();</span></p><p><span style="font-family: courier new,courier;">     } catch (NamingException e) {</span></p><p><span style="font-family: courier new,courier;">        // TODO Auto-generated catch block</span></p><p><span style="font-family: courier new,courier;">        e.printStackTrace();</span></p><p><span style="font-family: courier new,courier;">     }</span></p><p><span style="font-family: courier new,courier;">   }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">   void top() throws NamingException {</span></p><p><span style="font-family: courier new,courier;">      Hashtable<String, String> env = new Hashtable<String, String>();      </span></p><p><span style="font-family: courier new,courier;">      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");     </span></p><p><span style="font-family: courier new,courier;">      env.put(Context.PROVIDER_URL, "localhost:1099");      </span></p><p><span style="font-family: courier new,courier;">      env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" ); </span></p><p><span style="font-family: courier new,courier;">      InitialContext ctx = new InitialContext(env);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">      MyTimerRemote str = (MyTimerRemote) ctx.lookup("java:module/SantisTimer");</span></p><p>     </p><p><span style="font-family: courier new,courier;">      str.setRemainingSecs(3);</span></p><p><span style="font-family: courier new,courier;">      float rem = str.getRemainingSecs();</span></p><p><span style="font-family: courier new,courier;">      System.out.println("Remaining: " + rem);</span></p><p><span style="font-family: courier new,courier;">   }</span></p><p><span style="font-family: courier new,courier;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Starting the server (using "run on server -> JBoss 7.0 Runtime Server, i get the following output: </p><p><span style="font-family: courier new,courier;">15:05:06,995 INFO  [org.jboss.modules] JBoss Modules version 1.0.1.GA</span></p><p><span style="font-family: courier new,courier;">15:05:07,394 INFO  [org.jboss.msc] JBoss MSC version 1.0.0.GA</span></p><p><span style="font-family: courier new,courier;">  ...</span></p><p><span style="font-family: courier new,courier;">15:05:11,026 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) Starting deployment of "EJB_Servenull"</span></p><p><span style="font-family: courier new,courier;">15:05:11,146 INFO  [org.jboss.as.jpa] (MSC service thread 1-1) added javax.persistence.api dependency to EJB_Servenull</span></p><p><span style="font-family: courier new,courier;">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:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    java:global/EJB_Servenull/MyTimer!ch.my.timer.server.MyTimerRemote</span></p><p><span style="font-family: courier new,courier;">    java:app/EJB_Servenull/MyTimer!ch.my.timer.server.MyTimerRemote</span></p><p><span style="font-family: courier new,courier;">    java:module/MyTimer!ch.my.timer.server.MyTimerRemote</span></p><p><span style="font-family: courier new,courier;">    java:global/EJB_Servenull/MyTimer</span></p><p><span style="font-family: courier new,courier;">    java:app/EJB_Servenull/MyTimer</span></p><p><span style="font-family: courier new,courier;">    java:module/MyTimer</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">15:05:11,434 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "EJB_Servenull"</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Starting the client (run as Java Application) I get: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]</span></p><p><span style="font-family: courier new,courier;">    at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1302)</span></p><p><span style="font-family: courier new,courier;">    at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1382)</span></p><p><span style="font-family: courier new,courier;">    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:579)</span></p><p><span style="font-family: courier new,courier;">    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)</span></p><p><span style="font-family: courier new,courier;">    at javax.naming.InitialContext.lookup(InitialContext.java:392)</span></p><p><span style="font-family: courier new,courier;">    at ch.my.timer.client.MyTimerTest.top(MyTimerTest.java:28)</span></p><p><span style="font-family: courier new,courier;">    at ch.my.timer.client.MyTimerTest.main(MyTimerTest.java:14)</span></p><p><span style="font-family: courier new,courier;">Caused by: java.net.SocketTimeoutException: Receive timed out</span></p><p><span style="font-family: courier new,courier;">    at java.net.PlainDatagramSocketImpl.receive0(Native Method)</span></p><p><span style="font-family: courier new,courier;">    at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:145)</span></p><p><span style="font-family: courier new,courier;">    at java.net.DatagramSocket.receive(DatagramSocket.java:725)</span></p><p><span style="font-family: courier new,courier;">    at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1272)</span></p><p><span style="font-family: courier new,courier;">    ... 6 more</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Any Ideas? What am I missing?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thanks in advance.</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/630901#630901">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2030">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>