springboot整合mq接收消息队列

继上篇springboot整合mq发送消息队列
本篇主要在上篇基础上进行activiemq消息队列的接收
springboot整合mq发送消息队列
第一步:新建marven项目,配置pom文件


  4.0.0
  com.sunjian.activitymq.receiver
  receiver
  0.0.1-SNAPSHOT
  war
  
         org.springframework.boot
        spring-boot-starter-parent
         1.5.4.RELEASE
    
    
        UTF-8
        UTF-8
         1.8
    
    
         
             org.springframework.boot
             spring-boot-starter
         
         
         
             org.springframework.boot
             spring-boot-starter-web
         
         
             org.springframework.boot
             spring-boot-starter-test
             test
         
         
             org.springframework.boot
             spring-boot-starter-activemq
         
         
         
             com.alibaba
             fastjson
             1.2.38
         
    
    
         
             
                 org.springframework.boot
                 spring-boot-maven-plugin
             
         
    

第二步:配置application.yml,注意port端口号必须与发送消息的端口号不一样,否则会端口冲突

spring:
  activemq:
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
queue: sunjian
server:
  port: 8083

第三步:新建Consumer消息队列

package com.sunjian.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.sunjian.entity.UserEntity;
@Component
public class Consumer {
    @JmsListener(destination = "${queue}")//activeMq监听监听接收消息队列
    public void receive(String msg){//这个msg就是从消息队列获得到的参数
         System.out.println(msg);
         JSONObject jsonObject = new JSONObject();
         UserEntity userEntity = jsonObject.parseObject(msg,UserEntity.class);
        System.out.println(userEntity.getName()+"---"+userEntity.getId());
    }
}

第四步:新建UserEntity实体类

package com.sunjian.entity;
public class UserEntity {
    private Long id;
    private String name;
    private Integer age;
    public UserEntity(Long id, String name, Integer age) {
         super();
         this.id = id;
         this.name = name;
         this.age = age;
    }
    public Long getId() {
         return id;
    }
    public void setId(Long id) {
         this.id = id;
    }
    public String getName() {
         return name;
    }
    public void setName(String name) {
         this.name = name;
    }
    public Integer getAge() {
         return age;
    }
    public void setAge(Integer age) {
         this.age = age;
    }
}

第五步:新建App执行程序,这里不在讲关于mq的安装问题,如果要看清浏览
springboot整合mq发送消息队列

package com.sunjian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

第六步:运行以后查看效果

springboot整合mq接收消息队列_第1张图片
Image.png
springboot整合mq接收消息队列_第2张图片
Image.png

你可能感兴趣的:(springboot整合mq接收消息队列)