1. 引入依赖
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.6.5.RELEASE</version>
</dependency>
</dependencies>
2.编写rabbitmq.properties
mq.host=192.168.240.135(这里改成你)
mq.username=guest
mq.password=guest
mq.port=5672
3.编写spring-rabbitmq.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:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:rabbitmq/rabbitmq.properties"/>
<rabbit:connection-factory id="cachingConnectionFactory"
username="${mq.username}" password="${mq.password}" host="${mq.host}" port="${mq.port}"
virtual-host="/" />
<rabbit:template id="rabbitTemplate" connection-factory="cachingConnectionFactory"></rabbit:template>
<rabbit:admin connection-factory="cachingConnectionFactory" />
<rabbit:queue durable="true" id="rabbitQueue" name="teskinfly.xia.email.queue" auto-delete="false"></rabbit:queue>
<rabbit:direct-exchange name="teskinfly.xia.exch" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="teskinfly.xia.email.queue" key="teskinfly.user.email.key"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<!-- 消息接收-->
<bean id="emailSender" class="com.teskinfly.utils.email.EmailSender"></bean>
<rabbit:listener-container connection-factory="cachingConnectionFactory" acknowledge="auto">
<rabbit:listener ref="emailSender" queues="teskinfly.xia.email.queue"></rabbit:listener>
</rabbit:listener-container>
</beans>
4.模拟测试一下
- 发送端
@Service
public class UserService implements IUserService {
@Autowired
IUserDao userDao;
@Autowired
RabbitTemplate rabbitTemplate;
@Override
public void addUser(User user) {
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME,user);
// userDao.addUser(user);
}
}
- 接收端
public class EmailSender implements ChannelAwareMessageListener {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
System.out.println("接收到了message----");
User user = ((User) new Jackson2JsonMessageConverter().fromMessage(message));
System.out.println(user);
}
}
- 补充一下MailConstants
public class MailConstants {
public static final Integer DELIVERING = 0;//消息投递中
public static final Integer SUCCESS = 1;//消息投递成功
public static final Integer FAILURE = 2;//消息投递失败
public static final Integer MAX_TRY_COUNT = 3;//最大重试次数
public static final Integer MSG_TIMEOUT = 1;//消息超时时间
public static final String MAIL_QUEUE_NAME = "teskinfly.xia.email.queue";
public static final String MAIL_EXCHANGE_NAME = "teskinfly.xia.exch";
public static final String MAIL_ROUTING_KEY_NAME = "teskinfly.user.email.key";
}
5.显示效果
参考博客
https://my.oschina.net/u/3387320/blog/2876559