生产者:
引入Maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
配置ActiveMQ的服务器地址和用户名密码application.properties
spring.activemq.broker-url: tcp://127.0.0.1:61616
spring.activemq.user: admin
spring.activemq.password: admin
queue: myQueue
server.port: 8082
消息实体类:
package com.example.domain;
public class User {
public User(Long id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
private Long id;
private String name;
private Integer 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;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
生产者启动类
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
队列配置类
package com.example.config;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class QueueConfig {
@Value("${queue}")
private String QueueName;
@Bean("queue")
public Queue queue(){
return new ActiveMQQueue(QueueName);
}
}
生产者类代码:
package com.example.config;
import java.util.Date;
import java.util.UUID;
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.User;
@Component
public class Producter {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
int age=18;
@Scheduled(fixedDelay=5000)
public void send(){
age++;
User user = new User(System.currentTimeMillis(), UUID.randomUUID().toString(), age);
JSONObject jsonObject = new JSONObject();
String jsonString = jsonObject.toJSONString(user);
jmsMessagingTemplate.convertAndSend(queue, jsonString);
System.out.println(new Date()+":"+jsonString);
}
}
消费者:配置文件和Maven依赖与上面的生产者一致
消费者类:
package com.example.config;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.User;
@Component
public class Comsumer {
@JmsListener(destination = "${queue}")
public void receive(String msg){
System.out.println(msg);
JSONObject jsonObject = new JSONObject();
User user = jsonObject.parseObject(msg, User.class);
System.out.println(user.getName()+"----"+user.getAge()+"-----"+user.getId());
}
}