• 【ActiveMQ】ActiveMQ在Windows的安装,以及点对点的消息发送案例


    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下。

    基于练习方便需要,使用Windows的版本。

    参考的优秀文章:

    activemq的几种基本通信方式总结

    Winsdows下安装

    下载后得到zip文件,解压后用命令行启动即可使用,有两个问题需注意。

    1. 启动的文件根据不同的系统类型,也分为32位、64位,请选择自身系统的启动文件
    2. 目前的apache-activemq-5.12.0版本需要1.7的JDK运行
    D:greenapache-activemq-5.12.0inwin64activemq.bat

    启动后,可见如下类型的日志,即正常启动:

    C:UsersAdministrator>D:greenapache-activemq-5.12.0inwin64activemq.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:greenJDKjdk1.7.0_71jr
    e7
    jvm 1    |   Heap sizes: current=61440k  free=55845k  max=932352k
    jvm 1    |     JVM args: -Dactivemq.home=../.. -Dactivemq.base=../.. -Djavax.net
    .ssl.keyStorePassword=password -Djavax.net.ssl.trustStorePassword=password -Djav
    ax.net.ssl.keyStore=../../conf/broker.ks -Djavax.net.ssl.trustStore=../../conf/b
    roker.ts -Dcom.sun.management.jmxremote -Dorg.apache.activemq.UseDedicatedTaskRu
    nner=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 -Dwrapper.key=XfRgHYr
    WI1FG6qgT -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.m
    ax=31999 -Dwrapper.pid=4536 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wra
    pper -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1
    jvm 1    | Extensions classpath:
    jvm 1    |   [....lib,....libcamel,....liboptional,....libweb,....l
    ibextra]
    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@3b7
    94c60: startup date [Tue Aug 18 23:20:11 CST 2015]; root of context hierarchy
    jvm 1    |  INFO | PListStore:[D:greenapache-activemq-5.12.0inwin64....d
    atalocalhost	mp_storage] started
    jvm 1    |  INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:green
    apache-activemq-5.12.0inwin64....datakahadb]
    jvm 1    |  INFO | KahaDB is version 6
    jvm 1    |  INFO | Recovering from the journal @1:138575
    jvm 1    |  INFO | Recovery replayed 9 operations from the journal in 0.02 secon
    ds.
    jvm 1    |  INFO | Apache ActiveMQ 5.12.0 (localhost, ID:USER-20141117SF-63255-1
    439911212886-0:1) is starting
    jvm 1    |  INFO | Listening for connections at: tcp://USER-20141117SF:61616?max
    imumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1    |  INFO | Connector openwire started
    jvm 1    |  INFO | Listening for connections at: amqp://USER-20141117SF:5672?max
    imumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1    |  INFO | Connector amqp started
    jvm 1    |  INFO | Listening for connections at: stomp://USER-20141117SF:61613?m
    aximumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1    |  INFO | Connector stomp started
    jvm 1    |  INFO | Listening for connections at: mqtt://USER-20141117SF:1883?max
    imumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1    |  INFO | Connector mqtt started
    jvm 1    | {}
    jvm 1    |  INFO | Listening for connections at ws://USER-20141117SF:61614?maxim
    umConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1    |  INFO | Connector ws started
    jvm 1    |  INFO | Apache ActiveMQ 5.12.0 (localhost, ID:USER-20141117SF-63255-1
    439911212886-0:1) started
    jvm 1    |  INFO | For help or more information please see: http://activemq.apac
    he.org
    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/ap
    i/jolokia/
    jvm 1    |  INFO | Initializing Spring FrameworkServlet 'dispatcher'
    jvm 1    |  INFO | jolokia-agent: No access restrictor found at classpath:/jolok
    ia-access.xml, access to all MBeans is allowed
    View Code

    默认情况下, activemq服务监听61616端口,如此端口被占用,会报错,请释放后再试。

    控制台

    http://127.0.0.1:8161/admin/

    默认账户/密码:admin/admin

    基于JMS的点对点队列,实现异步队列发送短信

    在点对点模式(P2P或point to point)中,消息发送方称为生产者(Producer),消息接收方称为消费者(Consumer)。而在发布订阅模式中,则分别为发布者、订阅者。

    系统需要给客户发送短信,一般来说,我们都是通过调用3大运营商的短信接口进行发送短信的。

    而在某些时候,比如春节,发送短信的人数特别多,所以接口变得堵塞,发送短信需时较久。我们不希望因发送短信堵塞应用程序,因此,我们想把发送短信的部分放到MQ,从而实现异步发送短信。(当然,在这种情况下,我们不关心发送短信是否成功。即,即使发送短信失败,在系统的界面并不提示用户)。

    以下只是学习例子,并不适应实际案例。

    消费者持续连接MQ服务器,接收消息,并实现发送短信的业务

    package com.nicchagil.activemq.study.No002队列异步发送短信案例;
    
    import java.util.HashMap;
    import java.util.concurrent.TimeUnit;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageConsumer;
    import javax.jms.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class SMSConsumer {
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, 
                    "tcp://127.0.0.1:61616");
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("SendSMS");
            MessageConsumer consumer = session.createConsumer(destination);
            
            while (true) {
                ObjectMessage message = (ObjectMessage)consumer.receive();
                if (message != null) {
                    HashMap<String, String> messageMap = (HashMap<String, String>)message.getObject();
                    sendSMS(messageMap.get("mobile"), messageMap.get("message"));
                }
            }
        }
        
        public static void sendSMS(String mobile, String message) {
            System.out.println("Send SMS to : " + mobile + ", content : " + message);
            
            /* 模拟在繁忙时间发送短信需时15秒 */
            try {
                TimeUnit.SECONDS.sleep(15);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("Done");
        }
    
    }
    View Code

    两个生产者,用于发送消息,调用消费者发送短信。两个生产者意在测试,他们的不同点仅在于短信文本。

    package com.nicchagil.activemq.study.No002队列异步发送短信案例;
    
    
    
    import java.util.HashMap;
    
    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.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    
    public class SMSProducer1 {
        
        public static void main(String[] args) throws JMSException {
            /* 连接 */
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, 
                    ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
            Connection connection = factory.createConnection();
            connection.start();
            
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 会话
            Destination destination = session.createQueue("SendSMS"); // 队列
            
            /* 生产者 */
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            
            /* 发送消息 */
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("mobile", "13000000000");
            map.put("message", "2016,新年快乐!");
            ObjectMessage message = session.createObjectMessage(map);
            producer.send(message);
            session.commit();
            System.out.println("sent...");
        }
    
    }
    View Code
    package com.nicchagil.activemq.study.No002队列异步发送短信案例;
    
    
    
    import java.util.HashMap;
    
    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.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    
    public class SMSProducer2 {
        
        public static void main(String[] args) throws JMSException {
            /* 连接 */
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, 
                    ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
            Connection connection = factory.createConnection();
            connection.start();
            
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 会话
            Destination destination = session.createQueue("SendSMS"); // 队列
            
            /* 生产者 */
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            
            /* 发送消息 */
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("mobile", "13000000000");
            map.put("message", "猴年快乐哦!");
            ObjectMessage message = session.createObjectMessage(map);
            producer.send(message);
            session.commit();
            System.out.println("sent...");
        }
    
    }
    View Code

    运行后消费者的日志(Main方法运行顺序:SMSConsumer > SMSProducer1 > SMSProducer2)

    Send SMS to : 13000000000, content : 2016,新年快乐!
    Done
    Send SMS to : 13000000000, content : 猴年快乐哦!
    Done
    View Code
  • 相关阅读:
    selenium+allure测试报告添加测试截图
    selenium pytest_html测试报告添加测试步骤截图
    pytest单元测试框架fixture应用
    unittest单元测试框架教程7-unittest.TestSuite类详解
    unittest单元测试框架教程6-unittest.TestCase类详解
    unittest单元测试框架教程5-使用subTest进行循环测试
    unittest单元测试框架教程3-利用unittest测试原理组织测试套件和用例
    unittest单元测试框架教程2-通过TestLoader运行用例
    Python学习相关链接
    GO语言相关的链接整理
  • 原文地址:https://www.cnblogs.com/nick-huang/p/4741023.html
Copyright © 2020-2023  润新知