Springboot整合RabbitMQ简单实例

RabbitMQ

简单来说,它就是个消息中间件,或者说消息中转站。一方负责把消息传到这个中间件,另一方负责接受。

实战训练
这是所有需要的java类

  • RabbitConfig(配置类)
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class RabbitConfig {
    public static final String QUEUE_NAME    = "queue_demo";
    private static final String IP_ADDRESS    = "localhost";
    private static final int    PORT          = 5672;
    private static final String vhost = "vhost1";  //虚拟机,默认是/
    private static final String username = "user1";
    private static final String password = "user1";

    @Bean
    public ConnectionFactory connectionFactory() { //建立连接
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(IP_ADDRESS,PORT);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }

    @Bean
    public Queue queueA() {
        return new Queue(QUEUE_NAME, true); //队列持久
    }
}

-Sender(发送方)

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

@Component
public class Sender {

    private RabbitTemplate rabbitTemplate;

    @Autowired
    public Sender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMsg(String content) { //content是发送的消息内容
        rabbitTemplate.convertAndSend(RabbitConfig.QUEUE_NAME, content);
    }

}

-Receiver(接收方)

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;

@Component
public class Receiver {

    @Autowired
    private UpdateService updateService;

    @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void process(byte[] body ){
        String message = null;
        message = new String(body, StandardCharsets.UTF_8);
        System.out.println(message);
    }
}

-SendController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class SendController {

    @Autowired
    private Sender sender;

    @RequestMapping("/send")
    public void sendmsg() {
        String msg = "hello";
        sender.sendMsg(msg);
    }
}

项目启动后,在浏览器里输入http://localhost:8080/send就可以发送消息,同时也能看到控制台输出接收到的消息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值