SpringBoot开发案例之整合ActiveMQ实现秒杀队列_第1页
免费预览已结束,剩余15页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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(&quot;开头秒杀五&quot;); 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+&quot;&quot;)=null) destination destination = new activemqqueue(&quot;seckill.queue&quot;); /思量如何返回给用户信息ws activemqsender.sendchannelmess(destination,killid+&quot;&quot;+userid); else /秒杀结束 ; executor.execute(task); try thread.sleep(10000); redisutil.cachevalue(killid+&quot;&quot;, null); long seckillcount = seckillservice.getseckillcount(seckillid); logger.info(&quot;一共秒杀出件商品&quot;,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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论