activemq示例

这几天都在折腾activemq 这个东西,都有点吐了。写个总结吧。

下面这个是网友的示例,我感觉挺好的,很是实用,

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方法,当接受到消息的时候会自动调用这个函数对消息进行处理。


import javax.jms.JMSException;   
  
public class Test {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) throws JMSException, Exception {   
        // TODO Auto-generated method stub   
        ConsumerTool consumer = new ConsumerTool();   
        ProducerTool producer = new ProducerTool();   
        // 开始监听   
        consumer.consumeMessage();   
           
        // 延时500毫秒之后发送消息   
        Thread.sleep(500);   
        producer.produceMessage("Hello, world!");   
        producer.close();   
           
        // 延时500毫秒之后停止接受消息   
        Thread.sleep(500);   
        consumer.close();   
    }   
}   


这是网友的,这个确实挺好的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值