您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關RabbitMQ中如何進行SSM框架整合xml配置,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
前提:jdk1.8,本博客使用的是RabbitTemplate模版,用封裝好的方法,不再使用
還有一個重點,自己一定要會使用rabbitmq服務器,自己創建exchange、queue等,不然使用該博客的話,會報錯的。
兩種方法:topic模式以及延遲隊列的使用
<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 # 確認方式 MANUAL 手動,AUTO 自動,NONE 自動確認 rabbitmq.acknowledgeMode=MANUAL # 線程池數量 = 并發數 * 監聽數 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"> <!--啟用注解監聽消息--> <rabbit:annotation-driven/> <!-- 配置連接工廠 --> <rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="${rabbitmq.username}" password="${rabbitmq.password}" /> <!-- 定義mq管理 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 聲明隊列 --> <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--> <!-- 定義交換機綁定隊列(通配符模式) #匹配一個或多個詞 *匹配一個詞 --> <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> <!--延遲隊列--> <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> <!-- 消息對象json轉換類 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> <!-- 定義模版 --> <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" /> <!-- 定義消費者 --> <bean name="delayConsumer" class="com.platform.mq.DelayListener" /> <bean name="seckillConsumer" class="com.platform.mq.SeckillHandler" /> <!-- 定義消費者監聽隊列 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="seckillConsumer" queues="queue_seckill" /> <rabbit:listener ref="delayConsumer" queues="immediate" /> </rabbit:listener-container> <!--消息監聽容器,配合注解監聽消息--> <bean id="rabbitListenerContainerFactory" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory"> <property name="connectionFactory" ref="connectionFactory"/> <!--并發消費者數量--> <property name="concurrentConsumers" value="${rabbitmq.concurrentConsumers:3}"/> <!--最大數量--> <property name="maxConcurrentConsumers" value="${rabbitmq.maxConcurrentConsumers:10}"/> <!--消息轉換--> <property name="messageConverter" ref="jsonMessageConverter"/> <!--任務線程池--> <property name="taskExecutor"> <task:executor id="amqpTaskExecutor" pool-size="${rabbitmq.task-executor.pool-size:100}"/> </property> <!--手動確認--> <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延遲隊列 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("延遲隊列時間為:"+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: 秒殺消費者消費消息,監聽執行業務邏輯處理 * @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 { //隊列中繼續執行秒殺 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模式發送數據到消息隊列"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance())); rabbitTemplate.convertAndSend("IExchange","lazy.dtb",mail); } /** * 死信隊列 long等待時間,目前測試為:自動消費 */ @GetMapping(value = "/test8") public void test13(long time) throws IOException { Mail mail = randomMail(); System.out.println("延遲隊列:dlx方式"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance())); rabbitTemplate.convertAndSend("dlx_delay_exchange","dlx_delay_road", mail, message -> { message.getMessageProperties().setExpiration(time + ""); return message; }); } /** * 隨機創建一個Mail實體對象,供接口測試 */ 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中如何進行SSM框架整合xml配置了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。