这里通过一个入门的案例来体会一下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 | [email protected]@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---->ActiveMQ的使用(一)-LMLPHP

二、我们通过一个实例来感受它的使用

在这里不会过多的介绍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的管理平台中,也可以看到如下:

activeMQ----&gt;ActiveMQ的使用(一)-LMLPHP

当然在这个平台上,我们也可以手动的产生并发送信息。

友情链接

05-10 23:20