springboot整合rabbitmq发送消息

2019-08-21  本文已影响0人  不见当年三月花

springboot整合rabbitmq发送消息

构建项目

image.png
repositories {
   mavenCentral()
}

ext{
   springBootVersion = "2.0.3.RELEASE"
}
dependencies {

   compile("org.springframework.boot:spring-boot-starter-amqp:1.5.2.RELEASE")
   compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
   compile("org.springframework.boot:spring-boot-starter-undertow:$springBootVersion")



}
//排除tomcat
configurations {
   compile.exclude module: 'spring-boot-starter-tomcat'
}
@Component
public class DemoProducer {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String sendMsg = "msg " + new Date();
        System.out.println("生产者开始生产信息 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("msg", sendMsg);
    }
}
@Component
@RabbitListener(queues = "msg")
public class DemoReceiver {

   @RabbitHandler
   public void process(String msg) {
       System.out.println("消费者 收到信息  : " + msg);
   }
}

@Controller
@RequestMapping("/rabbit")
public class DemoController {

    @Autowired
    private DemoProducer demoProducer;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(HttpServletRequest request) {
        demoProducer.send();
        return "success";
    }
}
spring:
  rabbitmq:
    host: ######
    port: ######
    username: ######
    password: ######
    virtual-host: /

server:
  port: 9000

上一篇 下一篇

猜你喜欢

热点阅读