Release Notes
Announcements
<!-- in your <dependencies> block --><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>4.9.4</version></dependency><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-acl</artifactId><version>4.9.4</version></dependency>
// Instantiate a message producer.DefaultMQProducer producer = new DefaultMQProducer(groupName,new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)) // ACL permissions.);// Set the NameServer address.producer.setNamesrvAddr(nameserver);// Start the producer instance.producer.start();
Parameter | Description |
groupName | Producer group name. It is recommended that the topic name be used. |
accessKey | Role token. You can copy the token 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. |
nameserver | Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console. |
for (int i = 0; i < 10; i++) {// Create a message instance and set the topic and message content.Message msg = new Message(topic_name, "TAG", ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));// Send messages.SendResult sendResult = producer.send(msg);System.out.printf("%s%n", sendResult);}
Parameter | Description |
topic_name | Topic name. You can copy the name from the Topic Management page in the console. 4.x virtual/exclusive cluster: Concatenate the namespace name in the format of full namespace name%topic name, such as MQ_INSTxxx_aaa%TopicTest.4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the topic name. |
TAG | Tags associated with the message. |
// Disable retries on message sending failure.producer.setRetryTimesWhenSendAsyncFailed(0);// Set the number of messages to send.int messageCount = 10;final CountDownLatch countDownLatch = new CountDownLatch(messageCount);for (int i = 0; i < messageCount; i++) {try {final int index = i;// Create a message instance and set the topic and message content.Message msg = new Message(topic_name, "TAG", ("Hello rocketMq " + index).getBytes(RemotingHelper.DEFAULT_CHARSET));producer.send(msg, new SendCallback() {@Overridepublic void onSuccess(SendResult sendResult) {// Logic for successful message sending.countDownLatch.countDown();System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());}@Overridepublic void onException(Throwable e) {// Logic for failed message sending.countDownLatch.countDown();System.out.printf("%-10d Exception %s %n", index, e);e.printStackTrace();}});} catch (Exception e) {e.printStackTrace();}}countDownLatch.await(5, TimeUnit.SECONDS);
Parameter | Description |
topic_name | Topic name. You can copy the name from the Topic Management page in the console. 4.x virtual/exclusive cluster: Concatenate the namespace name in the format of full namespace name%topic name, such as MQ_INSTxxx_aaa%TopicTest.4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the topic name. |
TAG | Tags associated with the message. |
for (int i = 0; i < 10; i++) {// Create a message instance and set the topic and message content.Message msg = new Message(topic_name, "TAG", ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));// Send a one-way message.producer.sendOneway(msg);}
Parameter | Description |
topic_name | Topic name. You can copy the name from the Topic Management page in the console. 4.x virtual/exclusive cluster: Concatenate the namespace name in the format of full namespace name%topic name, such as MQ_INSTxxx_aaa%TopicTest.4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the topic name. |
TAG | Tags associated with the message. |
// Instantiate a consumer.DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(groupName,new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))); // ACL permissions.// Set the NameServer address.pushConsumer.setNamesrvAddr(nameserver);
Parameter | Description |
groupName | Group name. You can copy the name from the Group Management page in the console. 4.x virtual cluster/exclusive cluster: Concatenate the namespace name in the format of full namespace name%group name, such as MQ_INSTxxx_aaa%GroupTest.4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the group name. |
nameserver | Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information 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. |
accessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
// Subscribe to a topic.pushConsumer.subscribe(topic_name, "*");// Register a callback implementation class to handle messages pulled from the broker.pushConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {// Message processing logic.System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);// Mark the message as successfully consumed and return an appropriate status.return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;});// Start the consumer instance.pushConsumer.start();
Parameter | Description |
topic_name | Topic name. You can copy the name from the Topic Management page in the console. 4.x virtual/exclusive cluster: Concatenate the namespace name in the format of full namespace name%topic name, such as MQ_INSTxxx_aaa%TopicTest.4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the topic name. |
"*" | If the subscription expression is null or uses the * wildcard, it indicates subscribing to all messages. It also supports the format "tag1 || tag2 || tag3" to subscribe to multiple types of tags. |
피드백