activeMQ 例子(一) 简单的p2p(point to point)模式

   从 http://activemq.apache.org/download.html下载MQ的最新版本

1. 点对点消息传递

        通过点对点的消息传递模型,一个应用程序可以向另外一个应用程序发送消息。在此传递模型中,目标类型是队列(Queue)。消息首先被传送至队列目标,然后从该队列中将消息传送至对此队列进行监听的某个消费者。

如下图:

                                                                             

 

        一个队列可以关联多个队列发送方和接收方,但一条消息仅传递给一个接收方。如果多个接收方正在监听队列上的消息,JMS Provider将根据“先来者优先”的原则确定由哪个价售房接受下一条消息。如果没有接收方在监听队列,消息将保留在队列中,直至接收方连接到队列为止。这种消息传递模型是传统意义上的拉模型或轮询模型。在此列模型中,消息不时自动推动给客户端的,而是要由客户端从队列中请求获得

2.源码附上(其实在activeMQ的下载包中,已经有很全的例子了)

生产者:

package com.liujx.activemq;

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

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

public class TestProducer {
	private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String USERNAMW=ActiveMQConnection.DEFAULT_USER;
	private static final String URL=ActiveMQConnection.DEFAULT_BROKER_URL;
	private static Connection connection = null;
	private static Session session = null;
	private static MessageProducer messageproducer = null;
	static{
		ConnectionFactory amf = new ActiveMQConnectionFactory(TestProducer.USERNAMW,TestProducer.PASSWORD,TestProducer.URL);
		try {
			connection = amf.createConnection();
			connection.start();
			session = connection.createSession(true,  Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue("liujxTest");
			messageproducer = session.createProducer(destination);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendMessage(){
		try {
			messageproducer.send(session.createTextMessage("aaaaaaaaaa"));
			session.commit();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		sendMessage();
	}

}
消费者;

package com.liujx.activemq;

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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class TestConsumer {
	private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String USERNAMW=ActiveMQConnection.DEFAULT_USER;
	private static final String URL=ActiveMQConnection.DEFAULT_BROKER_URL;
	private static ConnectionFactory connectionFactory = null;
	private static Connection connection = null;
	private static Session session = null;
	private static MessageConsumer messageConsumer = null;
	static{
		connectionFactory = new ActiveMQConnectionFactory(USERNAMW,PASSWORD, URL);
		try {
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue("HelloWorld");
			messageConsumer = session.createConsumer(destination);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public  void  getMessages(){
		while(true){
			try {
				TextMessage textMessage = (TextMessage) messageConsumer.receive(10000);
				if(textMessage != null){
					System.out.println(textMessage.getText());
				}else{
					break;
				}
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		new TestConsumer().getMessages();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值