spriing mvc项目下的Rabbit mq消息配置

1、引用相关依赖,spring-messaging版本尽量高点,3点几的版本一般不支持RabbitMQ

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-messaging</artifactId>
   <version>4.3.11.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework.amqp</groupId>
   <artifactId>spring-rabbit</artifactId>
   <version>1.7.4.RELEASE</version>
</dependency>

2、新建消息体

public class RedbagSendMessage {
    private Long userId;
    private Long redbagId;
    private Integer num;
    //生成get和set方法
}

3、

消息生产者

@Service
public class RabbitmqServiceImpl implements RabbitmqService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
   
    @Override
    public void sendPlanPlusMqData(long userId) {
            RedbagSendMessage message = new RedbagSendMessage();
            message.setUserId(2l);

            rabbitTemplate.convertAndSend(BUY_PLAN_PLUS_FANOUT_EXCHANGE, "", message);
    }
}

4、消息消费类

public class RedbagSendConsumer implements MessageListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedbagSendConsumer.class);
    @Autowired
    private UserManager userManager;

    public void onMessage(Message message) {
        //获取消息内容,转换成对象
        String body = new String(message.getBody(), "utf-8");
        RedbagSendMessage redbagInfo = JSON.parseObject(body, RedbagSendMessage.class);
        //处理业务相关
        User user  = userManager.findById(redbagInfo.getUserId());  
    }
}

5、创建Rabbitmq地址配置文件rabbitmq.properties, host地址替换成生产或者测试地址即可

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

6、创建配置文件spring-rabbitmq.xml,配置队列、绑定交换机。我用的是订阅模式,一个生产者,可以配置多个消费者。更多详情可自行查找。mq也可以持久化,业务用不上,所以没加

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
    <description>rabbitmq</description>

    <!-- 消息体 -->
     <bean id="classMapper" class="org.springframework.amqp.support.converter.DefaultClassMapper">
        <property name="idClassMapping">
            <map>
                <entry key="RedbagSendMessage">
                    <value>com.client.dto.mq.RedbagSendMessage</value>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="rabbitmqJsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">
        <property name="classMapper" ref="classMapper"></property>
    </bean>

    <rabbit:connection-factory id="rabbitmqConnectionFactory"
                               host="${spring.rabbitmq.host}"
                               username="${spring.rabbitmq.username}"
                               password="${spring.rabbitmq.password}"
                               port="${spring.rabbitmq.port}"
                               virtual-host="${spring.rabbitmq.virtual-host}"/>

    <rabbit:admin connection-factory="rabbitmqConnectionFactory"/>

    <rabbit:template id="rabbitTemplate" connection-factory="rabbitmqConnectionFactory" message-converter="rabbitmqJsonMessageConverter"/>

    <!-- 消费队列 -->
    <rabbit:queue name="integral.mall.send.redbag.queue" durable="true" auto-delete="false" exclusive="false" />

    <!-- 交换器 -->
    <rabbit:fanout-exchange id="integralExchangeRedbagFanoutExchange" name="integral:mall:send:redbag:exchange" durable="true" auto-delete="false" >
        <rabbit:bindings>
            <rabbit:binding queue="integral.mall.send.redbag.queue"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:fanout-exchange>

    <!-- 消息处理类 -->
    <bean id="integralExchangeRedbagReceiver" class="com.caikee.hometimetask.consumer.RedbagSendConsumer" ></bean>

    <!-- 监听 -->
    <rabbit:listener-container connection-factory="rabbitmqConnectionFactory">
        <rabbit:listener queues="integral.mall.send.redbag.queue" ref="integralExchangeRedbagReceiver" />
    </rabbit:listener-container>


</beans>

 

转载于:https://my.oschina.net/u/3833849/blog/1796840

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值