Retry Interval | Maximum Number of Retries |
5 minutes. | You can modify the consumer group configuration to customize the maximum number of retries. The default is 16 times. |
// Obtain the client.MQClient mqClient = new MQClient(endpoint, accessKey, secretKey);// Obtain the producer of the topic.MQProducer producer = mqClient.getProducer(namespace, topicName);
Parameter | Description |
endpoint | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
accessKey | Role token. You can copy it from the AccessKey column on the Cluster Permissions page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
namespace | Namespace name. You can copy the namespace name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter. |
topicName | Topic name. You can copy the name from the Topic Management page in the console. |
try {for (int i = 0; i < 10; i++) {TopicMessage pubMsg;pubMsg = new TopicMessage(("Hello RocketMQ " + i).getBytes(),"TAG");TopicMessage pubResultMsg = producer.publishMessage(pubMsg);System.out.println("Send mq message success. MsgId is: " + pubResultMsg.getMessageId());}} catch (Throwable e) {System.out.println("Send mq message failed.");e.printStackTrace();}
Parameter | Description |
TAG | Set the message tag. |
// Obtain the client.MQClient mqClient = new MQClient(endpoint, accessKey, secretKey);// Obtain the consumer of the topic.MQConsumer consumer = mqClient.getConsumer(namespace, topicName, groupName, "TAG");
Parameter | Description |
endpoint | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
accessKey | Role token. You can copy it from the AccessKey column on the Cluster Permissions page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
namespace | Namespace name. You can copy the namespace name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter. |
topicName | Topic name. You can copy the name from the Topic Management page in the console. |
groupName | Consumer group name. You can copy the name from the Group Management page in the console. |
do {List<Message> messages = null;try {// Consume messages through long round-robin scheduling.// Long round-robin scheduling means that if there are no messages in the topic, the request waits on the server side. If messages are available for consumption, they are returned immediately.// If your business is sensitive to consumption delays, it is highly recommended to pull messages concurrently using multiple threads.messages = consumer.consumeMessage(Integer.parseInt(batchSize),Integer.parseInt(waitSeconds));} catch (Throwable e) {e.printStackTrace();}if (messages == null || messages.isEmpty()) {System.out.println(Thread.currentThread().getName() + ": no new message, continue!");continue;}for (Message message : messages) {System.out.println("Receive message: " + message);}{List<String> handles = new ArrayList<String>();for (Message message : messages) {handles.add(message.getReceiptHandle());}try {consumer.ackMessage(handles);} catch (Throwable e) {if (e instanceof AckMessageException) {AckMessageException errors = (AckMessageException) e;System.out.println("Ack message fail, requestId is:" + errors.getRequestId() + ", fail handles:");if (errors.getErrorMessages() != null) {for (String errorHandle :errors.getErrorMessages().keySet()) {System.out.println("Handle:" + errorHandle + ", ErrorCode:" + errors.getErrorMessages().get(errorHandle).getErrorCode()+ ", ErrorMsg:" + errors.getErrorMessages().get(errorHandle).getErrorMessage());}}continue;}e.printStackTrace();}}} while (true);
Parameter | Description |
batchSize | Number of messages pulled in a single batch. It supports up to 16 messages. |
waitSeconds | Wait time in round-robin scheduling for a single pull request. It supports up to 30 seconds. |
Feedback