activeMQ 消息发送与接收(一)

一、默认已建项目、导包
(包在activemq解压目录/lib 及/lib/optional/目录下),
如下图(有几个包暂时不需要,不过后续代码中需使用):



二、点对点模式
2.1、发送消息
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;

/**
 * 点对点模式--消息发送、
 * 单连接、消息不持久化、无事务、消息自动确认
 * @author showlike
 * @4:46:25 PM @Jan 6, 2014
 */
public class Sender {
	
	public static void main(String args[]){
		//连接工厂,JMS用它创建连接
		ConnectionFactory connFactory; 
		//Provider 的连接
		Connection conn = null;
		//一个发送或接收消息的线程
		Session session ; 
		//消息的目的地;消息发送给谁
		Destination destination;
		MessageProducer producer;	//消息发送者
		//构造ConnectionFactory 实例对象,此处采用ActiveMQ的实现JAR
		connFactory = new ActiveMQConnectionFactory("admin","showlike","tcp://0.0.0.0:61616");
		try{
			//从连接工厂创建连接
			conn = connFactory.createConnection();
			conn.start();	//启动
			session = conn.createSession(Boolean.FALSE, 
					Session.AUTO_ACKNOWLEDGE);
			//创建
			destination = session.createQueue("FirstQueue");
			//得到消息生成者【发送者】
			producer = session.createProducer(destination);
			//设置不持久化,此处学习,实际根据项目决定
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			// 构造消息,此处写死,项目就是参数,或者方法获取  
            sendMessage(session, producer);  
//            session.commit();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
    public static void sendMessage(Session session, MessageProducer producer)  
    													throws Exception {
    	long startTime = System.currentTimeMillis();
    	for (int i = 1; i <= 1000000; i++) {  
    		TextMessage message = session.createTextMessage(
    				"ActiveMq 发送的消息" + i);  
    		// 发送消息到目的地方  
    		System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);  
    		producer.send(message);
//    		Thread.sleep(100);
    	}	
    	
    	long endTime = System.currentTimeMillis();
    	long diff = endTime - startTime;
        long diffSeconds = diff / 1000;

        System.out.print("sender use time: "+diffSeconds + " seconds.");
	}  
}


2.2、接收消息
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 点对点模式--消息接收
 * 单连接、消息不持久化、无事务、消息自动确认接收
 * @author showlike
 * @4:42:25 PM @Jan 6, 2014
 */
public class Receiver {
	
	public static void main(String[] args) {
		ConnectionFactory connFactory;
		Connection conn = null;
		Session session = null;
		Destination destination;
		MessageConsumer consumer;
		
		try{
			connFactory = new ActiveMQConnectionFactory("admin","showlike","tcp://0.0.0.0:61616");
			
			conn = connFactory.createConnection();
			conn.start();
			session = conn.createSession(Boolean.FALSE, //不开启事务
					Session.AUTO_ACKNOWLEDGE);			//消息自动确认
			destination = session.createQueue("FirstQueue");
			consumer = session.createConsumer(destination);
			
			long startTime = System.currentTimeMillis();
			int i = 1;
			do{
				TextMessage message = (TextMessage)consumer.receive(100000);
				if(message != null){
					System.out.println(i+" , 收到消息为: "+message.getText());
					i++;
				}else{
					System.out.println(i+" , 收到消息为null..... ");
				}
			}while(i<=1000000);
			
			long endTime = System.currentTimeMillis();
	    	long diff = endTime - startTime;
	        long diffSeconds = diff / 1000;

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

猜你喜欢

转载自showlike.iteye.com/blog/2000111