Springboot整合RabbitMQ简单实例

RabbitMQ

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

实战训练
Springboot整合RabbitMQ简单实例_第1张图片

  • 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就可以发送消息,同时也能看到控制台输出接收到的消息。

你可能感兴趣的:(springboot,springboot,rabbitmq)