spring boot中使用RabbitMQ

最近做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());
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值