Friday, November 23, 2007

Create an EJB as JMS client

Friday, November 23, 2007 Posted by Andre Broers
In this sample we create an EJB  that calls the MDB from previous blog.

Lets start with the EJB code (don't look at the method names, I copied from a previous example :-))

broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJBLocal.java

[sourcecode language="java"]

package com.bekijkhet;
public interface HelloMDBEJBLocal {
  public String sayHelloStateless();
  public String sayHelloStatelessLocal();
  public void setMember(String member);
}
[/sourcecode]

broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJB.java

[sourcecode language="java"]

package com.bekijkhet;
public interface HelloMDBEJB {
  public String sayHelloStateless();
  public String sayHelloStatelessRemote();
  public void setMember(String member);
}
 [/sourcecode]

broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJBBean.java
[sourcecode language="java"]

package com.bekijkhet;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;
import javax.jms.*;
import javax.naming.*;
@Stateless
@Remote(HelloMDBEJB.class)
@Local(HelloMDBEJBLocal.class)
public class HelloMDBEJBBean implements HelloMDBEJB,HelloMDBEJBLocal {

  private String member = "Not set!";

  public String sayHelloStateless() {
          try {
            Context ctx = new InitialContext();
            ConnectionFactory     connectionFactory = (ConnectionFactory)ctx.lookup("jms/ConnectionFactory");
            Queue     queue = (Queue)ctx.lookup("jms/SampleQueue");
            javax.jms.Connection  connection = connectionFactory.createConnection();
            javax.jms.Session        session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(queue);
            TextMessage message = session.createTextMessage();
            message.setText("my test message");
            System.out.println( "MDBEJB:"+ message.getText());
            messageProducer.send(message);
            connection.close();
          } catch (Exception e) {System.out.println(e.toString());}
    return "Hello Stateless "+member;
  }
  public String sayHelloStatelessLocal() {
    return "Hello Local Stateless "+member;
  }
  public String sayHelloStatelessRemote() {
    return "Hello Remote Stateless "+member;
  }
  public void setMember(String member) {
    this.member=member;
  }
}
[/sourcecode]

broersa@debian1:~/work/HelloApp/HelloMDBEJB$ cat build.xml
[sourcecode language="XML"]


   
        simple example build file
   

 
 
 
 

 
   
   
   
   
 


          description="compile the source " >
   
   
 


          description="generate the distribution" >
   
   

   
   
 


          description="clean up" >
   
   
   
 


[/sourcecode]

after this build with

asant dist

and deploy

asadmin deploy dist/HelloMDBEJB.jar

than the client to call the EJB which is similar to the client  in the previous samples:

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloMDBEJBClient.java

[sourcecode language="java"]

package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.HelloMDBEJB;
public class HelloMDBEJBClient {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      HelloMDBEJB h = (HelloMDBEJB)ctx.lookup("com.bekijkhet.HelloMDBEJB");

      System.out.println(h.sayHelloStateless());
    }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
}
[/sourcecode]

compile:

javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloMDBEJB/dist/HelloMDBEJB.jar:. -d . HelloMDBEJBClient.java

run:

java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloMDBEJB/dist/HelloMDBEJB.jar:. com.bekijkhet.helloclient.HelloMDBEJBClient
Hello Stateless Not set!

and the output in the $GLASSFISH_HOME/domains/domain1/logs/server.log

[#|2007-11-23T13:49:49.076+0100|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=26;_ThreadName=p: thread-pool-1; w: 61;|
MDBEJB:my test message|#]

[#|2007-11-23T13:49:49.091+0100|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=26;_ThreadName=p: thread-pool-1; w: 61;|
Got message: my test message|#]