Spring BOOT 整合 RabbitMq 實戰操作(一)
RabbitMq訊息消費者服務
開發工具Idea和Spring boot來開發的。
訊息消費目前只是一個簡單的Demo,後續會處理成更智慧一些。
首先配置檔案類,RabbitMqConfig,裡面配置一些使用者名稱和密碼嗨喲佇列資訊。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package com.basic.rabbitmq.consumer.config;
import com.basic.rabbitmq.consumer.listener.HandleMessageListenerAdapter;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/** * Rabbitmq配置類
* Created by sdc on 2017/7/4.
*/
@Configuration @ComponentScan (basePackages = { "com.basic" })
@PropertySource (value = { "classpath:application.properties" })
public class RabbitMqConfig {
@Autowired
private Environment env;
/**
* 構建connectionfactory
* @return
* @throws Exception
*/
@Bean
public ConnectionFactory connectionFactory() throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(env.getProperty( "spring.rabbitmq.host" ));
connectionFactory.setPort(Integer.valueOf( "5672" .trim()));
connectionFactory.setVirtualHost( "/" );
connectionFactory.setUsername(env.getProperty( "spring.rabbitmq.username" ));
connectionFactory.setPassword(env.getProperty( "spring.rabbitmq.password" ));
return connectionFactory;
}
/**
* CachingConnectionFactory
* @return
* @throws Exception
*/
@Bean
public CachingConnectionFactory cachingConnectionFactory() throws Exception {
return new CachingConnectionFactory(connectionFactory());
}
/**
* RabbitTemplate,類似於jdbctemplate一樣的工具類
* @return
* @throws Exception
*/
@Bean
public RabbitTemplate rabbitTemplate() throws Exception {
RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory());
rabbitTemplate.setChannelTransacted( true );
return rabbitTemplate;
}
@Bean
public AmqpAdmin amqpAdmin() throws Exception {
return new RabbitAdmin(cachingConnectionFactory());
}
@Bean
public SimpleMessageListenerContainer listenerContainer(
@Qualifier ( "handleMessageListenerAdapter" ) HandleMessageListenerAdapter handleMessageListenerAdapter) throws Exception {
//佇列名字
String queueName = env.getProperty( "emial.server.queue" ).trim();
//單一的訊息監聽容器
SimpleMessageListenerContainer simpleMessageListenerContainer =
new SimpleMessageListenerContainer(cachingConnectionFactory());
simpleMessageListenerContainer.setQueueNames(queueName);
simpleMessageListenerContainer.setMessageListener(handleMessageListenerAdapter);
//手動設定 ACK,就是成功消費資訊了,就設定一下這個,rabbitmq就從此佇列裡刪除這條資訊了。
simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return simpleMessageListenerContainer;
}
} |
我這裡配置了一個SimpleMessageListenerContainer,這個Bean,用來監聽佇列裡的訊息的。
具體的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.basic.rabbitmq.consumer.listener;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.mail.MailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/** * 監聽訊息的處理介面卡
* Created by sdc on 2017/7/10.
*/
@Component ( "handleMessageListenerAdapter" )
public class HandleMessageListenerAdapter extends MessageListenerAdapter {
// @Resource // private JavaMailSender mailSender; /**
* 這塊和activemq那個監聽器差不多,都是監聽資訊,也都是onMessage方法。
* @param message
* @param channel
* @throws Exception
*/
@Override
public void onMessage(Message message, Channel channel) throws Exception {
String messageDetail = new String(message.getBody()); //訊息體
System.out.println( "訊息消費:" + messageDetail);
// 手動ACK
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false );
}
} |
還有一些配製檔案,請看
http://10103778.blog.51cto.com/10093778/1945756
這個部落格,就可以看到具體的配製了。
啟動這個專案,就可以從佇列消費訊息了。消費者還是比較簡單的,對應到相應的佇列就可以處理了訊息了。
本文轉自 豆芽菜橙 51CTO部落格,原文連結:http://blog.51cto.com/shangdc/1945974
相關文章
- Spring Boot整合Redis實戰操作Spring BootRedis
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- spring boot-整合RabbitMq(RabbitMq基礎)Spring BootMQ
- Spring Boot 整合 Elasticsearch 實戰Spring BootElasticsearch
- 每日一學:如何在 Spring Boot 整合 RabbitMQ ?Spring BootMQ
- RabbitMQ(三):RabbitMQ與Spring Boot簡單整合MQSpring Boot
- MyBatis初級實戰之一:Spring Boot整合MyBatisSpring Boot
- spring-boot-route(十三)整合RabbitMQSpringbootMQ
- Spring Boot(十三)RabbitMQ安裝與整合Spring BootMQ
- Spring AMQP 實戰 - 整合 RabbitMQ 傳送郵件SpringMQ
- Spring AMQP 實戰 – 整合 RabbitMQ 傳送郵件SpringMQ
- Spring Boot 整合 Redis 實現快取操作Spring BootRedis快取
- Spring Boot實戰:資料庫操作Spring Boot資料庫
- 持續整合之 Spring Boot 實戰篇Spring Boot
- Spring Boot實戰:整合Swagger2Spring BootSwagger
- Spring Boot 整合 RabbitMQ 傳送延時訊息Spring BootMQ
- Spring Boot 整合 RabbitMQ 訊息事務(消費者)Spring BootMQ
- Spring Boot系列十七 Spring Boot 整合 websocket,使用RabbitMQ做為訊息代理Spring BootWebMQ
- Spring Boot整合Spring Cloud Task實現批處理操作Spring BootCloud
- RabbitMQ-Spring整合RabbitMQMQSpring
- 【RabbitMQ】RabbitMQ與Spring整合MQSpring
- Spring Boot實戰系列(7)整合Consul配置中心Spring Boot
- Spring Boot(六)整合 MyBatis 操作 MySQL 8Spring BootMyBatisMySql
- Spring Boot 揭祕與實戰(六) 訊息佇列篇 – RabbitMQSpring Boot佇列MQ
- Spring Boot 揭祕與實戰(六) 訊息佇列篇 - RabbitMQSpring Boot佇列MQ
- Spring Boot從入門到實戰:整合通用Mapper簡化單表操作Spring BootAPP
- Spring Boot整合Mybatis完成級聯一對多CRUD操作Spring BootMyBatis
- 整合RabbitMQ&SpringMQSpring
- Spring Boot功能實戰Spring Boot
- 《Elasticsearch技術解析與實戰》Chapter 1.4 Spring Boot整合ElasticsearchElasticsearchAPTSpring Boot
- go-micro整合RabbitMQ實戰和原理GoMQ
- Spring Boot實戰:模板引擎Spring Boot
- 架構實戰篇(二):Spring Boot 整合Swagger2架構Spring BootSwagger
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- Spring Boot(八):RabbitMQ 詳解Spring BootMQ
- Spring Boot整合Spring Data JPA進行資料庫操作Spring Boot資料庫
- Spring Boot 揭祕與實戰(一) 快速上手Spring Boot