這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)RabbitMQ中如何進(jìn)行SSM框架整合xml配置,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、越秀網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為越秀等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
前提:jdk1.8,本博客使用的是RabbitTemplate模版,用封裝好的方法,不再使用
還有一個(gè)重點(diǎn),自己一定要會(huì)使用rabbitmq服務(wù)器,自己創(chuàng)建exchange、queue等,不然使用該博客的話,會(huì)報(bào)錯(cuò)的。
兩種方法:topic模式以及延遲隊(duì)列的使用
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.16</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.7.11.RELEASE</version> </dependency>
# rabbitmq 消息配置 rabbitmq.addresses=localhost:5672 rabbitmq.virtual-host=/ rabbitmq.username=guest rabbitmq.password=guest rabbitmq.channel-cache-size=50 rabbitmq.concurrentConsumers=3 rabbitmq.maxConcurrentConsumers=10 # 確認(rèn)方式 MANUAL 手動(dòng),AUTO 自動(dòng),NONE 自動(dòng)確認(rèn) rabbitmq.acknowledgeMode=MANUAL # 線程池?cái)?shù)量 = 并發(fā)數(shù) * 監(jiān)聽數(shù) rabbitmq.task-executor.pool-size=100
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!--啟用注解監(jiān)聽消息--> <rabbit:annotation-driven/> <!-- 配置連接工廠 --> <rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="${rabbitmq.username}" password="${rabbitmq.password}" /> <!-- 定義mq管理 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 聲明隊(duì)列 --> <rabbit:queue name="topicqueue2" auto-declare="false" durable="true"></rabbit:queue> <rabbit:queue name="queue_seckill" auto-declare="false" durable="true"></rabbit:queue> <rabbit:queue name="dlx_delay_queue" auto-declare="false" durable="true"> <rabbit:queue-arguments> <entry key="x-message-ttl" value="6000" value-type="java.lang.Long"/> <entry key="x-dead-letter-exchange" value="dlx_delay_exchange" /> <entry key="x-dead-letter-routing-key" value="immediate_road" /> </rabbit:queue-arguments> </rabbit:queue> <rabbit:queue name="immediate" auto-declare="false" durable="true"> </rabbit:queue> <!--producer--> <!-- 定義交換機(jī)綁定隊(duì)列(通配符模式) #匹配一個(gè)或多個(gè)詞 *匹配一個(gè)詞 --> <rabbit:topic-exchange name="IExchange" id="IExchange"> <rabbit:bindings> <rabbit:binding queue="topicqueue2" pattern="lazy.#"/> <rabbit:binding queue="queue_seckill" pattern="seckill.#"/> </rabbit:bindings> </rabbit:topic-exchange> <!--延遲隊(duì)列--> <rabbit:direct-exchange name="dlx_delay_exchange" durable="true" auto-declare="false"> <rabbit:bindings> <rabbit:binding queue="dlx_delay_queue" key="dlx_delay_road" /> <rabbit:binding queue="immediate" key="immediate_road" /> </rabbit:bindings> </rabbit:direct-exchange> <!-- 消息對(duì)象json轉(zhuǎn)換類 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> <!-- 定義模版 --> <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" /> <!-- 定義消費(fèi)者 --> <bean name="delayConsumer" class="com.platform.mq.DelayListener" /> <bean name="seckillConsumer" class="com.platform.mq.SeckillHandler" /> <!-- 定義消費(fèi)者監(jiān)聽隊(duì)列 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="seckillConsumer" queues="queue_seckill" /> <rabbit:listener ref="delayConsumer" queues="immediate" /> </rabbit:listener-container> <!--消息監(jiān)聽容器,配合注解監(jiān)聽消息--> <bean id="rabbitListenerContainerFactory" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory"> <property name="connectionFactory" ref="connectionFactory"/> <!--并發(fā)消費(fèi)者數(shù)量--> <property name="concurrentConsumers" value="${rabbitmq.concurrentConsumers:3}"/> <!--最大數(shù)量--> <property name="maxConcurrentConsumers" value="${rabbitmq.maxConcurrentConsumers:10}"/> <!--消息轉(zhuǎn)換--> <property name="messageConverter" ref="jsonMessageConverter"/> <!--任務(wù)線程池--> <property name="taskExecutor"> <task:executor id="amqpTaskExecutor" pool-size="${rabbitmq.task-executor.pool-size:100}"/> </property> <!--手動(dòng)確認(rèn)--> <property name="acknowledgeMode" value="MANUAL"/> </bean> </beans>
import cn.hutool.core.date.DateUtil; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; import java.text.DateFormat; import java.util.Date; /** * @Author:MuJiuTian * @Description: RabbitMq延遲隊(duì)列 https://blog.csdn.net/m912595719/article/details/83787486 * ChannelAwareMessageListener(Message memssage,Channel channel) MessageListener(Message message) * @Date: Created in 下午4:17 2019/8/12 */ public class DelayListener implements MessageListener { @Autowired RabbitTemplate rabbitTemplate; private static final ObjectMapper MAPPER = new ObjectMapper(); @Override public void onMessage(Message message) { try { JsonNode jsonData = MAPPER.readTree(message.getBody()); System.out.println("延遲隊(duì)列時(shí)間為:"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance())); } catch (IOException e) { e.printStackTrace(); } } }
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.platform.service.SeckillService; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.beans.factory.annotation.Autowired; /** * @Author:MuJiuTian * @Description: 秒殺消費(fèi)者消費(fèi)消息,監(jiān)聽執(zhí)行業(yè)務(wù)邏輯處理 * @Date: Created in 下午5:01 2019/8/14 */ public class SeckillHandler implements MessageListener { @Autowired SeckillService seckillService; private static final ObjectMapper MAPPER = new ObjectMapper(); @Override public void onMessage(Message message) { try { //隊(duì)列中繼續(xù)執(zhí)行秒殺 JsonNode jsonData = MAPPER.readTree(message.getBody()); String goodsId = jsonData.get("goodsId").asText(); int productId = jsonData.get("productId").asInt(); int userId = jsonData.get("userId").asInt(); int sellerNum = jsonData.get("sellerNum").asInt(); //開始秒殺 seckillService.seckillredis(goodsId,productId,sellerNum,userId); } catch (Exception e) { e.printStackTrace(); } } }
public class Mail implements Serializable { private static final long serialVersionUID = -8140693840257585779L; private String mailId; private String country; private Double weight; public Mail() { } public Mail(String mailId, String country, double weight) { this.mailId = mailId; this.country = country; this.weight = weight; } public String getMailId() { return mailId; } public void setMailId(String mailId) { this.mailId = mailId; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "Mail [mailId=" + mailId + ", country=" + country + ", weight=" + weight + "]"; } }
/** * topic:通配符模式 */ @GetMapping(value = "/test7") public void test11(){ Mail mail = new Mail("21","China",27.2); System.out.println("topic模式發(fā)送數(shù)據(jù)到消息隊(duì)列"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance())); rabbitTemplate.convertAndSend("IExchange","lazy.dtb",mail); } /** * 死信隊(duì)列 long等待時(shí)間,目前測(cè)試為:自動(dòng)消費(fèi) */ @GetMapping(value = "/test8") public void test13(long time) throws IOException { Mail mail = randomMail(); System.out.println("延遲隊(duì)列:dlx方式"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance())); rabbitTemplate.convertAndSend("dlx_delay_exchange","dlx_delay_road", mail, message -> { message.getMessageProperties().setExpiration(time + ""); return message; }); } /** * 隨機(jī)創(chuàng)建一個(gè)Mail實(shí)體對(duì)象,供接口測(cè)試 */ public static Mail randomMail() { Mail mail = new Mail(); mail.setMailId(new Random().nextInt(100)+""); mail.setCountry("China"); mail.setWeight(new Random().nextDouble()); return mail; }
上述就是小編為大家分享的RabbitMQ中如何進(jìn)行SSM框架整合xml配置了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:RabbitMQ中如何進(jìn)行SSM框架整合xml配置
URL標(biāo)題:http://aaarwkj.com/article22/gopojc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、建站公司、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)