|
@@ -0,0 +1,54 @@
|
|
|
+package com.hcloud.microserver.h5.config.mq;
|
|
|
+
|
|
|
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
|
|
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Scope;
|
|
|
+
|
|
|
+import java.util.logging.Logger;
|
|
|
+
|
|
|
+
|
|
|
+ * 读取application.properties中的连接配置
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class RabbitMQConfiguration {
|
|
|
+
|
|
|
+ private static Logger logger = Logger.getLogger("RabbitMQConfiguration");
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.host}")
|
|
|
+ public String host;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.port}")
|
|
|
+ public int port;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.username}")
|
|
|
+ public String username;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.password}")
|
|
|
+ public String password;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.virtual-host}")
|
|
|
+ public String virtualHost;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ConnectionFactory connectionFactory() {
|
|
|
+ CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
|
|
|
+ connectionFactory.setUsername(username);
|
|
|
+ connectionFactory.setPassword(password);
|
|
|
+ connectionFactory.setVirtualHost(virtualHost);
|
|
|
+ connectionFactory.setPublisherConfirms(true);
|
|
|
+ logger.info("Create ConnectionFactory bean ..");
|
|
|
+ return connectionFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
|
|
+ public RabbitTemplate rabbitTemplate() {
|
|
|
+ RabbitTemplate template = new RabbitTemplate(connectionFactory());
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+}
|