【SpringBoot】阿里云消息队列RcoketMQ消费者代码示例

项目中使用到了mq,使用的是阿里云消息队列,作为消费者进行消费,记录下相关的代码。

阿里云消息队列maven依赖

			1.8.4.Final

            
                com.aliyun.openservices
                ons-client
                ${mq.version}
            

mq配置:

# producer
mq.producer.properties.AccessKey=test
mq.producer.properties.SecretKey=test
mq.producer.properties.NAMESRV_ADDR=addr
mq.consumer.properties.ProducerId=producerId

# consumer
mq.consumer.properties.AccessKey=accesskey
mq.consumer.properties.SecretKey=SecretKey
mq.consumer.properties.NAMESRV_ADDR=add
mq.consumer.properties.ConsumerId=consumerId
mq.consumer.properties.ConsumeThreadNums=25

# subscription
mq.subscription.settleCallback.topic=TOPIC
# expression即Tag,可以设置成具体的Tag,如 taga||tagb||tagc,也可设置成*。 *仅代表订阅所有Tag,不支持通配
mq.subscription.settleCallback.expression= "*"

mq配置类:

@Data
@ConfigurationProperties(prefix = "mq")
public class MqConfigProperties {

    private Producer producer;
    private Consumer consumer;
    private Map subscription;

    @Data
    public static class Producer {
        private Properties properties;
    }

    @Data
    public static class Consumer {
        private Properties properties;
    }

}
@Slf4j
@Configuration
@EnableConfigurationProperties(MqConfigProperties.class)
public class MqAutoConfiguration {

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean mqProducer(MqConfigProperties mqConfigProperties) {
        ProducerBean bean = new ProducerBean();
        bean.setProperties(mqConfigProperties.getProducer().getProperties());
        return bean;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean consumerMessageListener(MqConfigProperties mqConfigProperties,
    											AutoCheckPayMqListener autoCheckPayMqListener,
                                                SettleCallbackMqListener settleCallbackMqListener) {
        ConsumerBean bean = new ConsumerBean();
        bean.setProperties(mqConfigProperties.getConsumer().getProperties());
        Map subscriptionTable = new HashMap<>(3);
        //消息订阅,如果消费多个消息进行配置
               
    //    public static final String SETTLE_CALLBACK = "settleCallback";
	//    public static final String MQ_CONFIG_PAY_QUERY = "payQuery";
               subscriptionTable.put(mqConfigProperties.getSubscription().get(Constant.MQ_CONFIG_PAY_QUERY), autoCheckPayMqListener);
subscriptionTable.put(mqConfigProperties.getSubscription().get(Constants.SETTLE_CALLBACK), settleCallbackMqListener);
        bean.setSubscriptionTable(subscriptionTable);
        //日志
log.info("mq-consumer Listener subscription Table: ");
        subscriptionTable.forEach((subscription, messageListener) -> log.info("subscription: {} , messageListener: {}",
                JSON.toJSONString(subscription), JSON.toJSONString(messageListener)));
        return bean;
    }
}

mq信息处理:

@Service("settleCallbackMqListener")
public class SettleCallbackMqListener implements MessageListener {

    @Resource
    private PaymentTradeService paymentTradeService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            String data = new String(message.getBody(), StandardCharsets.UTF_8);
              // todo 
        } catch (Exception e) {
            // todo 
        }
        return Action.CommitMessage;
    }

}

你可能感兴趣的:(JAVA常用工具类,Java,rabbitmq)