博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四、RocketMq简单的消费者和生产者(示例代码)
阅读量:4361 次
发布时间:2019-06-07

本文共 4718 字,大约阅读时间需要 15 分钟。

一、生产者

  使用RocketMQ以三种方式发送消息:可靠的同步,可靠的异步和单向传输。

  (1)同步发送消息(可靠的同步传输,适用于重要的短信通知等)

public class SyncProducer {    public static void main(String[] args) throws Exception {        //Instantiate with a producer group name.        DefaultMQProducer producer = new            DefaultMQProducer("please_rename_unique_group_name");        // Specify name server addresses.        producer.setNamesrvAddr("localhost:9876");        //Launch the instance.        producer.start();        for (int i = 0; i < 100; i++) {            //Create a message instance, specifying topic, tag and message body.            Message msg = new Message("TopicTest" /* Topic */,                "TagA" /* Tag */,                ("Hello RocketMQ " +                    i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */            );            //Call send message to deliver message to one of brokers.            SendResult sendResult = producer.send(msg);            System.out.printf("%s%n", sendResult);        }        //Shut down once the producer instance is not longer in use.        producer.shutdown();    }}

   (2)异步传输通常用于响应时间敏感的业务场景。

public class AsyncProducer {    public static void main(String[] args) throws Exception {        //Instantiate with a producer group name.        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");        // Specify name server addresses.        producer.setNamesrvAddr("localhost:9876");        //Launch the instance.        producer.start();        producer.setRetryTimesWhenSendAsyncFailed(0);        for (int i = 0; i < 100; i++) {                final int index = i;                //Create a message instance, specifying topic, tag and message body.                Message msg = new Message("TopicTest",                    "TagA",                    "OrderID188",                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));                producer.send(msg, new SendCallback() {                    @Override                    public void onSuccess(SendResult sendResult) {                        System.out.printf("%-10d OK %s %n", index,                            sendResult.getMsgId());                    }                    @Override                    public void onException(Throwable e) {                        System.out.printf("%-10d Exception %s %n", index, e);                        e.printStackTrace();                    }                });        }        //Shut down once the producer instance is not longer in use.        producer.shutdown();    }}

  (3)以单向模式发送消息(单向传输用于需要中等可靠性的情况,例如日志收集。)

public class OnewayProducer {    public static void main(String[] args) throws Exception{        //Instantiate with a producer group name.        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");        // Specify name server addresses.        producer.setNamesrvAddr("localhost:9876");        //Launch the instance.        producer.start();        for (int i = 0; i < 100; i++) {            //Create a message instance, specifying topic, tag and message body.            Message msg = new Message("TopicTest" /* Topic */,                "TagA" /* Tag */,                ("Hello RocketMQ " +                    i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */            );            //Call send message to deliver message to one of brokers.            producer.sendOneway(msg);        }        //Shut down once the producer instance is not longer in use.        producer.shutdown();    }}

二、消费者

  

public class Consumer {    public static void main(String[] args) throws InterruptedException, MQClientException {        // Instantiate with specified consumer group name.        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");                 // Specify name server addresses.        consumer.setNamesrvAddr("localhost:9876");                // Subscribe one more more topics to consume.        consumer.subscribe("TopicTest", "*");        // Register callback to execute on arrival of messages fetched from brokers.        consumer.registerMessageListener(new MessageListenerConcurrently() {            @Override            public ConsumeConcurrentlyStatus consumeMessage(List
msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); //Launch the consumer instance. consumer.start(); System.out.printf("Consumer Started.%n"); }}

 

转载于:https://www.cnblogs.com/yangming5423/p/9960023.html

你可能感兴趣的文章
wx.ScrolledWindow wx.PseudoDC
查看>>
莫比乌斯反演
查看>>
【BZOJ】【1041】【HAOI2008】圆周上的点
查看>>
高并发常见面试题
查看>>
java面向对象中的抽象,类与对象
查看>>
Git学习笔记
查看>>
《Java技术》第二次作业计科1501赵健宇
查看>>
判断线段和直线相交 POJ 3304
查看>>
下拉菜单
查看>>
.net中调用exchange服务器发邮件
查看>>
nginx知识问答
查看>>
JS - 跳转页面
查看>>
显示消息提示对话框(WebForm)
查看>>
分享下自己编译 XBMC 的过程(zhuan)
查看>>
selenium3 + python - cookie定位
查看>>
通过百度地图API获取地址经纬度
查看>>
Map接口
查看>>
【NIO】之IO和NIO的区别
查看>>
for+next()实现数组的遍历及while list each 的使用
查看>>
MySQL中查询获取每个班级成绩前三名的学生信息
查看>>