上一篇我们讲到了RabbitMQ的基本使用,在这边博客中,我们将讲到如何将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-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="main/java"/>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:config/spring/service.properties"></property>
</bean>
<rabbit:connection-factory id="connectionFactory"
username="${rabbitmqUserName}" password="${rabbitmqUserPwd}" host="${rabbitmqHost}"
port="5672" />
<rabbit:annotation-driven />
<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="concurrentConsumers" value="3" />
<property name="maxConcurrentConsumers" value="10" />
</bean>
<!-- 定义rabbit template用于数据的接收和发送 -->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"
exchange="myExchange" />
<!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="myQueue" durable="false" auto-delete="false"
exclusive="false">
</rabbit:queue>
<rabbit:topic-exchange name="MQPush" durable="true"
auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="myQueue" pattern="TestTopic" />
</rabbit:bindings>
</rabbit:topic-exchange>
</beans>
创建一个java类,PushAndCoustom,内容如下
package testMQ;
import java.io.UnsupportedEncodingException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PushAndCoustom {
public static void main(String[] args) throws UnsupportedEncodingException {
String msg = "amqp发送消息";
ApplicationContext ctx = new ClassPathXmlApplicationContext("config/spring/spring-rabbitmq.xml");
// 生产者
RabbitTemplate rabbitTemplate = (RabbitTemplate) ctx.getBean("rabbitTemplate");
rabbitTemplate.convertAndSend("MQPush", "", msg);
System.out.println("发送消息---------" + msg);
// 消费者
Message receive = rabbitTemplate.receive("myQueue");
System.out.println("接收消息----------" + new String(receive.getBody(), "utf-8"));
}
}
在这里因为我们的项目不是放在web容器中的,所以不会去加载配置文件,我们得自己去读取配置文件信息,如果是放在web容器中运行,那么就可以更简单,使用如下代码就可以了。
package testMQ;
import java.io.UnsupportedEncodingException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
public class PushAndCoustom {
@Autowired
private static RabbitTemplate rabbitTemplate;
public static void main(String[] args) throws UnsupportedEncodingException {
String msg = "amqp发送消息";
// 生产者
rabbitTemplate.convertAndSend("MQPush", "", msg);
System.out.println("发送消息---------" + msg);
// 消费者
Message receive = rabbitTemplate.receive("myQueue");
System.out.println("接收消息----------" + new String(receive.getBody(), "utf-8"));
}
}
运行main方法。
运行完毕。
谢谢您的赏析,如有什么疑问和写的不好的地方请留言,看到会第一时间回复!下一篇我们将讲到SpringBoot整合RabbitMQ的使用,以及DirectExchange,FanoutExchange,TopicExchange的用法!