Article catalog
- Official test case of rocketmq4.3.0 consumer producer
- Add the dependency of rocketmq
- Reliable synchronous transmission is widely used in scenarios, such as important notification messages, SMS notifications, SMS marketing systems, etc.
- consumer
- Asynchronous transmission is usually used in response time sensitive business scenarios.
- One way transfers are used for situations that require medium reliability, such as log collection.
- rocketmq keyword description
Official test case of rocketmq4.3.0 consumer producer
- Note that the version of client and server should be the same!!
Add the dependency of rocketmq
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.3.0</version> </dependency>
Reliable synchronous transmission is widely used in scenarios, such as important notification messages, SMS notifications, SMS marketing systems, etc.
- Consumer groups
- rocketmq address
- Subject, secondary subject (tag), message
/** * Reliable synchronous transmission is widely used in scenarios, such as important notification messages, SMS notifications, SMS marketing systems, etc. */ public class SyncProducer { public static void main(String[] args) throws Exception { //Instantiate with a producer group name. DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name"); // Specify name server addresses. producer.setNamesrvAddr("192.168.102.134:9876"); //Launch the instance. producer.start(); for (int i = 0; i < 100; i++) { //Create a message instance, specifying topic, tag and message body. Message msg = new Message("cancan" /* Topic */, "TagA" /* Tag */, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ ); //Call send message to deliver message to one of brokers. SendResult sendResult = producer.send(msg); System.out.printf("%s%n", sendResult); } //Shut down once the producer instance is not longer in use. producer.shutdown(); } }
consumer
- Consumption group
- Consumer address
- Theme, secondary theme (tag)
/** * Consumer producer data */ public class Consumer { public static void main(String[] args) throws InterruptedException, MQClientException { // Instantiate with specified consumer group name. DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name"); // Specify name server addresses. consumer.setNamesrvAddr("localhost:9876"); // Subscribe one more more topics to consume. //Subscription topics, followed by * are secondary topics, also known as tag s consumer.subscribe("TopicTest", "*"); // Register callback to execute on arrival of messages fetched from brokers. consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); for (MessageExt msg : msgs) { String str = new String(msg.getBody()); System.out.println("Get message body:"+str); } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); //Launch the consumer instance. consumer.start(); System.out.printf("Consumer Started.%n"); } }
Asynchronous transmission is usually used in response time sensitive business scenarios.
- Consumer groups
- rocketmq address
- Topic, secondary topic (tag), key (message distribution is used to determine which server to store), message
/** * Asynchronous transmission is usually used in response time sensitive business scenarios. */ public class AsyncProducer { public static void main(String[] args) throws Exception { //Instantiate with a producer group name. DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name"); // Specify name server addresses. producer.setNamesrvAddr("192.168.102.134:9876"); //Launch the instance. producer.start(); producer.setRetryTimesWhenSendAsyncFailed(0); int messageCount = 103;//Number of messages sent final CountDownLatch countDownLatch = new CountDownLatch(messageCount);//Define conditions for (int i = 0; i < 100; i++) { final int index = i; //Create a message instance, specifying topic, tag and message body. Message msg = new Message("TopicTest", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET)); producer.send(msg, new SendCallback() { public void onSuccess(SendResult sendResult) { countDownLatch.countDown(); System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId()); } public void onException(Throwable e) { countDownLatch.countDown(); System.out.printf("%-10d Exception %s %n", index, e); e.printStackTrace(); } }); } //Shut down once the producer instance is not longer in use. //Thread.sleep(3000); / / delay the execution time of the main thread countDownLatch.await(5, TimeUnit.SECONDS);//Up to 100 shut down production (sender) producer.shutdown(); } }
One way transfers are used for situations that require medium reliability, such as log collection.
- Consumer groups
- rocketmq address
- Subject, secondary subject (tag), message
/** * One way transfers are used for situations that require medium reliability, such as log collection. */ public class OnewayProducer { public static void main(String[] args) throws Exception{ //Instantiate with a producer group name. DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name"); // Specify name server addresses. producer.setNamesrvAddr("192.168.102.134:9876"); //Launch the instance. producer.start(); for (int i = 0; i < 100; i++) { //Create a message instance, specifying topic, tag and message body. Message msg = new Message("TopicTest" /* Topic */, "TagA" /* Tag */, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ ); //Call send message to deliver message to one of brokers. producer.sendOneway(msg); } //Shut down once the producer instance is not longer in use. producer.shutdown(); } }
rocketmq keyword description
Refer to my previous post: https://blog.csdn.net/qq_34168515/article/details/106534430