ActiveMQ-如何使用JMS API?

JMS编程模型

JMS定义了Java中访问消息中间件的一组接口,主要包括ConnectionFactory、Connection、Session、Destination、MessageProducer、MessageConsumer、Message等,这些接口的具体实现由具体的消息中间件完成。以ActiveMQ为例,类javax.jms.Connection定义了接口,具体实现类为org.apache.activemq.ActiveMQConnection。在JMS编程模型中,JMS客户端使用ConnectionFactory对象创建一个Connection,消息服务发送及接收消息都通过该连接进行,创建Connection时需要验证客户端身份同时分配通信资源,大部分客户端只用一个Connection来进行所有的消息传送。既然只有一个Connection,怎么创建多个队列呢?Connection用于创建会话Session,Session是一个用于生成和使用消息的单线程上下文,Session用于创建Producer、Consumer、Destination、TextMessage等对象,其它的属性在具体的对象方法内设置。

编程实例

Producer

package com.activemqtest.client;

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

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConnectionFactory connectionFactory;
		Connection connection = null;
		Session session;
		Destination destination;
		MessageProducer messageProducer;
		
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD,
				"tcp://172.16.1.15:20001");
		
		try {
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);   // 第一个True设置是否支持事务性  第二个参数用于设置回执模式
			destination = session.createQueue("Peter_Test_Queue1");
			messageProducer = session.createProducer(destination);
			messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 设置是否持久化消息
			sendMessage(session, messageProducer);
			session.commit();  // 事务性提交
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != connection){
					connection.close();  // 关闭连接
				}
			} catch (Throwable ignore) {
				
			}
		}

	}
	
	public static void sendMessage(Session session, MessageProducer producer)throws Exception {
		for (int i = 1; i <= 10; i++){
			TextMessage message = session.createTextMessage("ActiveMQ 发送的消息" + i);  // 创建消息
			System.out.println("发送消息: "+ "ActiveMQ发送的消息" + i);
			producer.send(message); // 发送消息
		}
	}

}

Consumer  

package com.activemqtest.client;

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

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConnectionFactory connectionFactory;
		Connection connection = null;
		Session session;
		Destination destination;
		MessageConsumer consumer;
		
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD,
				"tcp://172.16.1.15:20001"
				);
		
		try {
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue("Peter_Test_Queue");
			consumer = session.createConsumer(destination);
			while(true){
				TextMessage message = (TextMessage)consumer.receive(100);
				if (null != message){
					System.out.println("收到消息==="+message.getText());
				}else{
					break;
				}
			} 
				
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				try {
					if(null != connection){
						connection.close();
					}
				} catch (Throwable ignore){
					
				}
			}
		}
	}

  

 

posted @ 2017-12-26 16:47  Peterer~王勇  阅读(713)  评论(0编辑  收藏  举报