下载ActiveMq:

ActiveMQ官网下载地址:http://activemq.apache.org/download.html

需要注意的是ActiveMq的版本要支持自己的jdk版本:

MQ版本号

  Build-Jdk 依赖JDK
apache-activemq-5.0.0 1.5.0_12 1.5+
apache-activemq-5.1.0 1.5.0_12 1.5+
apache-activemq-5.2.0 1.5.0_15 1.5+
apache-activemq-5.3.0 1.5.0_17 1.5+
apache-activemq-5.4.0 1.5.0_19 1.5+
apache-activemq-5.5.0 1.6.0_23 1.6+
apache-activemq-5.6.0 1.6.0_26 1.6+
apache-activemq-5.7.0 1.6.0_33 1.6+
apache-activemq-5.8.0 1.6.0_37 1.6+
apache-activemq-5.9.0 1.6.0_51 1.6+
apache-activemq-5.10.0 1.7.0_12-ea 1.7+
apache-activemq-5.11.0 1.7.0_60 1.7+
apache-activemq-5.12.0 1.7.0_80 1.7+
apache-activemq-5.13.0 1.7.0_80 1.7+
apache-activemq-5.14.0 1.7.0_80 1.7+
apache-activemq-5.15.0 1.8.0_112 1.8+

下载完了解压出来

ActiveMq入门实例

在bin下选择对应自己电脑的操作系统,点击activemq.bat运行程序;

启动ActiveMQ以后,登陆:http://localhost:8161/admin/

输入账号密码,默认都是admin,在Queues里面能看到队列情况;

创建Eclipse项目,导入我们需要的jar包,我这里为了省事直接导入了acvicema-all-5.6.0这个jar包;

创建消息发送端:

public class Sender {
    public static void main(String[] args) {
        // ConnectionFactory :连接工厂,JMS 用它创建连接
        ConnectionFactory connectionFactory;
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = null;
        // Session: 一个发送或接收消息的线程
        Session session;
        // Destination :消息的目的地;消息发送给谁.
        Destination destination;
        // MessageProducer:消息发送者
        MessageProducer producer;
        // 构造ConnectionFactory实例对象
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                ActiveMQConnection.DEFAULT_BROKER_URL
              );
        try {
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.TRUE,
                    Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("TestQueue");
            // 得到消息发送者
            producer = session.createProducer(destination);
            // 设置不持久化
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 发送消息
            producer.send(session.createTextMessage("今天是星期五"));
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }

}

创建消息接收端:

public class Receiver {  
    public static void main(String[] args) {  
        // ConnectionFactory :连接工厂,JMS 用它创建连接  
        ConnectionFactory connectionFactory;  
        // Connection :JMS 客户端到JMS Provider 的连接  
        Connection connection = null;  
        // Session: 一个发送或接收消息的线程  
        Session session;  
        // Destination :消息的目的地;消息发送给谁.  
        Destination destination;  
        // 消费者,消息接收者  
        MessageConsumer consumer;  
        connectionFactory = new ActiveMQConnectionFactory(  
                ActiveMQConnection.DEFAULT_USER,  
                ActiveMQConnection.DEFAULT_PASSWORD,  
                ActiveMQConnection.DEFAULT_BROKER_URL);  
        try {  
            // 构造从工厂得到连接对象  
            connection = connectionFactory.createConnection();  
            // 启动  
            connection.start();  
            // 获取操作连接  
            session = connection.createSession(Boolean.FALSE,  
                    Session.AUTO_ACKNOWLEDGE);  
            //设置从哪里获取消息
            destination = session.createQueue("TestQueue");
            //创建消息接受者
            consumer = session.createConsumer(destination);  
            TextMessage message =  (TextMessage)consumer.receive();
            if(message!=null){
                System.out.println("收到消息:"+message.getText());
            }
            
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (null != connection)  
                    connection.close();  
            } catch (Throwable ignore) {  
            }  
        }  
    }  

先运行发送端,再运行接收端:

ActiveMq入门实例

这样就完成了一个最基本的一个例子,仅供参考~

相关文章: