Spring web集成rabbitmq代码实例

网友投稿 416 2023-06-29

Spring web集成rabbitmq代码实例

Spring web集成rabbitmq代码实例

这篇文章主要介绍了Spring web集成rabbitmq代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

引入java包:

本项目中仅引入了四个java包:amqp-client-5.7.3.jar,spring-rabbit-2.2.2.RELEASE.jar,spring-retry-1.2.4.RELEASE.jar,spring-amqp-2.2.2.RELEASE.jar

spring-rabbitmq.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://springframework.org/schema/rabbit"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/rabbit http://springframework.org/schema/rabbit/spring-rabbit.xsd">

username="guest" password="guest" host="localhost" port="5672" />

exchange="exchangeTest" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://springframework.org/schema/rabbit"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/rabbit http://springframework.org/schema/rabbit/spring-rabbit.xsd">

username="guest" password="guest" host="localhost" port="5672" />

exchange="exchangeTest" />

username="guest" password="guest" host="localhost" port="5672" />

exchange="exchangeTest" />

exchange="exchangeTest" />

spring中需要引入这个xml, 主要在总spring.xml。或者web.xml中需要引入下。

RabbitMqMessageConsumer

package club.codeapes.web.core.rabbitmq;

import club.codeapes.common.date.DateUtil;

import com.alibaba.fastjson.JSON;

import org.springframework.amqp.core.Message;

import org.springframework.amqp.core.MessageListener;

public class RabbitMqMessageConsumer implements MessageListener {

@Override

public void onMessage(Message message) {

System.out.println("消费信息," + DateUtil.getNow("yyyy-MM-dd HH:mm:ss") + "---->" + message);

}

}

RabbitMqMessageProducer:

package club.codeapes.web.core.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

@Repository

public class RabbitMqMessageProducer{

@Autowired

private AmqpTemplate amqpTemplate;

public void sendMessage(Object message) {

System.out.println("to send message:" + message);

amqpTehttp://mplate.convertAndSend("queueTestKey", message);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot+vue部署按照及运行方法
下一篇:在IDEA启动多个Spring Boot工程实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~