码迷,mamicode.com
首页 > 编程语言 > 详细

springboot初学---rabbitmq的使用

时间:2019-01-24 17:26:42      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:template   文件   auth   str   print   一对多   class   user   ext   

1.jar包依赖

<!-- 配置rabbitMQ -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>1.5.10.RELEASE</version>
</dependency>

2.application.properties配置文件

## 配置rabbitMQ
spring.application.name=spirng-boot-rabbitmq-example

spring.rabbitmq.host=192.168.80.6
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

3.queue模式

<1>.一对一的字符串的简单传输

1).队列配置

/**
 * 队列的配置
 * @author mengjh
 *
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Queue Queue() {
        return new Queue("hello");
    }
}

2).发送者

/**
 * hello的发送者
 * @author Leruan
 *
 */
@Component
public class HelloSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

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

}

3).接受者

/**
 * hello的接受者
 * @author 
 *
 */
@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver  : " + hello);
    }

}

4).测试

/**
 * rabbitmq的hello测试
 * @author
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQHelloTest {
    @Autowired
    private HelloSender helloSender;

    @Test
    public void hello() throws Exception {
        helloSender.send();
    }
}

<2>.一对多和多对多传输

1).在配置队列中添加配置

@Bean
 /**
  * 测试多对多
  */
public Queue neoQueue() {
    return new Queue("neo");
}

2).发送者

@Component
public class NeoSender1 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(int i) {
        String context = "spirng boot neo queue" + " ****** " + i;
        System.out.println("Sender1 : " + context);
        this.rabbitTemplate.convertAndSend("neo", context);
    }

}



@Component
public class NeoSender2 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(int i) {
        String context = "spirng boot neo queue" + " ****** " + i;
        System.out.println("Sender2 : " + context);
        this.rabbitTemplate.convertAndSend("neo", context);
    }

}

 

3).接受者

@Component
@RabbitListener(queues = "neo")
public class NeoReceiver1 {

    @RabbitHandler
    public void process(String neo) {
        System.out.println("Receiver 1: " + neo);
    }

}


@Component
@RabbitListener(queues = "neo")
public class NeoReceiver2 {

    @RabbitHandler
    public void process(String neo) {
        System.out.println("Receiver 2: " + neo);
    }

}

 

4).测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ManyTest {
    @Autowired
    private NeoSender1 neoSender1;

    @Autowired
    private NeoSender2 neoSender2;

    @Test
    public void oneToMany() throws Exception {
        for (int i=0;i<100;i++){
            neoSender1.send(i);
        }
    }

    @Test
    public void manyToMany() throws Exception {
        for (int i=0;i<100;i++){
            neoSender1.send(i);
            neoSender2.send(i);
        }
    }

}

<3>.传输对象和集合类型数据

1).在队列配置中添加配置

@Bean
/**
 * 对象
 */
public Queue objQueue() {
    return new Queue("object");
}

@Bean
/**
 * 集合
 */
public Queue listQueue() {
    return new Queue("list");
}

 

2).发送者

/**
 * rabbitmq传输对象-发送端
 * @author mengjh
 *
 */
@Component
public class ObjectSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;


    public void send(UserEntity user) {
        System.out.println("Sender : " + user);
        this.rabbitTemplate.convertAndSend("object", user);
    }

}



/**
 * 测试rabbitMQ关于list--发送端
 * @author Leruan
 *
 */
@Component
public class ListSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(List list) {

        System.out.println("Sender : " + list);
        this.rabbitTemplate.convertAndSend("list", list);
    }

}

 

3).接受者

/**
 * rabbitmq传输对象-接收端
 * @author mengjh
 *
 */
@Component
@RabbitListener(queues = "object")
public class ObjectReceiver {

    @RabbitHandler
    public void process(UserEntity user) {
        System.out.println("Receiver  : " + user);
    }

}



/**
 * rabbitmq测试接收list
 * @author mengjh
 *
 */
@Component
public class ListReceiver {


    @RabbitListener(queues = "list")
    public void process(List<UserEntity> user) {
        System.out.println("Receiver  : " + user);
    }

}

 

4).测试

/**
 * rabbitmq的object测试
 * @author Leruan
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQObjectTest {

    @Autowired
    private ObjectSender objSender;
    @Autowired
    private User1Mapper userMapper;

    @Test
    public void test() {

        objSender.send(userMapper.getOne(1));
    }
}



/**
 * rabbitmq的list测试
 * @author mengjh
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQListTest {

    @Autowired
    private ListSender listSender;
    @Autowired
    private User1Mapper userMapper;

    @Test
    public void test() {
        List<UserEntity> users = userMapper.getAll();
        listSender.send(users);
    }
}

4.topic模式

<1>.配置topic的队列TopicRabbitConfig.java

@Configuration
public class TopicRabbitConfig {

    final static String message = "topic.message";
    final static String messages = "topic.messages";

    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.message);
    }

    @Bean
    public Queue queueMessages() {
        return new Queue(TopicRabbitConfig.messages);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
}

<2>.topic发送者

@Component
public class TopicSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hi, i am message all";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.1", context);
    }

    public void send1() {
        String context = "hi, i am message 1";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.message", context);
    }

    public void send2() {
        String context = "hi, i am messages 2";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.messages", context);
    }

}

3).topic接受者

/**
 * 发布订阅方式的rabbitmq接收端1
 * @author mengjh
 *
 */
@Component
@RabbitListener(queues = "topic.message")
public class TopicReceiver1 {

    @RabbitHandler
    public void process(String message) {
        System.out.println("Topic Receiver1  : " + message);
    }
}





/**
 * 发布订阅方式的rabbitmq接收端2
 * @author Leruan
 *
 */
@Component
@RabbitListener(queues = "topic.messages")
public class TopicReceiver2 {

    @RabbitHandler
    public void process(String message) {
        System.out.println("Topic Receiver2  : " + message);
    }
}

 

springboot初学---rabbitmq的使用

标签:template   文件   auth   str   print   一对多   class   user   ext   

原文地址:https://www.cnblogs.com/javamjh/p/10315375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!