RocketMQ使用例

11663 ワード

package com.anve.coupon.basic.rocketmq;

import com.anve.coupon.basic.logic.ConfigLogic;
import com.anve.coupon.domain.constant.Constans;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * linggan on 2017/12/20
 */
@Service
public class SyncProducer {

    private static final Logger logger = LogManager.getLogger(SyncProducer.class);
    private DefaultMQProducer producer;

    @Autowired
    ConfigLogic configLogic;

    /**
     *    mq
     */
    private void init() {
        try {
            //   
            String groupName = Constans.COUPON_UPDATE_ORDER_GROUPNAME;
            //mq  
            String ipAddress = configLogic.getKey(ConfigLogic.ROCKETMQ_IP);
            //           。
            producer = new DefaultMQProducer(groupName);
            producer.setNamesrvAddr(ipAddress);
            producer.start();
            logger.debug("Method[initMq]   mq  :      :{},mq  :{}", groupName, ipAddress);
        } catch (MQClientException e) {
            logger.error("Method[initMq]   mq  :{}", e.getMessage(), e);
        }
    }


    /**
     *      
     *
     * @param data
     * @return
     */
    public boolean push(List data) {
        if (CollectionUtils.isEmpty(data)) {
            return false;
        }
        for (String message : data) {
            push(Constans.COUPON_UPDATE_ORDER_TOPIC, Constans.COUPON_UPDATE_ORDER_TAG, message);
        }
        return true;
    }


    /**
     *    
     *
     * @param topic
     * @param tag
     * @param body
     * @return
     */
    public boolean push(String topic, String tag, String body) {
        if (StringUtils.isEmpty(topic) || StringUtils.isEmpty(tag) || StringUtils.isEmpty(body)) {
            logger.debug("Method[push]topic,tag,body    ,topic:{},tag:{},body:{}", topic, tag, body);
            return false;
        }
        try {
            Message msg = new Message(topic, tag, (body).getBytes());
            SendResult sendResult = producer.send(msg);
            logger.debug("Method[push]Mq     ;{}", sendResult);
        } catch (Exception e) {
            logger.debug("Method[push]     ,topic:{},tag:{},body:{},    :{}", topic, tag, body, e.getMessage(), e);
        }
        return true;
    }


    /**
     *            ,    。
     */
    public void shutdown() {
        producer.shutdown();
        logger.debug("Method[shutdown]Mq       ");
    }

 
  
 
  

id="syncProducer" class="com.anve.coupon.basic.rocketmq.SyncProducer"
      scope="singleton" init-method="init" destroy-method="shutdown">
}
 
  
         ,           ,      ,