【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. nyoj 125-盗梦空间 (数学ans += temp * 60 * pow(0.05, cnt))

    125-盗梦空间 内存限制:64MB 时间限制:3000ms 特判: No 通过数:8 提交数:10 难度:2 题目描述: <盗梦空间>是一部精彩的影片,在这部电影里,Cobb等人可以进入 ...

  2. opencv 5 图像转换(2 霍夫变换)

    霍夫线变换 标准霍夫变换和多尺度霍夫变换(HoughLines()函数) 实例: #include <opencv2/opencv.hpp> #include <opencv2/im ...

  3. PL真有意思(二):程序设计语言语法

    前言 虽然标题是程序语言的语法,但是讲的是对词法和语法的解析,其实关于这个前面那个写编译器系列的描述会更清楚,有关语言语法的部分应该是穿插在整个设计当中的,也看语言设计者的心情了 和英语汉语这些自然语 ...

  4. String类的详细

    String str = new String("abc")创建过程 (1) 先定义一个名为str的对String类的对象引用变量放入栈中. (2) 然后在堆中(不是常量池)创建一 ...

  5. Github远程库与Git本地库连接

    Github远程库与Git本地库连接 以下有任何[]符号只是将内容扩起,输入命令不需要将[]加入 创建SSH Key 用户主目录有.ssh->id_rsa和id_rae.pub->直接跳过 ...

  6. mac系统下docker安装配置mysql详细步骤

    上文介绍了MacOS安装Docker傻瓜式教程,安装好后第一件事就决定把本地数据库迁移过来,那么首先就得安装mysql,下面就开始我们的安装之旅吧. 一.docker配置镜像加速器 我们使用docke ...

  7. sql注入问题回顾

    (以下语法均为在python中使用mysql语句,部分代码省略,使用python中的pymsql模块获取游标对象即可直接执行sql语句) sql注入:在传入参数的时候做出改变,使得插入数据这条sql语 ...

  8. 设计模式之观察者模式--PHP

    列举一个场景:下班之后回家,打开家门,开始做饭,之后睡觉 以上场景如果按照传统的开始方式就是封装一个用户类,里面有回家方法,打开门方法,做饭方法,睡觉方法,之后在外面依次调用. 假设你代码开发完了,这 ...

  9. [ch04-03] 用神经网络解决线性回归问题

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 4.3 神经网络法 在梯度下降法中,我们简单讲述了一下神 ...

  10. 关于jsp中jstl报错Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core

    有的时候在开发jsp时,需要使用jstl时,在jsp上面引用jstl却出现错误:Can not find the tag library descriptor for "http://jav ...