Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate ActiveMQ to realize second kill queue in SpringBoot

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how SpringBoot integrates ActiveMQ to achieve the second kill queue". The content is detailed, the steps are clear, and the details are handled properly. I hope that this "how SpringBoot integrates ActiveMQ to achieve the second kill queue" article can help you solve your doubts.

Preface

In the actual production environment, producers and consumers are usually two independent applications, so that service decoupling and broadcasting can be achieved through message queues. Because this project is only a case study, production and consumption are defined in the same project for convenience.

Basic configuration

Pom.xml add dependencies:

Org.springframework.boot

Spring-boot-starter-activemq

Application.properties basic configuration:

# basic activemq configuration

# spring.activemq.broker-url=tcp://47.94.232.109:61616

# set password in production environment

# spring.activemq.user=admin

# spring.activemq.password=123456

# spring.activemq.in-memory=true

# spring.activemq.pool.enabled=false

Project integration

Define the producer:

Import javax.jms.Destination

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.jms.core.JmsMessagingTemplate

Import org.springframework.stereotype.Component

@ Component

Public class ActiveMQSender {

@ Autowired

Private JmsMessagingTemplate jmsTemplate

/ *

* send a message. Destination is the queue to which it is sent, and message is the message to be sent.

, /

Public void sendChannelMess (Destination destination, final String message) {

JmsTemplate.convertAndSend (destination, message)

}

}

Define consumers:

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.jms.annotation.JmsListener

Import org.springframework.stereotype.Service

Import com.itstyle.seckill.common.entity.Result

Import com.itstyle.seckill.common.enums.SeckillStatEnum

Import com.itstyle.seckill.common.redis.RedisUtil

Import com.itstyle.seckill.common.webSocket.WebSocketServer

Import com.itstyle.seckill.service.ISeckillService

@ Service

Public class ActiveMQConsumer {

@ Autowired

Private ISeckillService seckillService

@ Autowired

Private RedisUtil redisUtil

/ / configure queues monitored by consumers using JmsListener, where text is the received message

@ JmsListener (destination = "seckill.queue")

Public void receiveQueue (String message) {

/ / perform a second kill operation after receiving a message from the channel (oversold)

String [] array = message.split (";")

Result result = seckillService.startSeckilDBPCC_TWO (Long.parseLong (array [0]), Long.parseLong (array [1]))

If (result.equals (Result.ok (SeckillStatEnum.SUCCESS) {

WebSocketServer.sendInfo (array [0] .toString (), "successful second kill"); / / push to the front desk

} else {

WebSocketServer.sendInfo (array [0] .toString (), "second kill failed"); / / push to the front desk

RedisUtil.cacheValue (array [0], "ok"); / / the second kill ends

}

}

}

Test case:

@ ApiOperation (value= "second kill five (ActiveMQ distributed queue)", nickname= "science help network")

@ PostMapping ("/ startActiveMQQueue")

Public Result startActiveMQQueue (long seckillId) {

SeckillService.deleteSeckill (seckillId)

Final long killId = seckillId

LOGGER.info ("start killing five")

For (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report