We all know rocketmq Send message with 3 Medium mode :
1, synchronization
Send messages synchronously , You can get the results ,
2,oneway mode
This is the best way to send logs , such as , Fastest . This can be done .
3, asynchronous
It's a bit of a misunderstanding , In fact, this also returns the result .
You can see from the second screenshot , You need to implement the .
In fact , Whether it's synchronization , Or asynchronous ,broker metropolis response Relevant information came back . It's just that asynchrony requires an interface .
The following code I copy ons Of the code above :
public static void main(String[] args) {
Properties properties = new Properties();
properties.put(PropertyKeyConst.AccessKey, “DEMO_AK”);// AccessKey
Alicloud authentication , Create in Alibaba cloud server management console
properties.put(PropertyKeyConst.SecretKey, “DEMO_SK”);// SecretKey
Alicloud authentication , Create in Alibaba cloud server management console
properties.put(PropertyKeyConst.ProducerId, “DEMO_PID”);// You created Producer ID
properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis,
“3000”);// Set send timeout , Unit millisecond
Producer producer = ONSFactory.createProducer(properties);
// Before sending a message , Must call start Method to start Producer, Just one call .
producer.start();
Message msg = new Message(
// Message Topic
“TopicTestMQ”,
// Message Tag, It can be understood as Gmail Tags in , Reclassify messages , convenient Consumer Specify filter conditions in MQ Server filtering
“TagA”,
// Message Body, Any binary form of data ,MQ No intervention , need Producer And Consumer Negotiate a consistent way of serialization and deserialization
“Hello MQ”.getBytes());
// Set business critical properties on behalf of messages , Please be as global and unique as possible . In case you cannot receive the message normally , Available through MQ The console queries the message and reissues it .
// be careful : Do not set and will not affect the normal sending and receiving of messages
msg.setKey(“ORDERID_100”);
// Send message asynchronously , Send results through callback Return to client .
producer.sendAsync(msg, new SendCallback() {
@Override
public void onSuccess(final SendResult sendResult) {
// Consumption sent successfully
System.out.println(“send message success. topic=” + sendResult.getTopic() +
“, msgId=” + sendResult.getMessageId());
}
@Override
public void onException(OnExceptionContext context) {
// Message sending failed
System.out.println(“send message failed. topic=” + context.getTopic() + “,
msgId=” + context.getMessageId());
}
});
// stay callback Available before returning msgId.
System.out.println(“send message async. topic=” + msg.getTopic() + “, msgId=”
+ msg.getMsgID());
no
Before application exit , Destruction Producer object . be careful : If not destroyed
something the matter
producer.shutdown();
rocketmq send out Asynchronous messages and ons Almost , No more examples here
Technology
Daily Recommendation