什么都不必说--springboot集成rabbitmq

场景:消息中间件,对业务进行分离
复制代码

1.安装 rabbitmq

brew install rabbitmq
复制代码

安装完成后,将

export PATH=$PATH:/usr/local/sbin
复制代码

添加到

~/.bash_profile
复制代码

文件中

2.启动rabbitmq服务

rabbitmq-server
复制代码

3.添加maven

##rabbitmq
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
复制代码

4.添加配置

@Configuration
public class RabbitConfig {
    /**
     * test
     * @param connectionFactory
     * @return
     */
    @Bean
    public SimpleRabbitListenerContainerFactory testListenerContainer(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(connectionFactory);
        factory.setConnectionFactory();
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(5);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);//是否开启ACK模式
        return factory;
    }
}

复制代码

5.添加接收者

@Component
public class TestReceiver {
    @RabbitListener(queues = "test" ,containerFactory = "testListenerContainer")
    public void receiveMessageTest(String test) throws InterruptedException {
        System.out.println("======处理信息======"+System.currentTimeMillis());
        Thread.sleep(10000l);
        System.out.println("TEST Receiver  : " + test+"===="+System.currentTimeMillis());
        System.out.println("======处理信息结束======"+System.currentTimeMillis());
    }
}

复制代码

6.发送者

@Component
public class TestSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    public void send() {
        String context = "hello " + System.currentTimeMillis();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("test", context);
    }
}
复制代码

转载于:https://juejin.im/post/5b4c3e9af265da0f783c9278

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值