分类:
2008-11-16 22:32:48
@ConnectionConfig(destinationType = javax.jms.Queue.class, destinationJndiName = "queue/kuaffejb3/sample", durable = true, subscriptionId = "kuaffMessage") |
Messager.java package com.kuaff.ejb3.messager; import org.jboss.ejb3.mdb.ConnectionConfig; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.TextMessage; import javax.jms.MessageListener; @MessageDriven @ConnectionConfig(destinationType = javax.jms.Queue.class, destinationJndiName = "queue/kuaffejb3/sample", durable = true, subscriptionId = "kuaffMessage") public class Messager implements MessageListener { public void onMessage(Message recvMsg) { System.out.println("接收到的消息:"); try { TextMessage message = (TextMessage)recvMsg; System.out.println(message.getText()); } catch (JMSException e) { e.printStackTrace(); } } } |
Client.java package com.kuaff.ejb3.messager; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.TextMessage; import javax.naming.InitialContext; public class Client { public static void main(String[] args) throws Exception { QueueConnection cnn = null; QueueSender sender = null; QueueSession session = null; InitialContext ctx = new InitialContext(); Queue queue = (Queue) ctx.lookup("queue/kuaffejb3/sample"); QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory"); cnn = factory.createQueueConnection(); session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); TextMessage msg = session.createTextMessage("江湖快报:玉树临风风流倜傥的公子小巢又出现了。"); sender = session.createSender(queue); sender.send(msg); System.out.println("消息已经发出"); } } |
queue-example-service.xml <?xml version="1.0" encoding="UTF-8"?> <server> <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=kuaffMessage"> <attribute name="JNDIName">queue/kuaffejb3/sample</attribute> <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends> </mbean> </server> |