当前位置: 首页 > news >正文

Rocketmq java hello world 入门案例

从零手写实现 mq

rocketmq java 入门案例

整体结构

|____src
| |____main
| | |____java
| | | |____com
| | | | |____ryo
| | | | | |____rocket
| | | | | | |____demo
| | | | | | | |____common
| | | | | | | | |____consumer
| | | | | | | | | |____Consumer.java
| | | | | | | | |____productor
| | | | | | | | | |____Productor.java
| | |____resources
| | | |____log4j.properties

maven 依赖

  • pom.xml
<dependencies><!--rocketmq--><dependency><groupId>com.alibaba.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>${rocketmq.version}</version></dependency><dependency><groupId>com.alibaba.rocketmq</groupId><artifactId>rocketmq-common</artifactId><version>${rocketmq.version}</version></dependency><dependency><groupId>com.alibaba.rocketmq</groupId><artifactId>rocketmq-remoting</artifactId><version>${rocketmq.version}</version></dependency><!--log--><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>1.7.7</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
</dependencies>

配置

  • log4j.properties
log4j.rootLogger=warn, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%t] %d{MM-dd HH:mm:ss,SSS} - %m%n

代码

  • Consumer.java
/*** @author houbinbin* @on 17/1/2*/
public class Productor {public static void main(String[] args) {DefaultMQProducer producer = new DefaultMQProducer("Producer");producer.setNamesrvAddr("127.0.0.1:9876");try {producer.start();Message message = new Message("PushTopic", "push", "1", "Just fot test.".getBytes());SendResult result = producer.send(message);System.out.println("id:" + result.getMsgId() + " result:" + result.getSendStatus());message = new Message("PushTopic", "push", "3", "Just fot test.".getBytes());result = producer.send(message);System.out.println("id:" + result.getMsgId() + " result:" + result.getSendStatus());} catch (Exception e) {e.printStackTrace();} finally {producer.shutdown();}}
}

运行LOG如下:

id:C0A8026600002A9F000000000000011A result:SEND_OK
id:C0A8026600002A9F00000000000001A7 result:SEND_OK
  • Consumer.java
/*** @author houbinbin* @on 17/1/4*/
public class Consumer {public static void main(String[] args) {DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("PushConsumer");consumer.setNamesrvAddr("127.0.0.1:9876");try {// 订阅PushTopic下的Tag未push的消息consumer.subscribe("PushTopic", "push");// 程序第一次启动从消息队列头取数据consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);consumer.registerMessageListener(new MessageListenerConcurrently() {@Overridepublic ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {Message message = msgs.get(0);System.err.println(message.toString());return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;}});consumer.start();} catch (Exception e) {e.printStackTrace();}}
}

运行LOG如下:

MessageExt [queueId=1, storeSize=141, queueOffset=1, sysFlag=0, bornTimestamp=1483460812589, bornHost=/192.168.2.102:54835, storeTimestamp=1483460812590, storeHost=/192.168.2.102:10911, msgId=C0A8026600002A9F00000000000001A7, commitLogOffset=423, bodyCRC=1329428386, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=PushTopic, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=2, KEYS=3, WAIT=true, TAGS=push}, body=14]]
MessageExt [queueId=0, storeSize=141, queueOffset=1, sysFlag=0, bornTimestamp=1483460812558, bornHost=/192.168.2.102:54835, storeTimestamp=1483460812575, storeHost=/192.168.2.102:10911, msgId=C0A8026600002A9F000000000000011A, commitLogOffset=282, bodyCRC=1329428386, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=PushTopic, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=2, KEYS=1, WAIT=true, TAGS=push}, body=14]]

小结

整体非常简单,集合官方例子即可。

本文由博客一文多发平台 OpenWrite 发布!

相关文章:

  • LaMa Image Inpainting 图像修复 Onnx Demo
  • 贪心算法(算法竞赛、蓝桥杯)--修理牛棚
  • jmeter接口测试
  • Docker 第十九章 : 阿里云个人镜像仓使用
  • FPGA之带有进位逻辑的加法运算
  • docker单机启动mysql、redis容器命令
  • 2023中国PostgreSQL数据库生态大会:洞察前沿趋势,探索无限可能(附核心PPT资料下载)
  • 【SpringBoot3】统一参数校验
  • MySQL数据库基础知识总结(适合小白入门使用)一
  • Swagger3 使用详解
  • ChatGPT plus 的平替:9个可以联网的免费AI搜索引擎
  • MySQL:快照读和当前读
  • liunx操作系统 进程的基本概念
  • 未来已来:智慧餐饮点餐系统引领餐饮业的数字化转型
  • go mod中如何解决 xxx/yyy/lib@v1.1.0: unrecognized import path
  • [case10]使用RSQL实现端到端的动态查询
  • GitUp, 你不可错过的秀外慧中的git工具
  • IDEA 插件开发入门教程
  • Invalidate和postInvalidate的区别
  • JAVA 学习IO流
  • JS学习笔记——闭包
  • python3 使用 asyncio 代替线程
  • Redis 中的布隆过滤器
  • 大数据与云计算学习:数据分析(二)
  • 回顾2016
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 突破自己的技术思维
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (分类)KNN算法- 参数调优
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (转)为C# Windows服务添加安装程序
  • (转载)hibernate缓存
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • [.NET 即时通信SignalR] 认识SignalR (一)
  • [1204 寻找子串位置] 解题报告
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——
  • [20171101]rman to destination.txt
  • [AIGC] 使用Curl进行网络请求的常见用法
  • [BUUCTF NewStarCTF 2023 公开赛道] week4 crypto/pwn
  • [C#] 如何调用Python脚本程序
  • [Cocoa]iOS 开发者账户,联机调试,发布应用事宜
  • [codevs 1515]跳 【解题报告】