当前位置:编程学习 > JAVA >>

ActiveMQ接收消息+发送消息的简单实例

ActiveMQ的一个简单实例-ActiveMQ接收+发送消息   JMS消息框架——ActiveMQ
最近由于公司项目需要 -ActiveMQ接收+发送消息,用的是ActiveMQ。由于这方面网上的例子不是很多,而且有的也不完整。于是经过几天的摸索学习,在网上找到了合适的方案。
 
我的 IT技术资源库   http://itlib.tk/
ProducerTool.java用于发送消息:
 
java 代码
package homework;   
  
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 ProducerTool {   
  
    private String user = ActiveMQConnection.DEFAULT_USER;   
  
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;   
  
    private String url = ActiveMQConnection.DEFAULT_BROKER_URL;   
  
    private String subject = "TOOL.DEFAULT";   
  
    private Destination destination = null;   
  
    private Connection connection = null;   
  
    private Session session = null;   
  
    private MessageProducer producer = null;   
  
    // 初始化   
    private void initialize() throws JMSException, Exception {   
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(   
                user, password, url);   
        connection = connectionFactory.createConnection();   
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);   
        destination = session.createQueue(subject);   
        producer = session.createProducer(destination);   
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);   
    }   
  
    // 发送消息  
    public void produceMessage(String message) throws JMSException, Exception {   
        initialize();   
        TextMessage msg = session.createTextMessage(message);   
        connection.start();   
        System.out.println("Producer:->Sending message: " + message);   
        producer.send(msg);   
        System.out.println("Producer:->Message sent complete!");   
    }   
  
    // 关闭连接  
    public void close() throws JMSException {   
        System.out.println("Producer:->Closing connection");   
        if (producer != null)   
            producer.close();   
        if (session != null)   
            session.close();   
        if (connection != null)   
            connection.close();   
    }   
}   
 
 
ConsumerTool.java用于接受消息,我用的是基于消息监听的机制,需要实现MessageListener接口,这个接口有个onMessage方法,当接受到消息的时候会自动调用这个函数对消息进行处理。
 
java 代码
package homework;   
  
import javax.jms.Connection;   
import javax.jms.Destination;   
import javax.jms.JMSException;   
import javax.jms.MessageConsumer;   
import javax.jms.Session;   
import javax.jms.MessageListener;   
import javax.jms.Message;   
import javax.jms.TextMessage;   
  
import org.apache.activemq.ActiveMQConnection;   
import org.apache.activemq.ActiveMQConnectionFactory;   
  
public class ConsumerTool implements MessageListener {   
  
    private String user = ActiveMQConnection.DEFAULT_USER;   
  
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;   
  
    private String url = ActiveMQConnection.DEFAULT_BROKER_URL;   
  
    private String subject = "TOOL.DEFAULT";   
  
    private Destination destination = null;   
  
    private Connection connection = null;   
  
    private Session session = null;   
  
    private MessageConsumer consumer = null;   
  
    // 初始化  
    private void initialize() throws JMSException, Exception {   
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(   
                user, password, url);   
        connection = connectionFactory.createConnection();   
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);   
        destination = session.createQueue(subject);   
        consumer = session.createConsumer(destination);   
           
    }   
  
    // 消费消息   
    public void consumeMessage() throws JMSException, Exception {   
        initialize();   
        connection.start();   
           
        System.out.println("Consumer:->Begin listening...");   
        // 开始监听  
        consumer.setMessageListener(this);   
   
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,