Requires Free Membership to View
import javax.jms.*;
public class MyAsyncReceiver
{
public MyAsyncReceiver()
{
String queueName = "myqueue";
try {
Context jndiContext = new InitialContext();
QueueConnectionFactory queueConnectionFactory =
(QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
QueueConnection queueConnection =
queueConnectionFactory.createQueueConnection();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue)jndiContext.lookup(queueName);
QueueReceiver queueReceiver =
queueSession.createReceiver(queue);
queueReceiver.setMessageListener(new MyMsgListener());
queueConnection.start();
// wait code should be inserted here
} catch (JMSException e) {
System.err.println("Exception: " + e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
System.err.println("Exception: " + e.toString());
}
}
}
}
}
class MyMsgListener implements MessageListener
{
private MyEJB myEJB = null;
public void onMessage(Message message)
{
System.out.println("Message received");
callEJB();
}
private void callEJB()
{
try {
if (myEJB == null) {
Context initial = new InitialContext();
Object objref =
initial.lookup("java:comp/env/ejb/MyEJB");
MyEJBHome home =
(MyEJBHome)PortableRemoteObject.narrow(objref,
MyEJBHome.class);
myEJB = home.create();
}
// Call the EJB's business method
myEJB.retrieveMessage();
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
}
}
}
This was first published in November 2003

Join the conversationComment
Share
Comments
Results
Contribute to the conversation