下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、springboot开发案例之整合activemq实现秒杀队列前言 在实际生产环境中中,通常生产者和消费者会是两个自立的应用,这样才干通过消息队列实现了服务解耦和广播。由于此项目仅是一个案例,为了便利期间,生产和消费定义在了同一个项目中。 基础配置 pom.xml 添加依靠: org.springframework.boot spring-boot-starter-activemq perties 基础配置: activemq 基础配置 spring.activemq.broker-url=tcp:/09:61616 生产环境设置密码 spr
2、ing.activemq.user=admin spring.activemq.password=123456 spring.activemq.in-memory=true spring.activemq.pool.enabled=false 项目集成 定义生产者: import javax.jms.destination; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jms.core.jmsmessagingtemplate; import org.spri
3、ngframework.stereotype.component; component public class activemqsender autowired private jmsmessagingtemplate jmstemplate; /* * 发送消息,destination是发送到的队列,message是待发送的消息 */ public void sendchannelmess(destination destination, final string message) jmstemplate.convertandsend(destination, message); 定义消费
4、者: import org.springframework.beans.factory.annotation.autowired; import org.springframework.jms.annotation.jmslistener; import org.springframework.stereotype.service; import mon.entity.result; import mon.enums.seckillstatenum; import mon.redis.redisutil; import mon.websocket.websocketserver; import
5、 com.itstyle.seckill.service.iseckillservice; service public class activemqconsumer autowired private iseckillservice seckillservice; autowired private redisutil redisutil; / 用法jmslistener配置消费者监听的队列,其中text是接收到的消息 jmslistener(destination = "seckill.queue") public void receivequeue(s
6、tring message) /收到通道的消息之后执行秒杀操作(超卖) string array = message.split(""); result result = seckillservice.startseckildbpcc_two(long.parselong(array0), long.parselong(array1); if(result.equals(result.ok(seckillstatenum.success) websocketserver.sendinfo(array0.tostring(), "秒杀胜利&a
7、mp;quot;);/推送给前台 else websocketserver.sendinfo(array0.tostring(), "秒杀失败");/推送给前台 redisutil.cachevalue(array0, "ok");/秒杀结束 测试案例: apioperation(value="秒杀五(activemq分布式队列)",nickname="科帮网") postmapping("/startactivemqqueue&am
8、p;quot;) public result startactivemqqueue(long seckillid) seckillservice.deleteseckill(seckillid); final long killid = seckillid; logger.info("开头秒杀五"); for(int i=0;i<1000;i+) final long userid = i; runnable task = new runnable() override public void run() if(redisutil.getvalue(k
9、illid+"")=null) destination destination = new activemqqueue("seckill.queue"); /思量如何返回给用户信息ws activemqsender.sendchannelmess(destination,killid+""+userid); else /秒杀结束 ; executor.execute(task); try thread.sleep(10000); redisutil.cachevalue(killid+"", null); long seckillcount = seckillservice.getseckillcount(seckillid); logger.info("一共秒杀出件商品",seckillcount); catch (interruptedexception e) e.printstacktrace(); return result.ok(); 注重事项 spring-boot-starter-activemq 依靠即可默认采纳内嵌的 active
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年滁州天长市人民医院招聘调研笔试备考题库及答案解析
- 2026年上海工程技术大学高职单招职业适应性测试备考试题及答案详细解析
- 2026江西中电新材料科技有限公司诚聘英才笔试备考题库及答案解析
- 2026年新疆农业职业技术学院单招职业技能考试参考题库含详细答案解析
- 2026年青海高等职业技术学院高职单招职业适应性测试备考题库及答案详细解析
- 2026江西南昌市十字街学校招聘派遣制教师笔试备考题库及答案解析
- 2026福建莆田市涵江区选聘区属一级国有企业高级管理人员2人笔试备考试题及答案解析
- 2026广东佛山市禅城区海口小学招聘临聘教师1人笔试备考题库及答案解析
- 2026年喀什大学高层次人才引进(130人)笔试备考试题及答案解析
- 2026福建泉州南安市美林中心幼儿园招聘2人笔试备考试题及答案解析
- 2026年上海市宝山区初三上学期一模化学试卷和答案及评分标准
- 内蒙古赤峰市松山区2025-2026学年高一上学期期末数学试题(含答案)
- 2026年官方标准版离婚协议书
- 二十届中纪委五次全会知识测试题及答案解析
- 黑龙江大庆市2026届高三年级第二次教学质量检测化学(含答案)
- 未来五年造纸及纸制品企业数字化转型与智慧升级战略分析研究报告
- 2025年贵州省高考化学试卷真题(含答案及解析)
- 紧固件 弹簧垫圈 标准型(2025版)
- 2025年数字印刷技术应用项目可行性研究报告
- 2024年第41届全国中学生竞赛预赛物理试题(解析版)
- 民间借贷合同规范示范文本
评论
0/150
提交评论