activeMQ---->ActiveMQ的使用(一)


  這里通過一個入門的案例來體會一下ActiveMQ的作用以及使用方法。你要做一個不動聲色的大人了。不准情緒化,不准偷偷想念,不准回頭看。去過自己另外的生活。你要聽話,不是所有的魚都會生活在同一片海里。

 

ActiveMQ的案例

一、window上的ActiveMQ下載及安裝

下載地址: http://activemq.apache.org/download.html

下載的zip包,解壓就可以直接使用。進入D:\Apache\apache-activemq\apache-activemq-5.15.0\bin\win64。運行activemq就可以看到如下輸出

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.8.0_152 D:\Java\jdk\jre1.8
jvm 1    |   Heap sizes: current=188416k  free=175635k  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 -Dwrapper.key=yyWSlZPI4Cs9bJP5 -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=16960 -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@715c57f8: startup date [Fri Nov 03 15:13:52 CST 2017]; root of context hierarchy
jvm 1    |  INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\Apache\apache-activemq\apache-activemq-5.15.0\bin\win64\..\..\data\kahadb]
jvm 1    |  INFO | KahaDB is version 6
jvm 1    |  INFO | Recovering from the journal @1:45959
jvm 1    |  INFO | Recovery replayed 1030 operations from the journal in 0.063 seconds.
jvm 1    |  INFO | PListStore:[D:\Apache\apache-activemq\apache-activemq-5.15.0\bin\win64\..\..\data\localhost\tmp_storage] started
jvm 1    |  INFO | Apache ActiveMQ 5.15.0 (localhost, ID:Linux-61868-1509693233765-0:1) is starting
jvm 1    |  INFO | Listening for connections at: tcp://Linux:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector openwire started
jvm 1    |  INFO | Listening for connections at: amqp://Linux:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector amqp started
jvm 1    |  INFO | Listening for connections at: stomp://Linux:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector stomp started
jvm 1    |  INFO | Listening for connections at: mqtt://Linux:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector mqtt started
jvm 1    |  WARN | ServletContext@o.e.j.s.ServletContextHandler@3f6545d8{/,null,STARTING} has uncovered http methods for path: /
jvm 1    |  INFO | Listening for connections at ws://Linux:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector ws started
jvm 1    |  INFO | Apache ActiveMQ 5.15.0 (localhost, ID:Linux-61868-1509693233765-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:\Apache\apache-activemq\apache-activemq-5.15.0\bin\win64\..\..\data\kahadb only has 43466 mb of usable space. - resetting to maximum available disk space: 43466 mb
jvm 1    |  WARN | Temporary Store limit is 51200 mb (current store usage is 0 mb). The data directory: D:\Apache\apache-activemq\apache-activemq-5.15.0\bin\win64\..\..\data only has 43465 mb of usable space. - resetting to maximum available disk space: 43465 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
activeMQ的啟動

在瀏覽器輸入http://127.0.0.1:8161/admin/,用戶名和密碼為admin。即可看到activeMQ的管理頁面。

 

 二、我們通過一個實例來感受它的使用

 在這里不會過多的介紹ActiveMQ的一些知識,我們以基於JMS的點對點隊列,實現異步隊列發送信息為案例。項目的maven依賴

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>${activemq-version}</version>
</dependency>
  • 定義一個消息的生產者
package com.linux.huhx.firstdemo;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author: huhx
 * @Date: 2017-11-03 上午 11:38
 */
public class HuhxProducer {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 會話
        Destination destination = session.createQueue("HuhxSend"); // 隊列

        /* 生產者 */
        MessageProducer messageProducer = session.createProducer(destination);
        messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        /* 發送消息 */
        MailMessage mailMessage = new MailMessage("huhx", "linux", "Welcome to ActiveMQ!");
        ObjectMessage message = session.createObjectMessage(mailMessage);
        messageProducer.send(message);
        session.commit();
        System.out.println("senting...");
    }
}
  • 定義一個消息的消費者
package com.linux.huhx.firstdemo;


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: huhx
 * @Date: 2017-11-03 上午 11:24
 */
public class HuhxConsumer {
    public static void main(String[] args) throws JMSException, InterruptedException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        // JMS clients use the ConnectionFactory object to create connections to a JMS provider.
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 會話
        Destination destination = session.createQueue("HuhxSend"); // 隊列

        MessageConsumer consumer = session.createConsumer(destination);
        while (true) {
            ObjectMessage message = (ObjectMessage) consumer.receive();
            if (message != null) {
                MailMessage mailMessage = (MailMessage) message.getObject();
                System.out.println("send message " + mailMessage);
                TimeUnit.SECONDS.sleep(2);
                System.out.println("End");
            }
        }
    }
}
  • 一個消息的實體類,需要實現Serializable類
package com.linux.huhx.firstdemo;

import java.io.Serializable;

/**
 * @Author: huhx
 * @Date: 2017-11-03 上午 11:41
 */
public class MailMessage implements Serializable {
    private String userForm;
    private String message;
    private String userTo;

    public MailMessage(String userForm, String message, String userTo) {
        this.userForm = userForm;
        this.message = message;
        this.userTo = userTo;
    }

    public String getUserForm() {
        return userForm;
    }

    public String getMessage() {
        return message;
    }

    public String getUserTo() {
        return userTo;
    }

    @Override
    public String toString() {
        return "from " + userForm + ", to " + userTo + ", message " + message;
    }
}

 

三、運行測試的結果

運行main方法(HuhxConsumer --> HuhxProducer),在HuhxConsumer的控制台打印:

send message from huhx, to Welcome to ActiveMQ!, message linux
End

我們在activeMQ的管理平台中,也可以看到如下:

當然在這個平台上,我們也可以手動的產生並發送信息。

 

友情鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM