生产者:
MQProducer.java
package com.gdf.example.rabbitmq.helloworld.service;
public interface MQProducer {
void send(String key, Object object);
}
MQProducerImpl.java
package com.gdf.example.rabbitmq.helloworld.service.impl;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gdf.example.rabbitmq.helloworld.service.MQProducer;
@Service
public class MQProducerImpl implements MQProducer {
@Autowired
private AmqpTemplate amqpTemplate;
@Override
public void send(String key, Object object) {
// TODO Auto-generated method stub
amqpTemplate.convertAndSend(key, object);
}
}
消费者:
ConsumerListenter.java
package com.gdf.example.rabbitmq.helloworld.service.impl;
import java.io.UnsupportedEncodingException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class ConsumerListenter implements MessageListener {
@Override
public void onMessage(Message arg0) {
// TODO Auto-generated method stub
String str;
try {
str = new String(arg0.getBody(), "UTF-8");
System.out.println("=============监听【QueueListenter】消息"+arg0);
System.out.print("=====获取消息"+str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
主函数:
HelloMain.java
package com.gdf.example.rabbitmq.helloworld;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-rabbitmq.xml" });
context.start();
HelloService service = (HelloService)context.getBean("helloService");
service.test();
}
}
HelloService.java
package com.gdf.example.rabbitmq.helloworld;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.gdf.example.rabbitmq.helloworld.service.MQProducer;
public class HelloService {
@Autowired
MQProducer mQProducer;
public void test() {
Map<String, Object> msg = new HashMap<String, Object>();
msg.put("hello", "Hello World");
mQProducer.send("mq.hello", msg);
}
}
Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
">
<description>rabbitmq 服务配置</description>
<context:property-placeholder location="classpath:properties/rabbitmq.properties" />
<context:component-scan base-package="com.gdf.example.rabbitmq.helloworld.service.impl" />
<context:annotation-config />
<!-- 连接服务配置 -->
<rabbit:connection-factory id="connectionFactory" host="${mq.rabbit.host}" port="${mq.rabbit.port}"
username="${mq.rabbit.username}" password="${mq.rabbit.password}" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- 定义rabbit template用于数据的接收和发送 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<!--定义queue 说明:durable:是否持久化 exclusive: 仅创建者可以使用的私有队列,断开后自动删除 auto_delete: 当所有消费客户端连接断开后,是否自动删除队列-->
<rabbit:queue id="mq.hello" name="mq.hello" durable="true" auto-delete="false" exclusive="false" />
<!--定义direct-exchange -->
<rabbit:direct-exchange name="mq.helloExchange" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="mq.hello" key="mq.hello.send" ></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<!-- Queue Litener 观察监听模式,当有消息到达时会通知监听在对应的队列上的监听对象 -->
<bean id="CustumerListener" class="com.gdf.example.rabbitmq.helloworld.service.impl.ConsumerListenter" />
<rabbit:listener-container connection-factory="connectionFactory"
acknowledge="manual" concurrency="1" prefetch="1">
<rabbit:listener queues="mq.hello" ref="CustumerListener" />
</rabbit:listener-container>
<bean id="helloService" class="com.gdf.example.rabbitmq.helloworld.HelloService" />
</beans>
pom.xml
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>rabbitmq-client</artifactId>
<version>1.3.0</version>
</dependency>