当前位置: 首页 > >

RocketMQ入门篇-三分钟入门RocketMQ

发布时间:


微信公众号:Java患者
专注Java领域技术分享



RocketMQ入门Demo

首先在window中安装好RocketMQ


依赖


????org.apache.rocketmq
????rocketmq-client
????4.3.0


生产者

package?com.zero.day1;

import?org.apache.rocketmq.client.producer.DefaultMQProducer;
import?org.apache.rocketmq.client.producer.SendResult;
import?org.apache.rocketmq.common.message.Message;
import?org.apache.rocketmq.remoting.common.RemotingHelper;

public?class?SyncProducerDemo?{

????public?static?void?main(String[]?args)?{
????????DefaultMQProducer?producer?=?null;
????????try?{
????????????producer?=?new?DefaultMQProducer("TopicTst");
????????????producer.setNamesrvAddr("127.0.0.1:9876");

????????????producer.start();

????????????for?(int?i?=?0;?i?????????????????Message?msg?=?new?Message("TopicTst",?"TagA",?"Hello?RocketMQ".getBytes(RemotingHelper.DEFAULT_CHARSET));
????????????????SendResult?sendResult?=?producer.send(msg);
????????????????System.out.println("返回内容"?+?sendResult);

????????????}
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}finally?{
????????????producer.shutdown();
????????}

????}
}

消费者

package?com.zero.day1;

import?org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import?org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import?org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import?org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import?org.apache.rocketmq.common.message.MessageExt;

import?java.util.List;

public?class?ConsumerDemo?{

????public?static?void?main(String[]?args)?{
????????try{
????????????DefaultMQPushConsumer?consumer?=?new?DefaultMQPushConsumer("TopicTst");
????????????consumer.setNamesrvAddr("127.0.0.1:9876");

????????????consumer.subscribe("TopicTst",?"*");
????????????consumer.registerMessageListener(new?MessageListenerConcurrently()?{
????????????????public?ConsumeConcurrentlyStatus?consumeMessage(List?list,?ConsumeConcurrentlyContext?consumeConcurrentlyContext)?{
????????????????????System.out.println("Receive:?"?+?list);
????????????????????return?ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
????????????????}
????????????});
????????????consumer.start();
????????}catch?(Exception?e)?{
????????????e.printStackTrace();
????????}


????}
}

启动











可能出现的错误

org.apache.rocketmq.client.exception.MQClientException:?No?route?info?of?this?topic,?TopicTest

自动创建Topic失败了,改成手动创建topic


mqadmin?updateTopic?-b?127.0.0.1:10911?-n?127.0.0.1:9876?-t?TopicTest

或者改成自动创建Topic


Window:?mqbroker.cmd?-n?localhost:9876?autoCreateTopicEnable=true
Linux:nohup?sh?mqbroker?-n?192.168.180.133:9876?autoCreateTopicEnable=true

插件部署
安装

在window安装RocketMQ插件


首先git下载文件 地址:https://github.com/apache/rocketmq-externals.git







配置

下载之后 今日rocketmq-consolesrcmain esource 修改application.properties文件


编译启动

进入rocketmq-console


执行


mvn?clean?package?-Dmaven.test.skip=true

启动后台

在target目录下 执行


java?-jar?rocketmq-console-ng-1.0.0.jar

启动浏览器 输入127.0.0.1:8001



专注分享Java技术,跟我一起学*吧


长按识别二维码关注



往期精彩
RocketMQ入门篇-安装


ActiveMQ高级篇-联合JMS详解


ActiveMQ进阶篇-整合Spring


ActiveMQ入门篇



友情链接: