【ActiveMQ】使用学习

转载:

1、启动

activemq start

2、停止

activemq stop

http://localhost:8161

admin / admin

Queue - Point-to-Point (点对点)

一条消息只能被一个消费者消费, 且是持久化消息 - 当没有可用的消费者时,该消息保存直到被消费为止;当消息被消费者收到但不响应时(具体等待响应时间是多久,如何设置,暂时还没去了解),该消息会一直保留或会转到另一个消费者当有多个消费者的情况下。当一个Queue有多可用消费者时,可以在这些消费者中起到负载均衡的作用。

Topic - Publisher/Subscriber Model (发布/订阅者)

一条消息发布时,所有的订阅者都会收到,topic有2种模式,Nondurable subscription(非持久订阅)和durable subscription (持久化订阅 - 每个持久订阅者,都相当于一个持久化的queue的客户端), 默认是非持久订阅。

持久化:消息产生后,会保存到文件/DB中,直到消息被消费, 如上述Queue的持久化消息。默认保存在ActiveMQ中:%ActiveMQ_Home%/data/kahadb

非持久化:消息不会保存,若当下没有可用的消费者时,消息丢失。

Spring Boot 中使用

配置 JmsTemplate 和 DefaultJmsListenerContainerFactory

package ycx.activemq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate; import javax.jms.ConnectionFactory; @Configuration
@EnableJms
public class ActiveMQConfig {
public static final String MY_QUEUE = "ycx.queue";
public static final String MY_TOPIC = "ycx.topic"; @Bean("queueJmsTemplate")
public JmsTemplate queueJmsTemplate(ConnectionFactory connectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.setDefaultDestinationName(MY_QUEUE);
return jmsTemplate;
} @Bean("queueContainerFactory")
public DefaultJmsListenerContainerFactory queueContainerFactory(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setSessionTransacted(true);
factory.setConcurrency("1");
factory.setRecoveryInterval(1000L);
return factory;
} @Bean("topicJmsTemplate")
public JmsTemplate topicJmsTemplate(ConnectionFactory connectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.setDefaultDestinationName(MY_QUEUE);
jmsTemplate.setPubSubDomain(true);
return jmsTemplate;
} @Bean("topicContainerFactory")
public DefaultJmsListenerContainerFactory topicContainerFactory(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setSessionTransacted(true);
factory.setConcurrency("1");
factory.setRecoveryInterval(1000L);
factory.setPubSubDomain(true);
return factory;
}
}

连接工厂使用自动注入进来的,如果不想使用默认的可以自动配置

spring:
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin

或者在 java中指定

@Bean
public ConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory(MY_USERNAME, MY_PASSWORD, MY_BROKER_URL);
}

定义监听器

Queue监听器A

package ycx.activemq.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import ycx.activemq.config.ActiveMQConfig; @Component
public class QueueMessageListenerA { @JmsListener(destination = ActiveMQConfig.MY_QUEUE, containerFactory = "queueContainerFactory")
public void handleMessage(String msg) {
System.out.println("queue A >>> " + msg);
}
}

Queue监听器B

package ycx.activemq.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import ycx.activemq.config.ActiveMQConfig; @Component
public class QueueMessageListenerB { @JmsListener(destination = ActiveMQConfig.MY_QUEUE, containerFactory = "queueContainerFactory")
public void handleMessage(String msg) {
System.out.println("queue B >>> " + msg);
}
}

Topic监听器A

package ycx.activemq.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import ycx.activemq.config.ActiveMQConfig; @Component
public class TopicMessageListenerA {
@JmsListener(destination = ActiveMQConfig.MY_TOPIC, containerFactory = "topicContainerFactory")
public void handleMessage(String msg) { System.out.println("topic A >>> " + msg); }
}

Topic监听器B

package ycx.activemq.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import ycx.activemq.config.ActiveMQConfig; @Component
public class TopicMessageListenerB {
@JmsListener(destination = ActiveMQConfig.MY_TOPIC, containerFactory = "topicContainerFactory")
public void handleMessage(String msg) {
System.out.println("topic B >>> " + msg);
}
}

Topic监听器C

package ycx.activemq.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import ycx.activemq.config.ActiveMQConfig; @Component
public class TopicMessageListenerC {
@JmsListener(destination = ActiveMQConfig.MY_TOPIC, containerFactory = "topicContainerFactory")
public void handleMessage(String msg) {
System.out.println("topic C >>> " + msg);
}
}

测试

package ycx.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ycx.activemq.config.ActiveMQConfig; import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map; @SpringBootApplication
@RestController
public class ActivemqServerApplication { public static void main(String[] args) {
SpringApplication.run(ActivemqServerApplication.class, args);
} @Autowired
@Qualifier("queueJmsTemplate")
JmsTemplate queueJmsTemplate; @Autowired
@Qualifier("topicJmsTemplate")
JmsTemplate topicJmsTemplate; @RequestMapping("/queue")
public Object queue() {
String content = "queue send: " + LocalTime.now().toString();
queueJmsTemplate.convertAndSend(ActiveMQConfig.MY_QUEUE, content); Map<String, String> res = new HashMap<>();
res.put("content", content);
return res;
} @RequestMapping("/topic")
public Object topic() {
String content = "topic send : " + LocalTime.now().toString();
topicJmsTemplate.convertAndSend(ActiveMQConfig.MY_TOPIC, content); Map<String, String> res = new HashMap<>();
res.put("content", content);
return res;
}
}

