一、下载

ActiveMQ 5.15.0下载地址

二、安装

解压apache-activemq-5.15.0-bin.zip

D:\apache-activemq-5.15.7-bin\apache-activemq-5.15.7\bin\win64

双击activemq.bat执行  开始自动安装启动

访问地址mq客户端页面     http://localhost:8161/admin/topics.jsp  登录账户密码默认admin/admin

三、使用

jar包  activemq-all-5.4.3.jar    activemq-pool-5.4.3.jar

3.1.1ActiviteMQ消息有3种形式

  1. 点对点方式(point-to-point)

点对点的消息发送方式主要建立在 Message Queue,Sender,reciever上,Message Queue 存贮消息,Sneder 发送消息,receive接收消息.具体点就是Sender Client发送Message Queue ,而 receiver Cliernt从Queue中接收消息和"发送消息已接受"到Quere,确认消息接收。消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行

  1. 发布/订阅 方式(publish/subscriber Messaging)

发布/订阅方式用于多接收客户端的方式.作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。作为subscriber ,在接收消息时有两种方法,destination的receive方法,和实现message listener 接口的onMessage 方法。

3.1.2ActiviteMQ接收和发送消息基本流程

发送消息的基本步骤:

(1)、创建连接使用的工厂类JMS ConnectionFactory

(2)、使用管理对象JMS ConnectionFactory建立连接Connection,并启动

(3)、使用连接Connection 建立会话Session

(4)、使用会话Session和管理对象Destination创建消息生产者MessageSender

(5)、使用消息生产者MessageSender发送消息

消息接收者从JMS接受消息的步骤

(1)、创建连接使用的工厂类JMS ConnectionFactory

(2)、使用管理对象JMS ConnectionFactory建立连接Connection,并启动

(3)、使用连接Connection 建立会话Session

(4)、使用会话Session和管理对象Destination创建消息接收者MessageReceiver

(5)、使用消息接收者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver消息接收者必须实现了MessageListener接口,需要定义onMessage事件方法。

四、java代码

Spring集成

新建activemq_config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- <value>tcp://192.168.0.140:61616</value> -->

<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://192.168.0.127:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>

<!--测试Queue,队列的名字是spring-queue-->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="spring-queue"/>
</bean>

<!--测试Topic-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="cms.vss.pic.topic2"/>
</bean>
<!--使用缓存可以提升效率-->
<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsFactory"/>
<property name="sessionCacheSize" value="1"/>
</bean>
<!-- 生产者 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="cachingConnectionFactory"/>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
<!-- 消费者 -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="cachingConnectionFactory"/>
<property name="destination" ref="destinationTopic"/>
<property name="messageListener" ref="topicListener"/>
</bean>

<!-- 消息监听器 -->
<bean id="topicListener" class="com.zlkj.activemq.listener.TopicListener">
</bean>
</beans>

发送消息

package com.zlkj.wl.service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.zlkj.wl.bean.ActiveBean;

@Service("senderService")
public class AMQSenderServiceImpl implements AmqSenderService {

@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;

//目的地队列的明证,我们要向这个队列发送消息
@Resource(name = "destinationTopic")
private Destination destination;

//向特定的队列发送消息
@Override
public void sendMsg(final ActiveBean mqParamDto) {
//final String msg = JsonTools.ojbToJson(mqParamDto, false);
// final String msg =mqParamDto;
try {
jmsTemplate.send(destination, new MessageCreator() {
@Override
public ObjectMessage createMessage(Session session) throws JMSException {
return session.createObjectMessage(mqParamDto);
//return session.createTextMessage(msg);
}
});

} catch (Exception ex) {
}

}
}

接收消息则通过xml中配置监听可以接收到消息

创建监听类

package com.zlkj.activemq.listener;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class TopicListener implements MessageListener {

@Override
public void onMessage(Message msg) {
try {
Destination destination = msg.getJMSDestination();
String topic = destination.toString();
if(topic.equals("topic://INOUT")) {
System.out.println("1");
TextMessage txtMsg = (TextMessage) msg;
String message = txtMsg.getText();
//实际项目中拿到String类型的message(通常是JSON字符串)之后,
//会进行反序列化成对象,做进一步的处理
System.out.println("receive txt msg===" + message);
} else if (topic.equals("topic://ENROLL")) {
System.out.println("2");
TextMessage txtMsg = (TextMessage) msg;
String message = txtMsg.getText();
//实际项目中拿到String类型的message(通常是JSON字符串)之后,
//会进行反序列化成对象,做进一步的处理
System.out.println("receive txt msg===" + message);
} else if (topic.equals("topic://ACTION")) {
System.out.println("3");
TextMessage txtMsg = (TextMessage) msg;
String message = txtMsg.getText();
//实际项目中拿到String类型的message(通常是JSON字符串)之后,
//会进行反序列化成对象,做进一步的处理
System.out.println("receive txt msg===" + message);
}

} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}

接收消息监听不打出时  注意constructor-arg的值(标红)和手动发送消息的session.createTopic("ENROLL")值应相同

不集成spring

发送消息

package com.zlkj.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.zlkj.pub.toolutil.JsonTools;
import com.zlkj.wl.bean.ActiveBean;

public class NoPersistenceActiveSender {

private static final int SENDNUM = 10;

public static void sendmes( ActiveBean record) {
//连接工厂
ConnectionFactory connectionFactory;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session;
//消息的目的地
Destination destination;
//消息生产者
MessageProducer messageProducer;
//实例化连接工厂(连接到ActiveMQ服务器)
connectionFactory = new ActiveMQConnectionFactory(null, null, "tcp://192.168.0.127:61616");

try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//创建一个名称为MyTopic的消息队列(生产者生成的消息放在哪)
destination = session.createTopic("ENROLL");
//创建消息生产者
messageProducer = session.createProducer(destination);
//发送消息
sendMessage(session, messageProducer,record);

session.commit();

} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
/**
* 发送消息
*
* @param session
* @param messageProducer 消息生产者
* @throws Exception
*/
public static void sendMessage(Session session, MessageProducer messageProducer,ActiveBean record) throws Exception {
ObjectMessage message = session.createObjectMessage(record);
messageProducer.send(message);
// }

}
}

接收消息

package com.zlkj.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class NoPersistenceReceiver {

public static void main(String[] args) {
ConnectionFactory connectionFactory;//连接工厂
Connection connection = null;//连接

Session session;//会话 接受或者发送消息的线程
Destination destination;//消息的目的地

MessageConsumer messageConsumer;//消息的消费者

//实例化连接工厂(连接到ActiveMQ服务器)
connectionFactory = new ActiveMQConnectionFactory(null, null, "tcp://192.168.0.127:61616");

try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//生产者将消息发送到MyTopic,所以消费者要到MyTopic去取
destination = session.createTopic("ENROLL1a");
//destination = session.createTopic("MyTopic");
//创建消息消费者
messageConsumer = session.createConsumer(destination);

Message message = messageConsumer.receive();

while (message != null) {
TextMessage txtMsg = (TextMessage) message;
System.out.println("收到消息:" + txtMsg.getText());
message = messageConsumer.receive();
}
} catch (JMSException e) {
e.printStackTrace();
}

}
}

接收消息的时候一定执行main方法  用while死循环一直轮训手动接收发送的消息

学习activeMq源路径网址    https://www.jianshu.com/p/8caa6d66b10d

activeMq学习应用的更多相关文章

  1. ActiveMQ学习笔记(5)——使用Spring JMS收发消息

      摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...

  2. 【ActiveMQ】Spring Jms集成ActiveMQ学习记录

    Spring Jms集成ActiveMQ学习记录. 引入依赖包 无论生产者还是消费者均引入这些包: <properties> <spring.version>3.0.5.REL ...

  3. ActiveMQ学习总结------原生实战操作(下)03

    本篇将继续延续上一篇的内容,作为知识补充篇,为接下来我们学习spring整合ActiveMQ打好基础 本篇主要学习内容: 1.ActiveMQ 队列服务监听 2.ActiveMQ Topic模型 回顾 ...

  4. ActiveMQ学习教程/2.简单示例

    ActiveMQ学习教程(二)——简单示例 一.应用IDEA构建Maven项目 File->New->Module...->Maven->勾选->选择->Next ...

  5. ActiveMQ学习教程/1.简要介绍与安装

    ActiveMQ学习教程(一)——简要介绍与安装 一.名词: 1.JMS:即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的 ...

  6. ActiveMQ学习总结(一)

    自己写的网上商城项目中使用了ActiveMQ,虽然相比于RabbitMQ,kafka,RocketMQ等相比,ActiveMQ可能性能方面不是最好的选择,不过消息队列其实原理区别不大,这里对学过的关于 ...

  7. 学习笔记-记ActiveMQ学习摘录与心得(二)

    上个周末被我玩过去了,罪过罪过,现在又是一个工作日过去啦,居然有些烦躁,估计这几天看的东西有点杂,晚上坐下来把自己首要工作任务总结总结.上篇学习博客讲了ActiveMQ的特性及安装部署,下面先把我以前 ...

  8. 学习笔记-记ActiveMQ学习摘录与心得(一)

    这两天在看开源的MQ技术,趁着晚上安静,把这两天学的东西摘录下.在公司学东西效率真心捉鸡,心里总觉得别扭,拿了公司的钱不干活还在那学习,表示心情不淡定,效率不行啊...晚上时间是我的,下班还是蛮开心的 ...

  9. ActiveMQ学习系列(四)----消息持久化到mysql

    前记:目前学习还比较杂乱,还未找到系统化地学习ActiveMq的方法.在网上看到消息持久化的demo,了解了一下,在此记录. 一.目前ActiveMq支持的持久化方法 url:http://activ ...

  10. ActiveMQ 学习第二弹

    经历了昨天的初识 ActiveMQ,正好今天下班有点事耽搁了还没法回家,那就再学习会 ActiveMQ 吧!现在官网的文档没啥好看的了,毕竟是入门学习,太深奥的东西也理解不了.然后看官网上有推荐书籍& ...

随机推荐

  1. 如何在Eclipse中写Processing的sketch

    有时候人们需要写更复杂的sketch,此时Processing提供的IDE就略显单薄,下面将介绍如何在eclipse中开发Processing. 一共分4步: 一.搭建环境:安装JRE.JDK.Ecl ...

  2. combox使用自定义的model列表中无元素显示

    自定义的model(stationModel)中有 name 和point两种属性名. 初始化stationModel Combobox{ textRole: 'name' model:station ...

  3. AtCoder Grand Contest 009 题解

    传送门 为啥这场题目少一点啊-- \(A\) 易知增加的数前面肯定比后面大,那么我们倒着做,然后维护一下最小要加多少就可以了 typedef long long ll; const int N=1e5 ...

  4. RS码的突发纠错能力

    RS码便于纠突发错误.所谓突发错误,是指burst errors. 即一长串连续位出错.例如 0011XXXX1010. 其中X表示出错.如果是GF(2^4)中定义的RS码,则可以由一个符号错误纠正. ...

  5. 《挑战30天C++入门极限》C++面向对象编程入门:类(class)

        C++面向对象编程入门:类(class) 上两篇内容我们着重说了结构体相关知识的操作. 以后的内容我们将逐步完全以c++作为主体了,这也意味着我们的教程正式进入面向对象的编程了. 前面的教程我 ...

  6. golang-package fmt

    package fmt import "fmt" mt包实现了类似C语言printf和scanf的格式化I/O.格式化动作('verb')源自C语言但更简单. Printing v ...

  7. win7“您可能没有权限使用网络资源”的解决办法

    删除“拒绝从网络访问这台计算机”里面的Gu   1 在屏幕的左下角点开[开始]----在搜索程序和文件那个文本框里输入[输入gpedit.msc]然后回车. 2 弹出如下图的对话框, 3 依次展开-- ...

  8. Nodejs技巧之---Exceljs

    工作中我们可能会遇到制作表格的需求,那么针对nodejs如何制作一个汇总表格呢? 今天我们就在此介绍下exceljs的基本使用,应该可以满足我们大部分的需求. 第一部分 下载 npm install ...

  9. 单一职责原则(SRP)

    内聚性:一个模块的组成元素之间的功能相关性.就一个类而言,应该仅有一个引起它变化的原因.当需求变化时,该变化会反映为类的职责的变化,如果一个类承担了多于一个的职责,那么引起它变化的原因就会有多个.如果 ...

  10. 如何在wcf中用net tcp协议进行通讯

    快速阅读 如何在wcf中用net tcp协议进行通讯,一个打开Wcf的公共类.比较好好,可以记下来. 配置文件中注意配置 Service,binding,behaviors. Service中配置en ...