title: 简单示例 date: 2017/12/18

categories: 文档翻译

Simple Message Example

Use RocketMQ to send messages in three ways: reliable synchronous, reliable asynchronous, and one-way transmission.

简单的消息示例

使用RocketMQ发送消息的3种方法:可靠同步发送、可靠异步发送和单向发送

This page exemplifies these three message-sending ways. Checkout the notes along with the example to figure out which way to use for your specific use case.

本页文档展示这3种消息发送方式。检出这些带有注释的示例代码,可以让你知道每一个用例对应哪一种发送方式。

Reliable synchronous transmission

Application: Reliable synchronous transmission is used in extensive scenes, such as important notification messages, SMS notification, SMS marketing system, etc..

  1. public class SyncProducer {
  2. public static void main(String[] args) throws Exception {
  3. //Instantiate with a producer group name.
  4. DefaultMQProducer producer = new
  5. DefaultMQProducer("please_rename_unique_group_name");
  6. //Launch the instance.
  7. producer.start();
  8. for (int i = 0; i < 100; i++) {
  9. //Create a message instance, specifying topic, tag and message body.
  10. Message msg = new Message("TopicTest" /* Topic */,
  11. "TagA" /* Tag */,
  12. ("Hello RocketMQ " +
  13. i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
  14. );
  15. //Call send message to deliver message to one of brokers.
  16. SendResult sendResult = producer.send(msg);
  17. System.out.printf("%s%n", sendResult);
  18. }
  19. //Shut down once the producer instance is not longer in use.
  20. producer.shutdown();
  21. }
  22. }

可靠同步发送

应用:可靠同步发送在众多场景中被使用,例如重要的通知消息、短信通知、短信营销系统,等等。

  1. public class SyncProducer {
  2. public static void main(String[] args) throws Exception {
  3. //Instantiate with a producer group name.
  4. DefaultMQProducer producer = new
  5. DefaultMQProducer("please_rename_unique_group_name");
  6. //Launch the instance.
  7. producer.start();
  8. for (int i = 0; i < 100; i++) {
  9. //Create a message instance, specifying topic, tag and message body.
  10. Message msg = new Message("TopicTest" /* Topic */,
  11. "TagA" /* Tag */,
  12. ("Hello RocketMQ " +
  13. i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
  14. );
  15. //Call send message to deliver message to one of brokers.
  16. SendResult sendResult = producer.send(msg);
  17. System.out.printf("%s%n", sendResult);
  18. }
  19. //Shut down once the producer instance is not longer in use.
  20. producer.shutdown();
  21. }
  22. }

Reliable asynchronous transmission

Application: asynchronous transmission is generally used in response time sensitive business scenarios.

  1. public class AsyncProducer {
  2. public static void main(String[] args) throws Exception {
  3. //Instantiate with a producer group name.
  4. DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
  5. //Launch the instance.
  6. producer.start();
  7. producer.setRetryTimesWhenSendAsyncFailed(0);
  8. for (int i = 0; i < 100; i++) {
  9. final int index = i;
  10. //Create a message instance, specifying topic, tag and message body.
  11. Message msg = new Message("TopicTest",
  12. "TagA",
  13. "OrderID188",
  14. "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
  15. producer.send(msg, new SendCallback() {
  16. @Override
  17. public void onSuccess(SendResult sendResult) {
  18. System.out.printf("%-10d OK %s %n", index,
  19. sendResult.getMsgId());
  20. }
  21. @Override
  22. public void onException(Throwable e) {
  23. System.out.printf("%-10d Exception %s %n", index, e);
  24. e.printStackTrace();
  25. }
  26. });
  27. }
  28. //Shut down once the producer instance is not longer in use.
  29. producer.shutdown();
  30. }
  31. }

可靠异步发送

应用:异步发送通常被用于对响应时间敏感的业务场景

  1. public class AsyncProducer {
  2. public static void main(String[] args) throws Exception {
  3. //Instantiate with a producer group name.
  4. DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
  5. //Launch the instance.
  6. producer.start();
  7. producer.setRetryTimesWhenSendAsyncFailed(0);
  8. for (int i = 0; i < 100; i++) {
  9. final int index = i;
  10. //Create a message instance, specifying topic, tag and message body.
  11. Message msg = new Message("TopicTest",
  12. "TagA",
  13. "OrderID188",
  14. "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
  15. producer.send(msg, new SendCallback() {
  16. @Override
  17. public void onSuccess(SendResult sendResult) {
  18. System.out.printf("%-10d OK %s %n", index,
  19. sendResult.getMsgId());
  20. }
  21. @Override
  22. public void onException(Throwable e) {
  23. System.out.printf("%-10d Exception %s %n", index, e);
  24. e.printStackTrace();
  25. }
  26. });
  27. }
  28. //Shut down once the producer instance is not longer in use.
  29. producer.shutdown();
  30. }
  31. }

One-way transmission

Application: One-way transmission is used for cases requiring moderate reliability, such as log collection.

  1. public class OnewayProducer {
  2. public static void main(String[] args) throws Exception{
  3. //Instantiate with a producer group name.
  4. DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
  5. //Launch the instance.
  6. producer.start();
  7. for (int i = 0; i < 100; i++) {
  8. //Create a message instance, specifying topic, tag and message body.
  9. Message msg = new Message("TopicTest" /* Topic */,
  10. "TagA" /* Tag */,
  11. ("Hello RocketMQ " +
  12. i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
  13. );
  14. //Call send message to deliver message to one of brokers.
  15. producer.sendOneway(msg);
  16. }
  17. //Shut down once the producer instance is not longer in use.
  18. producer.shutdown();
  19. }
  20. }

单向发送

应用:单向发送用于要求一定可靠性的场景,例如日志收集。

  1. public class OnewayProducer {
  2. public static void main(String[] args) throws Exception{
  3. //Instantiate with a producer group name.
  4. DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
  5. //Launch the instance.
  6. producer.start();
  7. for (int i = 0; i < 100; i++) {
  8. //Create a message instance, specifying topic, tag and message body.
  9. Message msg = new Message("TopicTest" /* Topic */,
  10. "TagA" /* Tag */,
  11. ("Hello RocketMQ " +
  12. i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
  13. );
  14. //Call send message to deliver message to one of brokers.
  15. producer.sendOneway(msg);
  16. }
  17. //Shut down once the producer instance is not longer in use.
  18. producer.shutdown();
  19. }
  20. }