spring rabbitmq整合
由于种种原因,要在项目中使用rabbit mq。
现将springmvc形式的接口换掉。采用rabbitmq队列形式来接收请求。
现将rabbitmq整合入项目中。如果项目时使用maven结构,在项目文件中添加:
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>${spring.amqp.version}</version> </dependency>
如果没有采用maven结构,这需要添加,
下面是spring 整合rabbitmq的配置代码:
rabbit-mq.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> <!-- rabbitMq连接服务配置 --> <rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" port="5672" /> <rabbit:admin connection-factory="connectionFactory" /> <!-- queue 队列声明 --> <rabbit:queue id="mq-queue" durable="true" auto-delete="false" exclusive="false" name="test_queue" /> <!-- exchange queue binging key 绑定 --> <rabbit:direct-exchange id="mq-exchange" name="test_exchange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding queue="mq-queue" key="test_key" /> </rabbit:bindings> </rabbit:direct-exchange> <!-- mq 模板 --> <rabbit:template id="amqpTemplate" exchange="mq-exchange" connection-factory="connectionFactory" /> <bean id="mqClientListener" class="xx.xx.services.mq.MqClientListener"/> <!-- mq 客户端(消费端) --> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 --> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> <rabbit:listener queues="mq-queue" ref="mqClientListener" /> </rabbit:listener-container> </beans>
MqClientListener.java
import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class MqClientListener implements MessageListener{ @Override public void onMessage(Message message) { System.out.println(new String(message.getBody())); } }
相关推荐
zhuxue 2020-10-14
shenzhenzsw 2020-10-09
shyoldboy 2020-09-27
leihui00 2020-09-16
lishijian 2020-08-17
程序员伊成 2020-08-06
ljcsdn 2020-07-27
waitzkj 2020-07-25
powrexly 2020-07-20
liym 2020-07-20
zhoucheng0 2020-07-19
shenzhenzsw 2020-07-18
woaishanguosha 2020-07-18
waitzkj 2020-07-18
zhoucheng0 2020-07-08
json0000 2020-07-04
NVEFLY 2020-07-04
OnMyHeart 2020-07-04