当前位置:网站首页>Subscribe to Alibaba demo send business messages
Subscribe to Alibaba demo send business messages
2022-04-23 12:56:00 【Play ha ha 527】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
Commercial Ali MQ Ordinary message sending subscription Demo
Preface
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 Send a normal message
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import java.util.Properties;
public class ProducerTest {
public static void main(String[] args) {
Properties properties = new Properties();
// AccessKeyId Alicloud Authentication , Get the information from Alibaba cloud user information management console .
properties.put(PropertyKeyConst.AccessKey,"XXX");
// AccessKeySecret Alicloud Authentication , Get the information from Alibaba cloud user information management console .
properties.put(PropertyKeyConst.SecretKey, "XXX");
// Set send timeout , Company : millisecond .
properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
// Set up TCP Access domain name , Enter the message queue RocketMQ View in the access point area of the console instance details page .
properties.put(PropertyKeyConst.NAMESRV_ADDR, "XXX");
Producer producer = ONSFactory.createProducer(properties);
// Before sending a message , Must call start Method to start Producer, Just one call .
producer.start();
// Loop messages .
for (int i = 0; i < 100; i++){
Message msg = new Message(
// Ordinary messages belong to Topic, Do not use the of ordinary messages Topic To send and receive other types of messages .
"TopicTestMQ",
// Message Tag Can be understood as Gmail The label in , Recategorize messages , convenient Consumer Specify filter conditions in message queue RocketMQ Version of server filtering .
"TagA",
// Message Body It can be any binary form of data , Message queue RocketMQ The version does not do any intervention .
// need Producer And Consumer Agree on a consistent way to serialize and deserialize .
"Hello MQ".getBytes());
// Set the business critical properties that represent the message , Please be as global and unique as possible .
// In order to facilitate you to receive messages in the case of normal , It can be through message queue RocketMQ The version console queries the message and reissues it .
// Be careful : No setting will not affect the normal sending and receiving of messages .
msg.setKey("ORDERID_" + i);
try {
SendResult sendResult = producer.send(msg);
// Synchronous messaging , As long as we don't throw exceptions, we will succeed .
if (sendResult != null) {
System.out.println(new Date() + " Send mq message success. Topic is:" + msg.getTopic() + " msgId is: " + sendResult.getMessageId());
}
}
catch (Exception e) {
// Message delivery failed , Need to retry processing , You can resend the message or persist the data for compensation processing .
System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
e.printStackTrace();
}
}
// Before the app exits , The destruction Producer object .
// Be careful : If you don't destroy it, there's no problem .
producer.shutdown();
}
}
Two 、 Subscribe to general news
Subscription mode
Message queue RocketMQ Version supports the following two subscription methods :
Cluster subscription
The same Group ID All marked Consumer Average share of consumption news . For example, a Topic Yes 9 Bar message , One Group ID Yes 3 individual Consumer example , Then, in the cluster consumption mode, each instance is allocated equally , Consume only... Of them 3 Bar message . The settings are as follows .
// Cluster subscription mode settings ( Without setting , The default is cluster subscription mode ).
properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
Broadcast subscription
The same Group ID All marked Consumer They will each consume a message once . For example, a Topic Yes 9 Bar message , One Group ID Yes 3 individual Consumer example , Then in the broadcast consumption mode, each instance will consume its own 9 Bar message . The settings are as follows .
// Broadcast subscription mode settings .
properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);
Message queue RocketMQ Version supports the following two message acquisition methods :
Push: The message is sent by the message queue RocketMQ Push version to Consumer.Push Under way , Message queue RocketMQ The version also supports batch consumption , Batch messages can be uniformly pushed to Consumer Consumption .
Pull: Message by Consumer Active from message queue RocketMQ Plate pulling .
Here we show only Push Send message by
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import java.util.Properties;
public class ConsumerTest {
public static void main(String[] args) {
Properties properties = new Properties();
// You created Group ID.
properties.put(PropertyKeyConst.GROUP_ID, "XXX");
// AccessKey ID Alicloud Authentication , In Ali cloud RAM Console creation .
properties.put(PropertyKeyConst.AccessKey, "XXX");
// Accesskey Secret Alicloud Authentication , In Alibaba cloud service RAM Console creation .
properties.put(PropertyKeyConst.SecretKey, "XXX");
// Set up TCP Access domain name , Enter the instance details page of the console TCP Protocol client access point area view .
properties.put(PropertyKeyConst.NAMESRV_ADDR, "XXX");
// Subscription cluster mode ( Default ).
// properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
// Broadcast subscription mode .
// properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);
Consumer consumer = ONSFactory.createConsumer(properties);
consumer.subscribe("TopicTestMQ", "TagA||TagB", new MessageListener() {
// Subscribe to multiple Tag.
public Action consume(Message message, ConsumeContext context) {
System.out.println("Receive: " + message);
return Action.CommitMessage;
}
});
// Subscribe to another Topic, To unsubscribe from this Topic, Please delete the subscription code in this section , Restart the consumer .
consumer.subscribe("TopicTestMQ-Other", "*", new MessageListener() {
// Subscribe to all Tag.
public Action consume(Message message, ConsumeContext context) {
System.out.println("Receive: " + message);
return Action.CommitMessage;
}
});
consumer.start();
System.out.println("Consumer Started");
}
}
summary
版权声明
本文为[Play ha ha 527]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230615230996.html
边栏推荐
- BUUCTF WEB [BJDCTF2020]The mystery of ip
- NPDP|产品经理如何做到不会被程序员排斥?
- How to prevent the website from being hacked and tampered with
- leetcode:437. 路径总和 III【dfs 选还是不选?】
- SSM框架系列——数据源配置day2-1
- Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
- Remote sensing image classification and recognition system based on convolutional neural network
- What are the forms of attack and tampering on the home page of the website
- At instruction of nbiot
- 洛谷P3236 [HNOI2014]画框 题解
猜你喜欢
如何防止网站被黑客入侵篡改
Mysql8 installation
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
mysql8安装
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
Resolve disagrees about version of symbol device_ create
The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address
Labels and paths
Please help me see what this is, mysql5 5. Thanks
Zero trust in network information security
随机推荐
The El table horizontal scroll bar is fixed at the bottom of the visual window
Sort out several uses of network IP agent
Web17 -- use of El and JSTL
Labels and paths
box-sizing
有趣的IDEA插件推荐,给你的开发工作增添色彩
Unable to create servlet under SRC subfile of idea
CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
航芯技术分享 | ACM32 MCU安全特性概述
Image attribute of input: type attribute of fashion cloud learning -h5
STM32 is connected to the motor drive, the DuPont line supplies power, and then the back burning problem
Luogu p3236 [hnoi2014] picture frame solution
Object.keys后key值数组乱序的问题
Recommended website for drawing result map
Zigbee之CC2530最小系统及寄存器配置(1)
Common problems of unity (1)
教你快速开发一个 狼人杀微信小程序(附源码)
5 free audio material websites, recommended collection
How to prevent the website from being hacked and tampered with
XinChaCha Trust SSL Organization Validated