RocketMQ Client 3.6.2.final framework Frequently Asked Questions (Frequently Asked Questions About Rocketmq Client 3.6.2.final Framework)
RocketMQ Client 3.6.2.final Frequency Frequently Asked Questions Answers
The following is a common question of common questions about RocketMQ Client 3.6.2.final framework:
Question 1: How to create a product sending message?
To create a product message, first of all, you need to create a DEFAULTMQPRODUCER object, and set attributes such as the Producer group name, the nameServer address, and message delivery time.Then call the Start method to start the Producer.Next, you can send messages to the specified Topic by send method.
The following is an example code:
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.producer.SendResult;
public class ProducerExample {
public static void main(String[] args) throws Exception {
// Instinct a product object
DefaultMQProducer producer = new DefaultMQProducer("producer_group");
// Set the nameServer address
producer.setNamesrvAddr("127.0.0.1:9876");
// Start the Produceer instance
producer.start();
// Create messages
Message message = new Message("topic", "tag", "Hello RocketMQ".getBytes());
// Send messages and receive the results of sending results
SendResult result = producer.send(message);
// Output sending results
System.out.println ("Send results:" + Result.Getsendstatus ());
// Close the Producer instance
producer.shutdown();
}
}
Question 2: How to create a consumer consumer message?
To create a consumer consumption message, you first need to create a DefaultMQPushConsume object, and set attributes such as the Consumer group name, NameServer address, and message consumption mode.Next, register a message monitor and implement the Consumemessage method processing message.Finally, call the start method to start the consumer.
The following is an example code:
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
public class ConsumerExample {
public static void main(String[] args) throws Exception {
// Example a Consumer object
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_group");
// Set the nameServer address
consumer.setNamesrvAddr("127.0.0.1:9876");
// Set the message consumption mode as the cluster mode (the default cluster mode)
consumer.setMessageModel(MessageModel.CLUSTERING);
// Register message monitor
consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
for (MessageExt message : msgs) {
// Treat the message
System.out.println ("Receive messages:" + new string (message.getBody ()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
// Start Consumer instance
consumer.start();
// Waiting program exit
Thread.sleep(60000);
// Close the consumer instance
consumer.shutdown();
}
}
The above is an answer to the common questions about RocketMQ Client 3.6.2.final framework and Java code example.If necessary, adjust and expand according to specific needs.