之前的博客和大家分享了Rabbitmq的基本框架,及其工作原理,网址为 < http://www.cnblogs.com/jun-ma/p/4840869.html >。今天呢,想和大家一起分享一下如何把rabbitmq应用到我们的Spring工程项目中。

Rabbitmq Server Install & Config

使用Rabbitmq需要我们安装rabbitmq服务器,服务器下载地址 < http://www.rabbitmq.com/download.html >。笔者使用的是Mac OS standalone 3.5.4版本,下载完成之后,解压文件即可。

进入sbin目录,后台启动rabbitmq server

./rabbitmq-server –detached

使用ps -A| grep rabbit可以查看到当前运行的rabbitmq进程。

然后,使用rabbitmq-plugin命令使能web管理:

./rabbitmq-plugins enable rabbitmq_management  

然后,我们就可以使用web界面管理我们的Rabbitmq Server,网址为:

http://localhost:15672/

用户名和密码默认为guest/guest,用户登录之后可以根据需要选择新增用户名和密码,并设置管理权限。

Spring Rabbitmq使用实例

Message Producer

生产者的Rabbitmq.xml配置,这里我们使用rabbit标签来配置rabbitmq客户端。

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      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">

    <!--配置connection-factory,指定连接rabbit server参数-->
    <rabbit:connection-factory id="connectionFactory" username="guest" password="guest"
                               host="localhost"
                               port="5672"
                               virtual-host="/"/>

    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--定义queue-->
    <rabbit:queue id="com.mj.test" name="com.mj.test" durable="true" auto-delete="false" exclusive="false"/>

    <!-- 定义direct exchange,绑定com.mj.test queue -->
    <rabbit:direct-exchange name="myChange" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="com.mj.test" key="hello"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>
    
    <!--定义rabbit template用于数据的接收和发送-->
    <rabbit:template id="myAmqpTemplate" connection-factory="connectionFactory" exchange="myChange"/>
</beans>

ApplicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.1.xsd
			http://www.springframework.org/schema/tx
			http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
			http://www.springframework.org/schema/jdbc
			http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    <!-- 使能AOP-->
    <aop:aspectj-autoproxy/>
    <!-- 自动装载bean使能-->
    <context:component-scan base-package="com.mj.amq"/>
    <context:annotation-config/>

    <!-- 声明一个事物管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 启用annotation的事务支持 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <import resource="Rabbitmq.xml"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3307/student?useUnicode=true&amp;characoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
</beans>

Producer发送数据Java代码

@Service
public class MessageSender {

    @Resource(name="myAmqpTemplate")
    AmqpTemplate amqpTemplate;

    public void sendMessage(Object message){
        amqpTemplate.convertAndSend("hello",message);
    }
}

Message Consumer

Rabbitmq.xml配置

Rabbitmq接收端需要配置Connection-Factory实例,监听的队列,以及Listener-container。同时,我们需要创建监听队列的java bean。这里我们使用的是spring rabbitmq提供的异步MessageListener接口,consumer的业务逻辑在onMessage中实现。

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      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">


    <rabbit:connection-factory id="connectionFactory" username="guest" password="guest"
                               host="localhost"
                               port="5672"
                               virtual-host="/"/>

    <rabbit:queue id="com.mj.test" name="com.mj.test" durable="true" auto-delete="false" exclusive="false"/>


    <bean id="messageReceiver" class="com.mj.amq.MessageReceiver"></bean>

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener queues="com.mj.test" ref="messageReceiver"/>
    </rabbit:listener-container>

</beans>

ApplicationContext.xml和producer的一致

Consumer侦听mq消息代码

public class MessageReceiver implements MessageListener {

    public void onMessage(Message message) {
        System.out.println(message);
    }

    public static void main(String[]args){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
    }
}

测试

启动MessageReceiver的main函数,运行producer的单元测试,会在MessageReceiver的concole端看到接收都到的数据。同时登陆rabbitmq客户端可以看到我们配置的Exchange和queue信息。


public class MessageSenderTest {

    private ApplicationContext context = null;

    @Before
    public void setUp() throws Exception {
        context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    }

    @Test
    public void should_send_a_amq_message() throws Exception {
        MessageSender messageSender = (MessageSender) context.getBean("messageSender");
        messageSender.sendMessage("Hello, I am amq sender");
    }
}

Conclusion

本文和大家分享了一个Spring rabbitmq代码实例,文中给出了完整的xml配置和java代码,希望能够给大家带来一些帮助。

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部