activeMQ 消息发送与接收(二)

三、发布/订阅模式
3.1、消息发送

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
 
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 发布/订阅模式--消息发送
 * 单连接、无持久化、无事务、消息接收自动确认
 */
public class TopicPublishers {

	public static void main(String[] args) {
		Connection conn = null;
		try{
			ActiveMQConnectionFactory connFactory = 
				new ActiveMQConnectionFactory("admin","showlike",
						"tcp://localhost:61616");
			conn = connFactory.createConnection();
			conn.start();
			
			Session session = conn.createSession(Boolean.FALSE,
					Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic("FirstTopic");
			
			MessageProducer producer = session.createProducer(topic);
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			
			long startTime = System.currentTimeMillis();
			int i = 1;
			do{
				TextMessage message = session.createTextMessage();
				message.setText(i+" , message_"+System.currentTimeMillis());
				producer.send(message);
				System.out.println("Sent message: "+message.getText());
				
//				Thread.sleep(1000);
				i++;
			}while(i<=100000);
			
			long endTime = System.currentTimeMillis();
	    	long diff = endTime - startTime;
	        long diffSeconds = diff / 1000;

	        System.out.print("top publishers total time: "+diffSeconds + " seconds.");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn != null){
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}

}


3.2、接收消息

import javax.jms.Connection;
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 javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 发布/订阅模式--消息接收
 * 单连接、无持久化、无事务、消息接收自动确认
 */
public class TopicSubscribers {

	public static void main(String[] args) {
		Connection conn = null;
		try{
			ActiveMQConnectionFactory connFactory = 
				new ActiveMQConnectionFactory("admin","showlike",
						"tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1");  
	        conn = connFactory.createConnection(); 
	        conn.start();  
	          
	        Session session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);  
	        Topic topic = session.createTopic("FirstTopic");  
	  
	        MessageConsumer consumer = session.createConsumer(topic); 
	        
	        long startTime = System.currentTimeMillis();
	        int i = 1;
	        do{
		        consumer.setMessageListener(new MessageListener(){  
		            public void onMessage(Message message) {  
		                TextMessage tm = (TextMessage) message;  
		                try {  
		                    System.out.println("Received message: " + tm.getText());  
		                } catch (JMSException e) {  
		                    e.printStackTrace();  
		                }  
		            }  
		        });
		        i++;
	        }while(i<=100000);   
	        
	        long endTime = System.currentTimeMillis();
	    	long diff = endTime - startTime;
	        long diffSeconds = diff / 1000;

	        System.out.print("top publishers total time: "+diffSeconds + " seconds.");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn != null){
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

猜你喜欢

转载自showlike.iteye.com/blog/2000113
今日推荐