最近做Docker计算需要同步任务信息,选择MQ做中间件,使用spring-boot-starter-amqp 来操作RabbitMQ
yaml 配置
rabbitmq:
ogc:
host: 121.36.39.67
port: 20672
username: potato-datagaea
password: potato-datagaea
virtualHost: potato-datagaea
messageExprartion: 10000
ogcservicepublish: datagaea.ogc.queue.publish.dev
ogcserviceexchange: datagaea.ogc.exchange.publish.dev
config类实现
package com.tudou.potato.datagaea.ogc.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* @author zhangzhuo
* @since 2019年-11月-20日
*/
@Configuration
public class RabbitMqConfig {
@Autowired
private OGCRabbitMqProperties ogcRabbitMqProperties;
@Bean(name = "ogcConnectionFactory")
@Primary
public ConnectionFactory ogcConnectionFactory() {
return initConnectionFactory(ogcRabbitMqProperties.getHost(),
ogcRabbitMqProperties.getPort(), ogcRabbitMqProperties.getUsername(), ogcRabbitMqProperties
.getPassword(), ogcRabbitMqProperties.getVirtualHost(), "ogcQueueName");
}
private CachingConnectionFactory initConnectionFactory(String host, int port, String username,
String password, String virtualHost, String connectName) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setConnectionNameStrategy((c) -> connectName);
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualHost);
return connectionFactory;
}
private Jackson2JsonMessageConverter messageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
return new Jackson2JsonMessageConverter(mapper);
}
@Bean(name = "ogcRabbitTemplate")
public RabbitTemplate ogcRabbitTemplate(
@Qualifier("ogcConnectionFactory") ConnectionFactory connectionFactory) {
RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
AmqpAdmin admin = new RabbitAdmin(connectionFactory);
Queue transfrerQueue = new Queue(ogcRabbitMqProperties.getOgcservicepublish());
admin.declareQueue(transfrerQueue);
//队列与交换绑定
DirectExchange exchange = new DirectExchange(ogcRabbitMqProperties.getOgcserviceexchange());
admin.declareExchange(exchange);
Binding binding = BindingBuilder.bind(transfrerQueue).to(exchange)
.with(ogcRabbitMqProperties.getOgcservicepublish());
admin.declareBinding(binding);
firstRabbitTemplate.setMessageConverter(messageConverter());
return firstRabbitTemplate;
}
@Bean(name = "ogcListenerFactory")
public SimpleRabbitListenerContainerFactory ogcListenerFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("ogcConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
}
package com.tudou.potato.datagaea.ogc.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Description: datagaeaTransfer
*
* @author lige
* @since 2020/02/05
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq.ogc")
public class OGCRabbitMqProperties {
/**
* rabbitmq服务ip
*/
private String host;
/**
* rabbitmq服务ip 连接端口号
*/
private int port;
/**
* rabbitmq服务 用户名
*/
private String username;
/**
* rabbitmq服务 密码
*/
private String password;
/**
* rabbitmq服务 虚拟机名
*/
private String virtualHost;
/**
* ttl 队列 或者 消息过期时间
*/
private int messageExprartion;
/**
* TTL 配置在消息上的缓冲区队列
*/
private String ogcservicepublish;
private String ogcserviceexchange;
}
监听消息处理
```java
package com.tudou.potato.datagaea.ogc.handle;
import com.alibaba.fastjson.JSONObject;
import com.tudou.potato.datagaea.ogc.common.CommonTools;
import com.tudou.potato.datagaea.ogc.rest.OgcLayerController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class OGCPublishListener {
@Autowired
private CommonTools commonTools;
@RabbitListener(queues = "${spring.rabbitmq.ogc.ogcservicepublish}", containerFactory = "ogcListenerFactory")
public void taskProcessConsume(Message message) {
try {
String realMessage = new String(message.getBody());
JSONObject jsonObject = JSONObject.parseObject(realMessage);
log.error(jsonObject.toJSONString());
if (null != jsonObject.get("dmId")) {
commonTools.autoPublish(jsonObject.get("dmId").toString());
}
} catch (Exception ex) {
ex.printStackTrace();
log.error("taskProcessConsume:"+ex);
log.error(ex.toString());
}
}
}