ActiveMQ初探

ActiveMQ初探

Apache ActiveMQ是一个高性能的消息代理,实现了JMS 1.1。
目前的最新版本是5.4.1
1、下载相关包,并将activemq-all-5.4.1.jar加入到工程中。

2、运行bin目录下的actionmq.bat文件,启动broker,在conf下是相关的配置文件,例如activemq.xml

3、可通过访问 http://localhost:8161/admin/来查看启动情况,有可通过netstat -an|find "61616" 来查看端口,因为通过查看activemq.xml中即可发现传输连接器即配置的占用61616端口:
<transportConnectors>
        <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
 </transportConnectors>

4、编写消息接收类:
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  JmsReceiver  {
    
public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory 
= new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, 
"tcp://localhost:61616");
        Connection connection 
= connectionFactory.createConnection();
        connection.start();
        Session session 
= connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        
// 需要在activemq的控制台中创建queue:testqueue
        Destination destination = session.createQueue("testqueue");
        MessageConsumer consumer 
= session.createConsumer(destination);
        
        
while (true{
            TextMessage message 
= (TextMessage) consumer.receive(1000);
            
if (null != message)
                System.out.println(
"收到消息:" + message.getText());
            
else
                
break;
        }

        session.close();
        connection.close();
    }

}


5、编写消息产生类:
import  javax.jms.Connection;
import  javax.jms.ConnectionFactory;
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  JmsSender  {
    
public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory 
= new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, 
"tcp://localhost:61616");
        Connection connection 
= connectionFactory.createConnection();
        connection.start();
        Session session 
= connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        Destination destination 
= session.createQueue("testqueue");
        MessageProducer producer 
= session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        sendMsg(session, producer);
        session.commit();
        connection.close();
    }


    
public static void sendMsg(Session session, MessageProducer producer) throws JMSException {
        
// 创建一条文本消息
        TextMessage message = session.createTextMessage("你好 ActiveMQ!");
        producer.send(message);
        System.out.println(
"");
    }

}


6、通过访问 http://localhost:8161/admin/,在queue中创建testqueue,先执行消息发送类,后执行消息接收类,借看看到消息接收类的如下执行结果:
收到消息:你好 ActiveMQ!
收到消息:你好 ActiveMQ!

(执行多次消息发送类,则会接受到多条“你好 ActiveMQ!”消息)

你可能感兴趣的:(ActiveMQ初探)