Spring Boot 整合ActiveMQ的過程
- 安裝ActiveMQ伺服器,(也可以不安裝,如果不安裝,會使用記憶體mq)
- 構建Spring boot專案,增加依賴項,只需要新增這一項即可
<!-- 新增acitivemq依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
- 增加Application類
@SpringBootApplication
@EnableScheduling //使用定時任務傳送訊息
public class MqTestApplication {
public static void main(String[] args) {
SpringApplication.run(MqTestApplication.class, args);
}
}
- 配置application.yml
spring:
activemq:
broker-url: tcp://127.0.01:61616
packages:
trust-all: true
- 構建一個資料Model,可以傳送和消費的資料型別有: String, byte array, Map<String,?>, Serializable object.
// 如果傳送的訊息是一個物件,必須implements Serializable介面
public class TModel implements Serializable {
private static final long serialVersionUID = -921008687184331557L;
private int count;
public TModel(int count) {
this.count = count;
}
@Override
public String toString() {
return "TModel [count=" + count + "]";
}
}
- 構建Producer
@Component
public class Producer {
// 在Producer中注入JmsTemplate,我們可以通過這個template傳送訊息
private final JmsTemplate jmsTemplate;
private int count = 0;
@Autowired
public Producer(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
// 這裡使用Spring Boot的定時任務傳送訊息
@Scheduled(fixedRate = 1000)
public void create() {
// 使用convertAndSend傳送訊息
jmsTemplate.convertAndSend("queue1", new TModel(count++));
}
}
- 構建Consumer
@Component
public class Consumer {
@JmsListener(destination = "queue1")
public void comsume(TModel content) {
System.out.println("recive message from queue1 [" + content + "]");
}
}
特別備註:如果我們的生產者和消費者在不同的Module中時,最好將要消費的資料抽象成公共Module.程式是通過Serializable來序列化和反序列化物件的。必須保證生產者和消費者的物件模型的serialVersionUID是一致的。
相關文章
- ActiveMq整合SpringMQSpring
- Spring與ActiveMQ整合SpringMQ
- Spring Boot 啟動過程Spring Boot
- Spring+ActiveMQ整合測試SpringMQ
- ActiveMQ的使用及整合spring的使用例項MQSpring
- Spring Boot 2.0(八):Spring Boot 整合 MemcachedSpring Boot
- Spring Boot(十八):使用 Spring Boot 整合 FastDFSSpring BootAST
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- Spring Boot 整合 elasticsearchSpring BootElasticsearch
- Spring Boot 整合 dockerSpring BootDocker
- Spring Boot 整合 elkSpring Boot
- Spring Boot 整合 ApolloSpring Boot
- Spring Boot整合RedisSpring BootRedis
- Spring Boot 整合redisSpring BootRedis
- Spring Boot 整合 MyBatisSpring BootMyBatis
- Spring Boot整合SocketSpring Boot
- spring boot整合jooqSpring Boot
- Spring Boot 整合 KafkaSpring BootKafka
- spring boot 整合mybatisSpring BootMyBatis
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot整合Spring AopSpring Boot
- Spring Boot原始碼分析-啟動過程Spring Boot原始碼
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- 在 Kubernetes 上使用Spring Boot+ActiveMQSpring BootMQ
- Spring Boot:整合Spring Data JPASpring Boot
- spring-boot+spring-session整合SpringbootSession
- Spring Boot 整合 Apache DubboSpring BootApache
- Spring Boot Actuator 整合 PrometheusSpring BootPrometheus
- spring boot整合HadoopSpring BootHadoop
- spring boot整合shiroSpring Boot
- ElasticSearch與Spring Boot整合ElasticsearchSpring Boot
- spring boot(三)整合 redisSpring BootRedis
- spring-boot整合kindeditorSpringboot
- spring boot 整合測試Spring Boot
- Spring boot 六 整合 MyBatisSpring BootMyBatis
- spring boot 整合shiroSpring Boot