消费队列

package com.pingan.wifi.dahua.base;


import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pingan.wifi.dahua.biz.UpgradeQueueService;


/**
 * 队列消费器
 * 
 * @author renpeng162
 * 
 */
@Component("upgradeQueue")
public class UpgradeQueue implements InitializingBean, DisposableBean {

    private static final Log logger = LogFactory.getLog(UpgradeQueue.class);

    // 要消费的队列名
    private String queueName = RedisKey.FUND_QUEUE_FIRST_BUG;

    // 消费线程池大小
    private int threadPoolSize = 1;

    // 消费线程池
    private List<ConsumerThread> consumerThreads;
    
    @Autowired
    private UpgradeQueueService queueService;

    // 消费线程启动延时
    private int startDelay = 10000;


    /**
     * 在队列消费器初始化时要先启动线程池中的线程
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        consumerThreads = new ArrayList<ConsumerThread>();
        
        for (int i = 0; i < threadPoolSize; i++) {
            ConsumerThread t = new ConsumerThread();
            t.setName("pools-consumer-" + this.queueName + "-" + i);
            t.setDaemon(true); // 把这些消费线程设置成守护线程,方便jvm正常退出
            t.start();
            consumerThreads.add(t);
        }
    }

    /**
     * 消费器销毁时,把所有的消费线程也关闭掉
     */
    @Override
    public void destroy() throws Exception {
        try {
            for (ConsumerThread ct : consumerThreads) {
                ct.shutdown();
            }
        } catch (Exception t) {
            logger.warn(t);
        }
    }

    private class UpgradeConsumerImpl implements UpgradeQueueConsumer {
        private volatile boolean status = true;

        @Override
        public boolean getStatus() {
            return this.status;
        }

        @Override
        public void setStatus(boolean status) {
            this.status = status;
        }

    }

    /**
     * 消费线程类定义
     * 
     * @author EX-ZHAOXIANGTAO001
     * 
     */
    private class ConsumerThread extends Thread {

        private UpgradeQueueConsumer msgConsumer = new UpgradeConsumerImpl();

        public ConsumerThread() {

        }

        @Override
        public void run() {
            logger.info("首先保证线程能够启动===========================状态:"+msgConsumer.getStatus());
            if (startDelay > 0) {
                try {
                    Thread.sleep(startDelay); // 先使线程挂起给定时间,已等待系统其它部分先正常启动
                } catch (InterruptedException e) {
                    logger.warn(e);
                }
            }
            queueService.pullMsg(queueName,msgConsumer);
        }

        public void shutdown() {
            this.msgConsumer.setStatus(false);
        }
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public int getThreadPoolSize() {
        return threadPoolSize;
    }

    public void setThreadPoolSize(int threadPoolSize) {
        this.threadPoolSize = threadPoolSize;
    }

    public int getStartDelay() {
        return startDelay;
    }

    public void setStartDelay(int startDelay) {
        this.startDelay = startDelay;
    }

}
 

转载于:https://my.oschina.net/u/2548090/blog/861486

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值