This example illustrates how to use the Novell exteNd Messaging Platform message bean
for simple create of message consumers. The MessageBean
class extends the
JMQMessageBean
and only have to invoke to appropriate constructor and implement the
onMessage
method. The connection factory, connection, session and consumer objects are
all available as bean properties in the JMQMessageBean
superclass.
Below is the source code for the MessageBean
class:
package bean; import javax.naming.InitialContext; import javax.jms.Queue; import javax.jms.Message; import javax.jms.TextMessage; import javax.jms.JMSException; import javax.jms.QueueConnectionFactory; import com.sssw.jms.api.JMQMessageBean; public class MessageBean extends JMQMessageBean { public static void main(String[] args) throws Exception { | // get the initial context | InitialContext ctx = new InitialContext(); | | // lookup the queue connection factory | QueueConnectionFactory factory = (QueueConnectionFactory) ctx. | lookup("queue/connectionFactory"); | | // lookup the queue object | Queue queue = (Queue) ctx.lookup("queue/queue0"); | | // create bean and enter dispatch wait loop | MessageBean bean = new MessageBean(factory, queue); | | System.out.println("bean ready to process messages ..."); | | bean.run(); } /** Simply calling the super class constructor connects this consumer. @param factory the queue connection factory @param queue the queue object */ public MessageBean(QueueConnectionFactory factory, Queue queue) throws JMSException { | super(factory, queue); } /** This method is called asynchronously by JMS when a message arrives at the queue. @param message A JMS message. */ public void onMessage(Message message) { | TextMessage msg = (TextMessage) message; | try { | | System.out.println("received: " + msg.getText()); | } catch (JMSException ex) { | | ex.printStackTrace(); | } } }
The MessageBean
class simply invokes the constructor of the
superclass with the connection factory and queue objects. The superclass
creates the necessary connection, session and queue receiver objects and
sets this class as the message listeners. As a result, the
MessageBean
class only has to implement the onMessage
method. The run
method can conveniently be used to suspend the
calling thread.
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.