SpringBoot极简教程 · Spring Boot Spring-BootSpring Boot

spring boot 中使用 RabbitMQ 教程二 wor

2018-02-09  本文已影响2392人  阿波罗程序猿

spring boot 中使用 RabbitMQ 教程一 生产者、队列、消费者,中讲的是,一对一的关系。这次我们来探寻一对多的关系。

import com.zb.rabbitMQtest.workqueues.receiver.WorkReceiver;
import com.zb.rabbitMQtest.workqueues.send.WorkSend;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 张博
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue workQueue() {
        return new Queue("work-queue");
    }

    @Bean
    public WorkReceiver workReceiver() {
        return new WorkReceiver("Receiver0");
    }

    @Bean
    public WorkReceiver workReceiver1() {
        return new WorkReceiver("Receiver1");
    }

    @Bean
    public WorkSend workSend() {
        return new WorkSend();
    }
}
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 张博
 */
@Component
public class WorkReceiver {

    private String receiverInstance;

    public WorkReceiver(String receiverInstance) {
        this.receiverInstance = receiverInstance;
    }

    @RabbitListener(queues = "work-queue")
    public void receive(String str) {
        System.out.println(receiverInstance.concat(" =====: ").concat(str));
    }
}
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 张博
 */
@Component
public class WorkSend {

    @Autowired
    private Queue workQueue;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        for (int i = 0; i < 10; i++) {
            String msg = "你好 " + i;
            System.out.println("send =====:".concat(msg));
            rabbitTemplate.convertAndSend(workQueue.getName(), msg);
        }
    }
}
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;

/**
 * @author 张博
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class WorkSendTest {

    @Autowired
    private WorkSend workSend;

    @Test
    public void send() throws Exception {
        workSend.send();
    }
}
send =====:你好 0
send =====:你好 1
send =====:你好 2
send =====:你好 3
send =====:你好 4
send =====:你好 5
send =====:你好 6
send =====:你好 7
send =====:你好 8
send =====:你好 9

Receiver1 =====: 你好 1
Receiver0 =====: 你好 0
Receiver0 =====: 你好 2
Receiver1 =====: 你好 3
Receiver0 =====: 你好 4
Receiver1 =====: 你好 5
Receiver0 =====: 你好 6
Receiver1 =====: 你好 7
Receiver0 =====: 你好 8
Receiver1 =====: 你好 9
import com.zb.rabbitMQtest.workqueues.receiver.WorkReceiver;
import com.zb.rabbitMQtest.workqueues.send.WorkSend;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 张博
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue workQueue() {
        return new Queue("work-queue");
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setPrefetchCount(0);
        return factory;
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory =  new CachingConnectionFactory("127.0.0.1", 5672);
        cachingConnectionFactory.setUsername("guest");
        cachingConnectionFactory.setPassword("guest");
        return cachingConnectionFactory;
    }

    @Bean
    public WorkReceiver workReceiver() {
        return new WorkReceiver("Receiver0");
    }

    @Bean
    public WorkReceiver workReceiver1() {
        return new WorkReceiver("Receiver1");
    }

    @Bean
    public WorkSend workSend() {
        return new WorkSend();
    }
}
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 张博
 */
@Component
public class WorkReceiver {

    private String receiverInstance;

    public WorkReceiver(String receiverInstance) {
        this.receiverInstance = receiverInstance;
    }

    @RabbitListener(queues = "work-queue", containerFactory = "rabbitListenerContainerFactory")
    public void receive(String str) {
        System.out.println(receiverInstance.concat(" =====: ").concat(str));
    }
}

导航

spring boot 中使用 RabbitMQ 教程一 生产者、队列、消费者

上一篇 下一篇

猜你喜欢

热点阅读