ActiveMQ学习(一)--入门 - 高飞网
1973 人阅读

ActiveMQ学习(一)--入门

2017-07-28 02:09:46

下载

    到http://activemq.apache.org/ 下载最新的ActiveMQ版本,本次使用的是 “Apache ActiveMQ 5.13.2 Released”。下载以后的文件是:apache-activemq-5.13.2-bin.zip

项目目录:


启动ActiveMQ

    由于我本机是64位操作系统,进入到目录:apache-activemq-5.13.2\bin\win64下面,找到activemq.bat运行(尝试bin目录下的activemq.bat不好使)。运行结果如下:

wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    |
jvm 1    | Java Runtime: Oracle Corporation 1.7.0_71 D:\software\Java\jdk1.7.0_71\jre
jvm 1    |   Heap sizes: current=124928k  free=116928k  max=932352k
jvm 1    |     JVM args: -Dactivemq.home=../.. -Dactivemq.base=../.. -Djavax.net.ssl.keyStorePassword=password -Djavax.net.ssl.trustStorePassword=password -Djavax.net.ssl.keyStore=.
./../conf/broker.ks -Djavax.net.ssl.trustStore=../../conf/broker.ts -Dcom.sun.management.jmxremote -Dorg.apache.activemq.UseDedicatedTaskRunner=true -Djava.util.logging.config.file=
logging.properties -Dactivemq.conf=../../conf -Dactivemq.data=../../data -Djava.security.auth.login.config=../../conf/login.config -Xmx1024m -Djava.library.path=../../bin/win64 -Dwr
apper.key=cRx6tOZ1wuFlQKtE -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=9936 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wrapper
 -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1
jvm 1    | Extensions classpath:
jvm 1    |   [..\..\lib,..\..\lib\camel,..\..\lib\optional,..\..\lib\web,..\..\lib\extra]
jvm 1    | ACTIVEMQ_HOME: ..\..
jvm 1    | ACTIVEMQ_BASE: ..\..
jvm 1    | ACTIVEMQ_CONF: ..\..\conf
jvm 1    | ACTIVEMQ_DATA: ..\..\data
jvm 1    | Loading message broker from: xbean:activemq.xml
jvm 1    |  INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@7bb6aea4: startup date [Thu Apr 14 10:29:49 CST 2016]; root of context hierarchy
jvm 1    |  INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\software\apache-activemq-5.13.2\bin\win64\..\..\data\kahadb]
jvm 1    |  INFO | PListStore:[D:\software\apache-activemq-5.13.2\bin\win64\..\..\data\localhost\tmp_storage] started
jvm 1    |  INFO | Apache ActiveMQ 5.13.2 (localhost, ID:xuyh-PC-54672-1460600991153-0:1) is starting
jvm 1    |  INFO | Listening for connections at: tcp://xuyh-PC:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector openwire started
jvm 1    |  INFO | Listening for connections at: amqp://xuyh-PC:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector amqp started
jvm 1    |  INFO | Listening for connections at: stomp://xuyh-PC:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector stomp started
jvm 1    |  INFO | Listening for connections at: mqtt://xuyh-PC:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector mqtt started
jvm 1    |  WARN | ServletContext@o.e.j.s.ServletContextHandler@3c175667{/,null,STARTING} has uncovered http methods for path: /
jvm 1    |  INFO | Listening for connections at ws://xuyh-PC:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector ws started
jvm 1    |  INFO | Apache ActiveMQ 5.13.2 (localhost, ID:xuyh-PC-54672-1460600991153-0:1) started
jvm 1    |  INFO | For help or more information please see: http://activemq.apache.org
jvm 1    |  WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: D:\software\apache-activemq-5.13.2\bin\win64\..\..\data\kahadb only has 8192 mb of usa
ble space. - resetting to maximum available disk space: 8192 mb
jvm 1    |  WARN | Temporary Store limit is 51200 mb (current store usage is 0 mb). The data directory: D:\software\apache-activemq-5.13.2\bin\win64\..\..\data only has 8192 mb of u
sable space. - resetting to maximum available disk space: 8192 mb
jvm 1    |  INFO | No Spring WebApplicationInitializer types detected on classpath
jvm 1    |  INFO | ActiveMQ WebConsole available at http://0.0.0.0:8161/
jvm 1    |  INFO | ActiveMQ Jolokia REST API available at http://0.0.0.0:8161/api/jolokia/
jvm 1    |  INFO | Initializing Spring FrameworkServlet 'dispatcher'
jvm 1    |  INFO | No Spring WebApplicationInitializer types detected on classpath
jvm 1    |  INFO | jolokia-agent: Using policy access restrictor classpath:/jolokia-access.xml
jvm 1    |  INFO | Connector vm://localhost started

    启动以后,即可以在浏览器输入 http://127.0.0.1:8161/admin/ 默认的用户密码是admin/admin。进入系统,如下:

点到“Queue”标签,创建一个Queue。这里创建一个名为HelloQueue的队列。


HelloWorld代码

首选是发送方:Sender.java

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 Sender {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;// 用以创建连接的工厂
        Connection connection = null;// jsm客户端到jms Provider的连接
        Session session;// 一个发送或接收消息的线程

        MessageProducer producer;// 消息发送者
        Destination destination;// 消息的目的地;

        // 创建工厂
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");

        try {
            connection = connectionFactory.createConnection();// 创建连接对象
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

            destination = session.createQueue("HelloQueue");
            producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            for (int i = 0; i < 100; i++) {
                // 发送消息
                TextMessage textMessage = session.createTextMessage("第一条消息" + i);
                producer.send(textMessage);
                System.out.println("发送第一条消息." + i);
                session.commit();
            }

        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

其次是接收方代码:Receiver.java

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 Receiver {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session;
        Destination destination;
        MessageConsumer consumer;

        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
        
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("HelloQueue");
            consumer = session.createConsumer(destination);
            while (true) {
                //设置接收者接收消息的时间,为了便于测试,这里谁定为100s
                /*
                 * 如果 timeout==-1,直到消息到达都会阻塞 测试结果,-1收不到消息,不阻塞
                 * 如果 timeout==0 ,将尝试不阻塞,如果有可用消息就返回消息 0在消息到来时即可收到消息,不阻塞
                 * 如果timeout>0  ,将会阻塞直到指定的时间 
                 * */
                TextMessage message = (TextMessage) consumer.receive(0);
                if (null != message) {
                    System.out.println("收到消息" + message.getText());
                } 
            }
            
        } catch (JMSException e) {
            e.printStackTrace();
        }
        
        
    }
}

分别运行Sender和Receiver,可以在控制台中观察到消息的发送和接收

在管理页面也可以观察到:

以上就是一个简单的ActiveMQ消息队列的入门使用。


还没有评论!
23.20.120.3