RabbitMQ(2)——Spring Boot RabbitMQ 整合

参考资料:

http://blog.csdn.net/qq_26562641/article/details/53332302

http://www.cnblogs.com/boshen-hzb/p/6841982.html

整体项目结构:

pom.xml 文件内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>micro</artifactId>
        <groupId>com.sinosoft</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>rabbitmq1</artifactId>
    <packaging>jar</packaging>

    <name>rabbitmq1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 application.yml 文件内容:

spring:
  application:
    name: rabbitmq
---
spring:
  rabbitmq:
    host: 10.28.37.56
    port: 5672
    username: admin
    password: 1234

RabbitmqApplication文件内容:

package com.sinosoft;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by xushuyi on 2017/7/20.
 */
@SpringBootApplication
public class RabbitmqApplication {
    public static void main(String[] args) {
        SpringApplication.run(RabbitmqApplication.class, args);
    }
}

config/Queues.java 文件:

package com.sinosoft.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by xushuyi on 2017/7/20.
 */
@Configuration
public class Queues {

    /**
     * 创建队列 hello.foo
     * @return
     */
    @Bean
    public Queue helloQueue() {
        return new Queue("hello.foo");
    }
}

rabbit/RabbitReceive.java文件:

package com.sinosoft.rabbit;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Created by xushuyi on 2017/7/20.
 */
@Component
public class RabbitReceive {

    /**
     * 接收通道
     *
     * @param i
     */
    @RabbitListener(queues = "hello.foo")
    public void process(String i) {
        System.out.println("执行接收Receiver1  : " + i.toString());
    }
}

rabbit/RabbitSend.java 文件:

package com.sinosoft.rabbit;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by xushuyi on 2017/7/20.
 */
@Component
public class RabbitSend {

    @Autowired
    private AmqpTemplate rabbitTemplate;


    public void send(String val) {
        String context = "hello " + new Date();
        System.out.println("Sender : " + val);
        this.rabbitTemplate.convertAndSend("hello.foo", val);
    }

}

RabbitmqTest.java 文件:

package com.sinosoft;

import com.sinosoft.rabbit.RabbitSend;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Created by xushuyi on 2017/7/20.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqTest {

    @Autowired
    private RabbitSend rabbitSend;

    @Test
    public void send(){
        for (int i = 0; i < 1000; i++) {
            rabbitSend.send(String.valueOf(i));
        }
    }

}

 

 

 

 

 

posted @ 2017-07-20 15:49  xu_shuyi  阅读(145)  评论(0)    收藏  举报