这里通过一个入门的案例来体会一下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的管理平台中,也可以看到如下:

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

友情链接

activeMQ---->ActiveMQ的使用(一)的更多相关文章

  1. ActiveMQ——activemq的安装详情,修改密码

    1.安装 下载 http://activemq.apache.org/download-archives.html, [推荐]ActiveMQ 5.13.4 Release与jdk1.7搭配(其它版本 ...

  2. Springboot+ActiveMQ(ActiveMQ消息持久化,保证JMS的可靠性,消费者幂等性)

    ActiveMQ 持久化设置: 在redis中提供了两种持久化机制:RDB和AOF 两种持久化方式,避免redis宕机以后,能数据恢复,所以持久化的功能 对高可用程序来说 很重要. 同样在Active ...

  3. ActiveMQ——activemq的报错见解javax.jms.JMSException: Software caused connection abort: recv failed

    activeMQ出现javax.jms.JMSException: Software caused connection abort: recv failed的问题解决 一直找不到原因,原来是在本地的 ...

  4. ActiveMQ——activemq的使用java代码实例(精选)

    ActiveMQ 在java中的使用,通过单例模式.工厂实现 Jms规范里的两种message传输方式Topic和Queue,两者的对比如下表():   Topic Queue 概要 Publish  ...

  5. ActiveMQ——activemq的详细说明,queue、topic的区别(精选)

    JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subscribe,topic).主要区别就是是否能重复消费. 点对点:Queue,不可重 ...

  6. [ActiveMQ]初识ActiveMQ

    初识ActiveMQ ActiveMQ介绍 官方网站:http://activemq.apache.org/ 最新版本:ActiveMQ 5.14.1(2016-10-28) 最新版本下载链接:htt ...

  7. C++ activemq CMS 学习笔记.

    很早前就仓促的接触过activemq,但当时太赶时间.后面发现activemq 需要了解的东西实在是太多了. 关于activemq 一直想起一遍文章.但也一直缺少自己的见解.或许是网上这些文章太多了. ...

  8. Activemq Jolokia

    打开JMX <broker … useJmx="true"> … <managementContext> <managementContext cre ...

  9. ActiveMQ;RabbitMQ;ZeroMQ

    中间件类型: Embedded middleware: As the name suggests, this typeof middleware handles embedded applicatio ...

  10. Activemq 平台搭建与C#示列

    ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS ...

随机推荐

  1. unity-------------UI的界面调节

    Rect Transform 我们都知道,Unity3D中所有的GameObject都必须要携带一个Transform组件,且该组件无法移除,那么作为UI显示的GameObject则不是携带Trans ...

  2. ISAPI和CGI限制中没有ASP.NET v4.0 ; vS2013检测到在集成的托管管道模式下不适用的 ASP.NET 设置。

    统确实自带了ASP.NET v4.0,但是ISAPI中没有这个选项,导致服务器开不起来 解决方法如下: 1.确保安装IIS时确实安装了ASP.NET,如果没有的话,勾上重新装一下,一般出现404.2时 ...

  3. C#页面前台<%%><%#%><%=%>

    ASP.net前台绑定用的最多,今天小小总结一下. 1:<%#Eval("")%> 2:<%#Bind("")%> 3:<%=变量 ...

  4. C# IP地址与数字之间的互转

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Te ...

  5. memcached监控工具

    最简单和最直接的方式是在启动memcached的时候加入-vv参数,从而在控制台打印每次客户端的请求和相应,这非常适合开发.另外一种较为直接的方式是通过telnet进行查看,例如:若server为本机 ...

  6. css 设置背景图片铺满固定不动

    #page{ position: relative; width: 100%; height: 100%; background-image:url(../img/bg.JPG); backgroun ...

  7. Blender 界面操作

    1.旋转场景 使用鼠标中间键旋转整个场景.按住Shift键,再操作鼠标中间键则可平移整个场景. 2.数字键盘功能 数字键盘1.3.7,分别控制场景向前.向后.向上显示. 数字键盘5,可以在正射投影(O ...

  8. windows下caffe安装配置、matlab接口

    一.CommonSettings.props caffe下载后解压.源代码文件夹caffe-master,到该文件夹下的windows文件夹下,将CommonSettings.props.exampl ...

  9. 跨域、跨服务器调用时候session丢失的问题

    最近新进一个公司,做的项目是手机支付系统.由于涉及到金钱相关的,所以安全性要求特别的高.项目分了很多个子系统,在部署(测试)的时候是每个Tomcat上面只放一个子系统.比如现在有5个子系统,那么就会对 ...

  10. java的this static public protected private abstract interface 在python的对应,java python一些区别

    1.因为工作的原因,最近使用了三个多月的java作为主力语言.很早之前在菜鸟教程也看过java文档两遍,但实践少,处于能看懂写出来不流畅的状态(对于java必须要略懂,不能能看到就头疼跳过,因为现在百 ...