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
package com.bekijkhet;
public interface HelloMDBEJBLocal {
public String sayHelloStateless();
public String sayHelloStatelessLocal();
public void setMember(String member);
}
broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJB.java
package com.bekijkhet;
public interface HelloMDBEJB {
public String sayHelloStateless();
public String sayHelloStatelessRemote();
public void setMember(String member);
}
broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJBBean.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;
}
}
broersa@debian1:~/work/HelloApp/HelloMDBEJB$ cat build.xml
<project name="HelloEJB" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/HelloMDBEJB.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
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
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();
}
}
}
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|#]
Wonderful! Thank you very much – this post helped me a LOT.
Comment by Michal — January 22, 2008 @ 2:44 pm