继上篇springboot整合mq发送消息队列
本篇主要在上篇基础上进行activiemq消息队列的接收
springboot整合mq发送消息队列
第一步:新建marven项目,配置pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sunjian.activitymq.receiver</groupId>
<artifactId>receiver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:配置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);
}
}
第六步:运行以后查看效果
Image.png
Image.png
原文链接:
http://www.jianshu.com/p/b93d54d3584b
关注我的公众号,都是满满的干货!