RabbitMq

spring集成rabbitmq

2018-02-01  本文已影响0人  AlberLiu

maven配置

<dependencies>
        <!--  spring 配置  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- spring test  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>


        <!--  rabbitmq -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        
    </dependencies>

rabbitmq.properties

mq.host=192.168.245.128
mq.username=admin
mq.password=admin123
mq.port=5672
mq.vhost=im_vhost

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: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/rabbit http:
//www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <!-- 标准配置 -->
                <value>classpath:rabbitmq.properties</value>
            </list>
        </property>
    </bean>

    <!-- 连接工厂-->
    <!--virtual-host="${mq.vhost}"-->
    <rabbit:connection-factory 
    id="connectionFactory" 
    host="${mq.host}" 
    username="${mq.username}" 
    password="${mq.password}" 
    port="${mq.port}" />
    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- spring template声明-->
    <rabbit:template exchange="exchangeOne" id="amqpTemplate"  connection-factory="connectionFactory"   />

    <!--
    说明:
    durable:是否持久化
    exclusive: 仅创建者可以使用的私有队列,断开后自动删除
    auto_delete: 当所有消费客户端连接断开后,是否自动删除队列
    -->
    <rabbit:queue id="queryOne" name="queryOne" durable="true" auto-delete="false" exclusive="false" />

    <!--
    说明:
    rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。
    rabbit:binding:设置消息queue匹配的key
    -->
    <rabbit:direct-exchange name="exchangeOne" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="queryOne" key="queryOneKey" ></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <bean id="queueListenter" class="rabbitmq.QueueListenter"/>

    <!--
    说明:
    queues:监听的队列,多个的话用逗号(,)分隔
    ref:监听器
    -->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
        <rabbit:listener queues="queryOne" ref="queueListenter"/>
    </rabbit:listener-container>
</beans>

QueueListenter.java

package rabbitmq;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

/**
 * Created by alber on 2017/3/25.
 */
public class QueueListenter implements ChannelAwareMessageListener {

    public void onMessage(Message message, Channel channel) throws Exception {
        byte[] bytes=message.getBody();
        System.out.println(message.getBody().toString());
        /*提交*/
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        /*回滚*/
        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
    }
}

BaseTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;

/**
 * Created by alber on 2017/3/25.
 */

@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/applicationContext.xml"})
public class BaseTest {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Test
    public void test() throws IOException {
        byte[] bytes="sdfasdf".getBytes();
        amqpTemplate.convertAndSend("queryOne",bytes);
        System.in.read();
    }
}

上一篇下一篇

猜你喜欢

热点阅读