ActiveMQ

最近学习了一段时间的ActiveMQ,apache的强劲的消息总线服务。学习过程参考了ActiveMQ in Action和whitesock的javaeye博客。使用消息中间件来进行消息传递的原理如下图

 

 

与大家分享2个最简单的消息通信的例子。生产者和消费者,发布者和订阅者

生产者

package cn.adcc.activemq.point2point;

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

public class TestProducer {
	private String user = ActiveMQConnection.DEFAULT_USER;
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
    private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private boolean transacted;//默认为false
    private String subject = "TOOL.DEFAULT";
    private Destination destination;//消息目的地
    private boolean persistent = true;
    
	public static void main(String[] args) throws JMSException {
		new TestProducer().run();
	}
	
	public void run() throws JMSException{
		Connection connection = null;
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        connection = connectionFactory.createConnection();
        connection.start();
        
        // Create the session
        Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
        destination = session.createQueue(subject);
        
        // Create the producer.
        MessageProducer producer = session.createProducer(destination);
              
        TextMessage message = session.createTextMessage("hello");

        producer.send(message);
        System.exit(0);
	}
}

 

消费者

package cn.adcc.activemq.point2point;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

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

public class TestConsumer implements MessageListener, ExceptionListener {
	 private String user = ActiveMQConnection.DEFAULT_USER;
	 private String password = ActiveMQConnection.DEFAULT_PASSWORD;
	 private String url = ActiveMQConnection.DEFAULT_BROKER_URL; 
		 //ActiveMQConnection.DEFAULT_BROKER_URL;
	 private Session session;
	 private boolean transacted;
	 private int ackMode = Session.AUTO_ACKNOWLEDGE;
	 
	 private String subject = "TOOL.DEFAULT"; 
		 //"TOOL.DEFAULT";//主题
	 private Destination destination = null;
	 private MessageProducer replyProducer;
	 private boolean persistent = true;
	 
	//回调方法
	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				
				TextMessage txtMsg = (TextMessage) message;
				String msg = "";

				msg = txtMsg.getText();

				System.out.println("Received: " + msg);
			}
		} catch (JMSException e) {
			e.printStackTrace();
		} 
	}

	public static void main(String[] args) {
		new TestConsumer().run();
	}
	
	public void run(){
		 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
         try {
			Connection connection = connectionFactory.createConnection();
			connection.setExceptionListener(this);
            connection.start();
            
            session = connection.createSession(transacted, ackMode);
            destination = session.createQueue(subject);
                        
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(this);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
	}

	@Override
	public void onException(JMSException arg0) {
		// TODO Auto-generated method stub
		
	}

}

 

发布者

package cn.adcc.activemq.publisher.mypackage;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;

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

public class TopicPublisher {
	private String url = "tcp://localhost:61616";
	private String user = ActiveMQConnection.DEFAULT_USER;
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private String topicName = "testtopic";
	
	private void run() throws JMSException{
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user,password,url);
		Connection connection = factory.createConnection();
		connection.start();
		
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		Topic topic = session.createTopic(topicName);
		MessageProducer producer = session.createProducer(topic);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		Message message = session.createTextMessage("hello world1!");
		producer.send(message);
		System.exit(0);
	}
	
	public static void main(String[] args) throws JMSException{
		new TopicPublisher().run();
	}
	
}

 

订阅者

package cn.adcc.activemq.publisher.mypackage;

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

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.CommandTypes;
import org.apache.activemq.command.ConsumerId;
import org.apache.activemq.command.ConsumerInfo;
import org.apache.activemq.command.DataStructure;
import org.apache.activemq.command.RemoveInfo;

public class TopicSubsribe implements MessageListener{
	private String url = "tcp://localhost:61616";
	private String user = ActiveMQConnection.DEFAULT_USER;
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private String topicName = "testtopic";
	
	public static void main(String[] args) throws JMSException{
		new TopicSubsribe().run();
	}
	
	private void run() throws JMSException{
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user,password,url);
		Connection connection = factory.createConnection();
		connection.start();
		
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createTopic(topicName);
	
		MessageConsumer consumer = session.createConsumer(destination);
		
		consumer.setMessageListener(this);
	}
	
	@Override
	public void onMessage(Message message) {
		if (message instanceof TextMessage) {
			
			TextMessage txtMsg = (TextMessage) message;
			String msg = "";

			try {
				msg = txtMsg.getText();
				System.out.println("Received: " + msg);
			} catch (JMSException e) {
				e.printStackTrace();
			} 
			
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值