一、消息产生、发送

 public class Producer {
public static void main(String[] args) throws MQClientException {
  DefaultMQProducer producer = new DefaultMQProducer("rmq-group");
  producer.setNamesrvAddr("172.18.4.114:9876");
  producer.setInstanceName("producer");
  producer.start();
  try {
    for (int i = ; i < ; i++) {
    Thread.sleep(); //每5秒发送一次MQ
    Message msg = new Message("TopicA-test",// topic
      "TagA",// tag
      (new Date() + " Hello RocketMQ ,QuickStart" + i)
      .getBytes()// body
      );
    SendResult sendResult = producer.send(msg);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  producer.shutdown();
  }
}

二、消息接收、消费

 import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt; import java.util.List; public class Consumer {
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("rmq-group"); consumer.setNamesrvAddr("172.18.4.114:9876");
consumer.setInstanceName("consumer");
consumer.subscribe("TopicA-test", "TagA"); consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(
List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
System.out.println(new String(msg.getTopic()));
System.out.println(new String(msg.getTags()));
System.out.println("=== " + new String(msg.getBody()));
} return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started.");
}
}

(二)RocketMq入门之消息发送和接收的更多相关文章

  1. RocketMQ(九):消息发送(续)

    匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...

  2. RabbitMQ 简单的消息发送与接收

    RabbitMQ是建立在AMQP(Advanced Message Queuing Protocol,高级消息队列协议)基础上的,而AMQP是建立在TCP协议之上的. 因此,RabbitMQ是需要建立 ...

  3. 整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收& ...

  4. RocketMQ(八):消息发送

    匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...

  5. Java微信公众平台开发(二)--微信服务器post消息体的接收

    转自: http://www.cuiyongzhi.com/post/39.html 在上一篇的文章中我们详细讲述了如何将我们的应用服务器和微信腾讯服务器之间的对接操作,最后接入成功,不知道你有没有发 ...

  6. DICOM医学图像处理:DIMSE消息发送与接收“大同小异”之DCMTK fo-dicom mDCM

    背景: 从DICOM网络传输一文开始,相继介绍了C-ECHO.C-FIND.C-STORE.C-MOVE等DIMSE-C服务的简单实现,博文中的代码给出的实例都是基于fo-dicom库来实现的,原因只 ...

  7. TeamTalk Android代码分析(业务流程篇)---消息发送和接收的整体逻辑说明

    第一次纪录东西,也没有特别的顺序,想到哪里就随手画了一下,后续会继续整理- 6.2消息页面动作流程 6.2.1 消息页面初始化的总体思路 1.页面数据的填充更新直接由页面主线程从本地数据库请求 2.数 ...

  8. 【RocketMQ】MQ消息发送

    消息发送 首先来看一个RcoketMQ发送消息的例子: @Service public class MQService { @Autowired DefaultMQProducer defaultMQ ...

  9. C#TCPClient应用-一个简单的消息发送和接收

    TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...

随机推荐

  1. PHP json_encode转换空数组为对象

    问题描述: php返回json格式的数据,当返回数据的为数组,且key为字符串时,json化后将返回jsonObject,但是如果是空数组,有可能返回的就是jsonArray,数据结构不一致导致端解析 ...

  2. Oracle基础重点概要

    表空间                                                                 逻辑上处于数据库之下,利用表空间可以更灵活地规划数据库结构. 创 ...

  3. 【LeetCode】Path Sum II 二叉树递归

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. matlab中将矩阵按照行打乱顺序的一个例子

    来自百度百科: A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12] rowrank = randperm(size(A, 1)); % 随机打乱的数字,从1~行数打 ...

  5. 将War发布到Tomcat7上遇到的问题及其解决

    用MyEclipse做了一个app,在其自带的Tomcat里运行正常,做成war后却出现如下错误: [ServletException in:/page/jsp/template/block.jsp] ...

  6. GET 和 POST的区别

    1.最普遍的答案 GET使用URL或Cookie传参.而POST将数据放在BODY中. GET的URL会有长度上的限制,则POST的数据则可以非常大. POST比GET安全,因为数据在地址栏上不可见. ...

  7. IDEA+Gradle相关资料

    要注意下载的版本,不能过于陈旧,否则会报错,导致无法正常配置. 具体配置参考:https://my.oschina.net/u/1994816/blog/297967 IDEA下载地址:https:/ ...

  8. window中findstr命令的用法

    http://www.netingcn.com/window-findstr-command.html findstr是window系统自带的命令,用途是查找指定的一个或多个文件文件中包含(或通过参数 ...

  9. 'htmlentities(): charset `utf8' not supported, assuming utf-8'

    TP5.1框架报错!  Fatal error: Uncaught exception 'think\exception\ErrorException' with message 'htmlentit ...

  10. zookeeper工作原理解析

    zookeeper一般用于distributed locking,并不适合用于distributed storage,由于zookeeper的每一个node.也叫做znode的存储容量限制是1M. z ...