访问地址: http://localhost:8080/topic

订阅 收到消息,所有的监听器都受到消息

topic B >>> topic send : ::05.024
topic C >>> topic send : ::05.024
topic A >>> topic send : ::05.024

访问地址:http://localhost:8080/queue

队列 收到消息,只有一个监听器收到消息

queue B >>> queue send: ::58.491

【ActiveMQ】使用学习的更多相关文章

  1. ActiveMQ的学习整理(代码实现PTP,以及Pub/Sub)

    (一)由于在实习过程中需要用到ActiveMQ,在网上看了很多文章,现在整理出来以防忘记. (二)这篇文章比较适合之前没有接触过的同学,在看下面文章的过程中,建议先学习参考链接中的知识点,然后自己再参 ...

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

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

  3. ActiveMQ进阶学习

    本文主要讲述ActiveMQ与spring整合的方案.介绍知识点包括spring,jms,activemq基于配置文件模式管理消息,消息监听器类型,消息转换类介绍,spring对JMS事物管理. 1. ...

  4. ActiveMQ初步学习

    本文主要参考张丰哲大神的简书文章,链接 https://www.jianshu.com/p/ecdc6eab554c JMS,即Java Message Service,通过面向消息中间件(MOM:M ...

  5. activemq的学习

    https://blog.csdn.net/csdn_kenneth/article/category/7352171/1

  6. MQ学习(一)----JMS规范(转发整合)

    最近进行ActiveMQ的学习,总结下已被不时之需. JMS规范: JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的A ...

  7. Java Message Service学习(一)

    一,背景 近期需要用到ActiveMQ接收Oozie执行作业之后的返回结果.Oozie作为消息的生产者,将消息发送给ActiveMQ,然后Client可以异步去ActiveMQ取消息. ActiveM ...

  8. 6、Sping Boot消息

    1.消息概述 可通过消息服务中间件来提升系统异步通信.扩展解耦能力 消息服务中两个重要概念:消息代理(message broker)和目的地(destination)当消息发送者发送消息以后,将由消息 ...

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

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

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

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

随机推荐

  1. Hadoop2.8.2 运行wordcount

    1 例子jar位置 [hadoop@hadoop02 mapreduce]$ pwd /hadoop/hadoop-2.8.2/share/hadoop/mapreduce [hadoop@hadoo ...

  2. linux内核的preempt抢占调度,preempt_count抢占保护“锁”

    抢断调度,是调度机制对实时系统需要的支持,是一种快速响应的重调度机制.既然与重调度有关,那么就先回顾一下调度和重调度. 调度分两种情况,1. 一种是自愿调度,由代码主动调用schedule来让度cpu ...

  3. aws msk

    1. 建立3个私网子网 2. 建立msk

  4. Python 编程语言要掌握的技能之一:善用变量来改善代码质量

    如何为变量起名 在计算机科学领域,有一句著名的格言(俏皮话): There are only two hard things in Computer Science: cache invalidati ...

  5. .NET Core 3.0 使用Nswag生成Api文档和客户端代码

    摘要 在前后端分离.Restful API盛行的年代,完美的接口文档,成了交流的纽带.在项目中引入Swagger (也称为OpenAPI),是种不错的选择,它可以让接口数据可视化.下文将会演示 利用N ...

  6. Vue.js大屏数字滚动翻转效果

    ================================ 大屏数字滚动翻转效果来源于最近工作中element后台管理页面一张大屏的UI图,该UI图上有一个模块需要有数字往上翻动的效果,以下是最 ...

  7. Python3 之 类属性与实例属性

    1.类属性与实例属性 类属性就相当与全局变量,实例对象共有的属性,实例对象的属性为实例对象自己私有. 类属性就是类对象(Tool)所拥有的属性,它被所有类对象的实例对象(实例方法)所共有,在内存中只存 ...

  8. 2019-9-11:渗透测试,基础学习,VMware安装centos 7

    VMware Workstation 15 Pro 安装Centos 7,详细图文步骤 1,点击VMware菜单栏的“文件”-->“新建虚拟机”,选择“典型”使用向导创建虚拟机,点击“下一步” ...

  9. 第三个视频作品《小白快速入门greenplum》上线了

    1.场景描述 第三个视频作品出炉了,<小白快速入门greenplum>上线了,有需要的朋友可以直接点击链接观看.(如需购买,请通过本文链接购买) 2. 课程内容 课程地址:https:// ...

  10. sql语句执行步骤详解

    目录 一.准备工作 二.SQL逻辑查询语句执行顺序 三.SQL书写习惯 一.准备工作 先来一段伪代码,首先你能看懂么? SELECT DISTINCT <select_list> FROM ...