黑马程序员技术交流社区
标题: JavaEE的13种核心技术规范-JMS(4) [打印本页]
作者: li745547 时间: 2018-3-25 14:22
标题: JavaEE的13种核心技术规范-JMS(4)
- import javax.jms.Connection;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageListener;
- import javax.jms.Queue;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- import org.apache.activemq.command.ActiveMQQueue;
- public class DeliveryModeReceiveTest {
- public staticvoid main(String[] args)throws Exception {
- ActiveMQConnectionFactory factory = newActiveMQConnectionFactory("vm://localhost");
-
- Connection connection = factory.createConnection();
- connection.start();
-
- Queue queue = new ActiveMQQueue("testQueue");
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer comsumer = session.createConsumer(queue);
- comsumer.setMessageListener(new MessageListener(){
- public void onMessage(Message m) {
- try {
- System.out.println("Consumer get " + ((TextMessage)m).getText());
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
[java] view plain copy
- import javax.jms.Connection;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageListener;
- import javax.jms.Queue;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- import org.apache.activemq.command.ActiveMQQueue;
- public class DeliveryModeReceiveTest {
- public static void main(String[] args) throws Exception {
- ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
-
- Connection connection = factory.createConnection();
- connection.start();
-
- Queue queue = new ActiveMQQueue("testQueue");
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer comsumer = session.createConsumer(queue);
- comsumer.setMessageListener(new MessageListener(){
- public void onMessage(Message m) {
- try {
- System.out.println("Consumer get " + ((TextMessage)m).getText());
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
运行上面的程序,可以得到下面的输出结果:
Consumer get A persistent Message
可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。
另外, 如果发送一个non persistent消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失.
深入掌握JMS(八):JMSReplyTo
在下面的例子中,首先创建两个Queue,发送者给一个Queue发送,接收者接收到消息之后给另一个Queue回复一个Message,然后再创建一个消费者来接受所回复的消息。
[java] view plaincopy
- import javax.jms.Connection;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageListener;
- import javax.jms.MessageProducer;
- import javax.jms.Queue;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- import org.apache.activemq.command.ActiveMQQueue;
- public class MessageSendReceiveAndReply {
- public staticvoid main(String[] args)throws Exception {
- ActiveMQConnectionFactory factory = newActiveMQConnectionFactory("vm://localhost");
-
- Connection connection = factory.createConnection();
- connection.start();
-
- //消息发送到这个Queue
- Queue queue = new ActiveMQQueue("testQueue");
- //消息回复到这个Queue
- Queue replyQueue = new ActiveMQQueue("replyQueue");
-
- final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- //创建一个消息,并设置它的JMSReplyTo为replyQueue。
- Message message = session.createTextMessage("Andy");
- message.setJMSReplyTo(replyQueue);
-
- MessageProducer producer = session.createProducer(queue);
- producer.send(message);
-
- //消息的接收者
- MessageConsumer comsumer = session.createConsumer(queue);
- comsumer.setMessageListener(new MessageListener(){
- public void onMessage(Message m) {
- try {
- //创建一个新的MessageProducer来发送一个回复消息。
- MessageProducer producer = session.createProducer(m.getJMSReplyTo());
- producer.send(session.createTextMessage("Hello " + ((TextMessage) m).getText()));
- } catch (JMSException e1) {
- e1.printStackTrace();
- }
- }
-
- });
-
- //这个接收者用来接收回复的消息
- MessageConsumer comsumer2 = session.createConsumer(replyQueue);
- comsumer2.setMessageListener(new MessageListener(){
- public void onMessage(Message m) {
- try {
- System.out.println(((TextMessage) m).getText());
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
作者: Yin灬Yan 时间: 2018-3-25 19:04
我来占层楼啊
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |