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();
			} 
			
		}
	}

}

 

你可能感兴趣的:(activemq,jms)