Get the App
SLTechnology News&Howtos  ›  Development  › 

How to integrate ActiveMQ to realize second kill queue in SpringBoot

Shulou Source: shulou.com Published: 2022-06-01 09:37:12 10月09日 Update

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)

Tags: Queue production configuration message consumption integration foundation password article consumer environment project content foreground case producer service test medium proper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple NVidia OPPO Reno Redmi