RocketMQ (1) -- helloworld,
Dual Master mode:
Server Environment
| Serial number |
IP |
Role |
Mode |
| 1 |
192.168.32.135 |
NameServer1, brokerServer1 |
Master1 |
| 2 |
192.168.32.136 |
NameServer2, brokerServer2 |
Master2 |
Helloworld sample code:
/*** Producer, send the message **/public class Producer {public static void main (String [] args) throws MQClientException, interruptedException {defamqmqproducer producer = new defamqmqproducer ("quickstart_producer"); producer. setNamesrvAddr ("192.168.32.135: 9876; 92.168.32.136: 9876"); producer. start (); for (int I = 0; I <100; I ++) {try {Message msg = new Message ("TopicTest", // topic "TagA ", // tag ("Hello RocketMQ" + I ). getBytes () // body); SendResult sendResult = producer. send (msg); System. out. println (sendResult);} catch (Exception e) {e. printStackTrace (); Thread. sleep (1000) ;}} producer. shutdown () ;}/ *** Consumer, subscribe to messages */public class Consumer {public static void main (String [] args) throws InterruptedException, MQClientException {defamqmqpushconsumer consumer = new DefaultMQPushConsumer ("quickstart_consumer"); consumer. setNamesrvAddr ("192.168.32.135: 9876; 192.168.32.136: 9876 "); /*** set whether to start consumption from the queue header or tail when the Consumer is started for the first time. <br> * if it is not started for the first time, continue consumption according to the position of the previous consumption */consumer. setConsumeFromWhere (ConsumeFromWhere. CONSUME_FROM_FIRST_OFFSET); consumer. subscribe ("TopicTest", "*"); consumer. registerMessageListener (new MessageListenerConcurrently () {@ Override public ConsumeConcurrentlyStatus consumeMessage (List <MessageExt> msgs, ConsumeConcurrentlyContext context) {// System. out. println (Thread. currentThread (). getName () + "Receive New Messages:" + msgs); try {for (MessageExt msg: msgs) {String topic = msg. getTopic (); String msgBody = new String (msg. getBody (), "UTF-8"); String tags = msg. getTags (); System. out. println ("received message:" + "topic:" + topic + "msgBody:" + msgBody + "tags:" + tags) ;}} catch (UnsupportedEncodingException e) {e. printStackTrace (); return ConsumeConcurrentlyStatus. RECONSUME_LATER;} return ConsumeConcurrentlyStatus. CONSUME_SUCCESS ;}}); consumer. start (); System. out. println ("Consumer Started. ");}}View Code
Start the consumer and producer. The result is as follows:
Q: The parameter of the consumeMessage method is List <MessageExt> msgs, which indicates that the consumer can consume multiple messages at a time. What can be done after one message fails to be processed?
Answer: 1. By default, the consumer consumes only one message at a time, which can be modified through consumer. setConsumeMessageBatchMaxSize (10.
2. If you first start the consumer and then start the production end, even if you have set it above, the consumer end also consumes a message at a time.
3. if the producer is started and then the consumer is started, the consumer consumes multiple messages at a time. For example, if five messages are pulled at a time, all the five messages will be retried. In this case, duplicate messages must be removed from the business logic.
Q: How do I retry a message?
Answer: 1. generate end retry: producer. setRetryTimesWhenSendFailed (3); producer. send (msg, 1000); the first two lines of code can achieve the production end retry, meaning that if the message sending exceeds the timeout (1000), resend three times
2. Consumption end retry: there are two types: one is timeout, but exception
Timeout indicates that MQ does not receive the return effect (RECONSUME_LATER/CONSUME_SUCCESS) from the consumer, which means MQ will always send messages to the consumer for retry.
Exception indicates that the consumer encounters an exception during the consumption process and returns the RECONSUME_LATER to MQ. This indicates that MQ will send messages to the consumer for retry according to certain rules.
Default retry rules: messageDelayLevel = 1 s 5S 10 s 30 s 1 m 2 m 3 m 4 m 5 m 6 m 7 m 8 m 9 9 m 10 m 20 m 30 m 1 h 2 h
Sample Code:
/*** Producer, send the message **/public class Producer {public static void main (String [] args) throws MQClientException, interruptedException {defamqmqproducer producer = new defamqmqproducer ("quickstart_producer"); producer. setNamesrvAddr ("192.168.32.135: 9876; 92.168.32.136: 9876"); producer. setRetryTimesWhenSendFailed (3); producer. start (); for (int I = 0; I <10; I ++) {try {Message msg = new Message ("TopicTest", // topic "TagA ", // tag ("Hello RocketMQ" + I ). getBytes () // body); SendResult sendResult = producer. send (msg, 1000); System. out. println (sendResult);} catch (Exception e) {e. printStackTrace (); Thread. sleep (1000) ;}} producer. shutdown () ;}/ *** Consumer, subscribe to messages */public class Consumer {public static void main (String [] args) throws InterruptedException, MQClientException {defamqmqpushconsumer consumer = new DefaultMQPushConsumer ("quickstart_consumer"); consumer. setNamesrvAddr ("192.168.32.135: 9876; 192.168.32.136: 9876 "); /*** set whether to start consumption from the queue header or tail when the Consumer is started for the first time. <br> * if it is not started for the first time, continue consumption according to the position of the previous consumption */consumer. setConsumeFromWhere (ConsumeFromWhere. CONSUME_FROM_FIRST_OFFSET); consumer. subscribe ("TopicTest", "*"); // The default value is 1 consumer. setConsumeMessageBatchMaxSize (10); consumer. registerMessageListener (new MessageListenerConcurrently () {@ Override public ConsumeConcurrentlyStatus consumeMessage (List <MessageExt> msgs, ConsumeConcurrentlyContext context) {// System. out. println (Thread. currentThread (). getName () + "Receive New Messages:" + msgs); try {System. out. println ("number of messages:" + msgs. size (); for (MessageExt msg: msgs) {String topic = msg. getTopic (); String msgBody = new String (msg. getBody (), "UTF-8"); String tags = msg. getTags (); System. out. println ("received message:" + "topic:" + topic + "msgBody:" + msgBody + "tags:" + tags); if ("Hello RocketMQ 4 ". equals (msgBody) {System. out. println ("===== failed message start ===="); System. out. println (msg); System. out. println (msgBody); System. out. println ("===== failed message end ===="); int I = 6/0 ;}} catch (UnsupportedEncodingException e) {e. printStackTrace (); return ConsumeConcurrentlyStatus. RECONSUME_LATER;} return ConsumeConcurrentlyStatus. CONSUME_SUCCESS ;}}); consumer. start (); System. out. println ("Consumer Started. ");}}View Code