Spring-RabbitMQ簡易テストdemo

3955 ワード

POM.xml構成
<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.amqp.version>1.2.0.RELEASE</spring.amqp.version>
		<cglib.version>2.2.2</cglib.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit</artifactId>
			<version>${spring.amqp.version}</version>
		</dependency>
		<dependency>
			<groupId>org.sonatype.sisu.inject</groupId>
			<artifactId>cglib</artifactId>
			<version>${cglib.version}</version>
		</dependency>
		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>

Springの構成
@Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

    @Bean
    public RabbitAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {

        //RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());

        return amqpAdmin().getRabbitTemplate();
    }

Junitテスト:
private final static String EXCHANGE_NAME = "fanout_test";

    private final static String ROUTE_KEY     = "route_k";

    private final static String QUEUE_NAME    = "spring_queue";

    RabbitTemplate              template;

    RabbitAdmin                 admin;

    @Before
    public void before() {
        ApplicationContext context = new AnnotationConfigApplicationContext(
            RabbitConfiguration.class);
        admin = context.getBean(RabbitAdmin.class);
        template = context.getBean(RabbitTemplate.class);

        
       //admin.deleteExchange(EXCHANGE_NAME);
       //admin.deleteQueue(QUEUE_NAME);
        
        //
        DirectExchange directExchange = new DirectExchange(EXCHANGE_NAME);
        admin.declareExchange(directExchange);

        //
        Queue queue = new Queue(QUEUE_NAME);
        admin.declareQueue(queue);

        template.setExchange(EXCHANGE_NAME);

    }

    @Test
    public void send() {
        DirectExchange directExchange = new DirectExchange(EXCHANGE_NAME);
        admin.declareExchange(directExchange);

        //
        Queue queue = new Queue(QUEUE_NAME);
        admin.declareQueue(queue);
        Binding binding = BindingBuilder.bind(queue).to(directExchange).with(ROUTE_KEY);

        admin.declareBinding(binding);
        template.convertAndSend(ROUTE_KEY, "foo");
    }

    @Test
    public void receive() {


       String message = (String) template.receiveAndConvert(QUEUE_NAME);
        System.out.println("message:" + message);
    }

参照先:http://docs.spring.io/spring-amqp/reference/html/