rabbitmq-consumer配置
程序员文章站
2022-07-12 13:07:59
...
<?xml version="1.0" encoding="UTF-8"?>
<!-- 异步的线程池,线程池的最在数不能设定太小,不然<rabbit:listener/>/@RabbitListener太多的话,会出现发无法正常消费问题 -->
<task:executor id="taskExecutor" pool-size="4-256"
queue-capacity="128" />
<!-- 定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbit.ip}" port="${rabbit.port}" username="${rabbit.username}"
password="${rabbit.password}" virtual-host="${rabbit.vhost}"
publisher-confirms="true" publisher-returns="true" channel-cache-size="5"
executor="taskExecutor" />
<!-- MQ的管理,包括队列、交换器等 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 定义消息队列 -->
<rabbit:queue name="jtOrderQueue" auto-declare="true" />
<!-- 定义交换机,并且完成队列和交换机的绑定 -->
<rabbit:direct-exchange name="orderExchange"
auto-declare="true">
<rabbit:bindings>
<!-- 前台系统只接收商品更新的消息,key路由key -->
<rabbit:binding queue="jtOrderQueue" key="saveOrder" />
</rabbit:bindings>
</rabbit:direct-exchange>
<bean id="orderConsumer" class="com.jt.order.service.OrderConsumer"></bean>
<!-- 定义监听 -->
<rabbit:listener-container
connection-factory="connectionFactory">
<!-- 监听一个队列,当队列中有消息,就会自动触发类.方法,传递消息就作为方法的参数,根据方法声明的参数强转 -->
<rabbit:listener ref="orderConsumer" method="saveOrder"
queue-names="jtOrderQueue" />
</rabbit:listener-container>