ActiveMQ介绍(二)

一、PTP方式的消息发送

/**
 * 
 */
package com.sugon.activemq.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * @author WangMing
 * 
 */
public class QueueSendMessage {
	public static void main(String[] args) {
		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://10.0.31.40:61616");
		Connection conn = null;
		Session session = null;
		MessageProducer producer = null;
		try {
			conn = cf.createConnection();
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = new ActiveMQQueue("messageQueue");
			producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//不持久化消息
			TextMessage message = session.createTextMessage("这是发送到队列\"messageQueue\"的消息!");
			conn.start();
			long starttime = System.currentTimeMillis();
			producer.send(message);
			long endtime = System.currentTimeMillis();
			System.out.println("消息发送成功!\t所用时间为:"+(endtime-starttime)+"毫秒");
		} catch (JMSException e) {
			e.printStackTrace();
		}finally{
			if(session != null || conn!=null || producer!=null){
				try {
					producer.close();
					session.close();
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
				
			}
		}

	}
}

运行结果:

ActiveMQ介绍(二)_第1张图片

 

二、PTP方式的消息接收(非消息监听器)

package com.sugon.activemq.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * @author WangMing
 *
 */
public class QueueReceiveMessage { 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://10.0.31.40:61616");
		Connection conn = null;
		Session session = null;
		MessageConsumer consumer = null;
		try {
			conn = cf.createConnection();
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = new ActiveMQQueue("messageQueue");
			consumer = session.createConsumer(destination);
			conn.start();
			long starttime = System.currentTimeMillis();
			Message message = consumer.receive();
			long endtime = System.currentTimeMillis();
			TextMessage textMessage = (TextMessage) message;
			System.out.println("消息接收成功!\t所用时间为:"+(endtime-starttime)+"毫秒");
			System.out.println("消息目标为:"+message.getJMSDestination().toString());
			System.out.println("接收到的消息为:\t"+textMessage.getText()+"\t消息ID为:\t"+textMessage.getJMSMessageID());
		} catch (JMSException e) {
			e.printStackTrace();
		}finally{
			if(session != null || conn!=null ||consumer!=null){
				try {
					consumer.close();
					session.close();
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
				
			}
		}
		

	}

}

运行结果:

ActiveMQ介绍(二)_第2张图片

 

三、PTP方式的消息接收(消息监听器)

package com.sugon.activemq.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * @author WangMing
 *
 */
public class QueueMessageListenerReceiveMessage  {

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://10.0.31.40:61616");
		Connection conn = null;
		Session session = null;
		try {
			conn = cf.createConnection();
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = new ActiveMQQueue("messageQueue");
			MessageConsumer consumer = session.createConsumer(destination);
			conn.start();
			consumer.setMessageListener(new MessageListener() {
				
				@Override
				public void onMessage(Message message) {
                    try {
                        System.out.println("接收到的消息为:\t" + ((TextMessage) message).getText());
                    	
                    } catch (JMSException e) {
                        e.printStackTrace();
                    } 
					
				}
			});
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
	}

}

监听器方式的消息接收启动后会一直运行,当有新消息到达中间件时,中间件会把消息推送给客户端。

ActiveMQ介绍(二)_第3张图片

再次向中间件发送消息后,会推送到客户端进行处理。

ActiveMQ介绍(二)_第4张图片

你可能感兴趣的:(activemq)