Retry Interval | Maximum Number of Retries |
5 minutes | You can customize the maximum number of retries by modifying the consumer group configuration. The default is 16. |
// Get the clientMQClient mqClient = new MQClient(endpoint, accessKey, secretKey);// Get the topic producerMQProducer producer = mqClient.getProducer(namespace, topicName);
Parameter | Description |
endpoint | Get the cluster access address from the access information module on the console cluster basic information page. ![]() |
accessKey | Role key, copied from the AccessKey column on the Cluster Permission page in the console. ![]() |
secretKey | Role name, copied from the SecretKey column on the Cluster Permission page in the console. |
namespace | Copy the namespace name from the namespace page on the console. If you use a 4.x generic cluster or 5.x cluster, fill in the Cluster ID here. ![]() |
topicName | Copy the topic name from the Topic 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. |
// Get the clientMQClient mqClient = new MQClient(endpoint, accessKey, secretKey);// Get the topic consumerMQConsumer consumer = mqClient.getConsumer(namespace, topicName, groupName, "TAG");
Parameter | Description |
endpoint | Get the cluster access address from the access information module on the console cluster basic information page. ![]() |
accessKey | Role key, copied from the AccessKey column on the Cluster Permission page in the console. ![]() |
secretKey | Role name, copied from the SecretKey column on the Cluster Permission page in the console. |
namespace | Copy the namespace name from the namespace page on the console. If you use a 4.x generic cluster or 5.x cluster, fill in the Cluster ID here. ![]() |
topicName | Copy the topic name from the Topic page in the console. |
groupName | Copy the consumer group name from the Group Management page in the console. ![]() |
do {List<Message> messages = null;try {// long polling of consumption messages// Long polling means that if the topic has no messages, the request will wait on the server for 3 seconds. If there is a message that can be consumed within 3 seconds, it will return immediately.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 | The number of messages pulled at a time. Maximum value: 16. |
waitSeconds | The polling waiting time for a message pull. Maximum value: 30 seconds. |
Feedback