当前位置:网站首页>ActiveMQ Basics
ActiveMQ Basics
2022-04-23 14:12:00 【All the names I thought of were used】
One 、 The role of message middleware
( One ) The application of decoupling
For example, during login , After receiving the registration message, the server writes the user information into the database , Then call the push system to send a short message to the user to inform that the registration is successful . However , The push system may be separate from this application , We can deliver the message to the push system through message middleware , After the push system receives the message , call service Push text messages to users .
Some students said , Then I use dubbo It can also complete the corresponding functions . Yes , But not as fast as message oriented middleware , Because it is an asynchronous , As long as the server sends the message, it can respond to the user without waiting for the result returned by the push system
( Two ) Traffic peak clipping
In the second kill system , The second kill request initiated by the user will first reach the message middleware , Then the second kill system takes out the messages from the message queue in order , The rest of the message is returned , Tell the user that the second kill failed .
Two 、ActiveMq Basic components
( One ) ConnectionFactory
This class is used to create connection Connected
// Define the link factory
ConnectionFactory connectionFactory = null;
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.70.151:61616");
( Two ) Connection: Connection between client and middleware
// Create connection objects
connection = connectionFactory.createConnection();
// Start the connection
connection.start();
( 3、 ... and ) Session: Session between client and middleware
/**
* transacted: Whether to use transactions Optional value is :true|false
* true: With a transaction When setting the secondary variable value .Session.SESSION_TRANSACTED
* false: Not applicable to matters , Set the secondary variable be acknowledgeMode Parameters must be set
* acknowledgeMode:
* Session.AUTO_ACKNOWLEDGE: Automatic message acknowledgment mechanism
* Session.CLIENT_ACKNOWLEDGE: Client confirmation mechanism
* Session.DUPS_OK_ACKNOWLEDGE: Client acknowledgement message mechanism with replica
*/
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
( Four ) destination
Point to point mode :queue
A subscription model :topic
// Create destination , The destination name is the name of the queue . The consumer of the message needs to access the corresponding queue by this name
destination = session.createQueue("my-destination");
destination = session.createTopic("my-destination");
( 5、 ... and ) Producer && Consumer
// Create the producer of the message
MessageProducer producer = session.createProducer(destination);
// Create consumers of messages
MessageConsumer consumer = session.createConsumer(destination);
( 6、 ... and ) Message
// Create a text message
message = session.createTextMessage(”hello“);
producer.send(message);
// Receive text message
TextMessage message = (TextMessage)consumer.receive();
string text = message.getText();
// Create object message
message = session.createObjectMessage(obj);
producer.send(message);
// Object receive message
ObjectMessage message = (ObjectMessage)consumer.receive();
User user = message.getObject();
3、 ... and 、Springboot Integrate activeMq
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
( One ) yml To configure
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
jms:
pub-sub-domain: true # The default is false:queue true:topic
queue: queue_mq # Point to point consumer names
topic: topic_mq # Subscription consumer name
( Two ) destination To configure
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
@EnableJms
public class ActiveMqConfig {
@Value("${queue}")// Corresponding yml Defined in the file queue
private String queue;
@Value("${topic}")// Corresponding yml Defined in the file topic
private String topic;
/**
* Create a peer-to-peer queue A message can only be consumed by one consumer --- one-on-one
* @return
*/
@Bean
public Queue queue(){
return new ActiveMQQueue(queue);
}
/**
* Create a subscription queue A message can be consumed by multiple consumers --- One to many
* @return
*/
@Bean
public Topic topic(){
return new ActiveMQTopic(topic);
}
}
( 3、 ... and ) producer
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
import javax.jms.Topic;
@RestController
public class ProducerController {
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* Producers of peer-to-peer message queues
* @param string
*/
@GetMapping("/queue")
public void sendMsgQueue(@RequestParam String string){
System.out.println(" The message has been sent , Ready to be consumed , The message is ---> "+string);
jmsMessagingTemplate.convertAndSend(queue,string);
}
/**
* Producers of one to many message queues
* @param string
*/
@GetMapping("/topic")
public void sendMsgTopic(@RequestParam String string){
System.out.println(" The message has been sent , Ready to be consumed , The message is ---> "+string);
jmsMessagingTemplate.convertAndSend(topic,string);
}
}
( Four ) consumer
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class TopicConsumer {
/**
* Listen to the message , Name is the name sent by the producer , Should agree , Otherwise we won't be able to monitor .
* Because it's subscriber mode , There can be multiple consumers , Here are two examples to test
* @param string
*/
@JmsListener(destination = "${topic}")
public void consumerTopicOne(String string){
System.out.println(" I'm consumer one : Consumption message succeeded , The message is ---> "+string);
}
@JmsListener(destination = "${topic}")
public void consumerTopicTwo(String string){
System.out.println(" I'm consumer number two : Consumption message succeeded , The message is ---> "+string);
}
}
版权声明
本文为[All the names I thought of were used]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231404419510.html
边栏推荐
猜你喜欢
基于CM管理的CDH集群集成Phoenix
帆软中需要设置合计值为0时,一整行都不显示的解决办法
squid代理
微信小程序调用客服接口
Use of WiFi module based on wechat applet
Visio installation error 1:1935 2: {XXXXXXXX
星界边境文本自动翻译机使用说明
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
Operation instructions of star boundary text automatic translator
教育行业云迁移最佳实践:海云捷迅使用HyperMotion云迁移产品为北京某大学实施渐进式迁移,成功率100%
随机推荐
关于训练过程中损失函数出现断崖式增长的问题
Wechat applet communicates with low-power Bluetooth - receives data sent by hardware (IV)
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
云迁移的六大场景
基于ibeacons三点定位(微信小程序)
JSP学习2
使用Postman进行Mock测试
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
如何快速批量创建文本文档?
烟雾传感器(mq-2)使用详细教程(基于树莓派3b+实现)
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
squid代理
Promtail + Loki + Grafana 日志监控系统搭建
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
微信小程序调用客服接口
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
帆软实现分页时第一行和最后两行冻结方式