关于阻塞队列\生产者消费者模式用于信息上传的案例

public class UploadInfoBiz extends BaseBiz {
    
    private final static Logger log = Logger.getLogger(UploadInfoBiz.class);
    private static UploadInfoBiz instance = null;
    private static boolean isStarted = false;
    private final static int MAX_SIZE = 50000;   //队列容量
    private static LinkedBlockingQueue<UploadInfo> uploadList = new LinkedBlockingQueue<UploadInfo>(MAX_SIZE);//阻塞线程
    Thread uploadService = null;
    public static synchronized UploadInfoBiz getInstance() {
        if (instance == null) {
            instance = new UploadInfoBiz();
        }
        return instance;
    }
     
    public void start() {
        isStarted = true;
        if (uploadService == null) {
            uploadService = new Thread(new NewUploadTask()); 
            uploadService.start();
        }
    }

    public void stop() {
        isStarted = false;
    }
    
    /**
     *  接受处理数据并放入队列
     * @param jsonReq  json
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     * @throws DBAccessException
     */
    public JsonObject uploadList(UploadInfo uploadInfo) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, DBAccessException {
        JsonObject jsonObject = new JsonObject();
        putUploadMessage(uploadInfo);
        jsonObject.setState(0);
         jsonObject.setStateText("上传成功");
        return jsonObject;
    }
    
    public void putUploadMessage(UploadInfo uploadInfo) {
        try {
            uploadList.put(uploadInfo);
        } catch (InterruptedException e) {
            log.error("数据插入队列失败", e);
        }
     }
      
     /**
      * 数据从队列取出的方法
      */
     public class NewUploadTask implements Runnable {
        public void run() {
            while (isStarted) {
                try {
                    int num = 500;
                    if (uploadList.size() < 500) {
                         num = uploadList.size();
                    }
                    dealWithInfo(num);  //从队列里拿出数据
                    UploadInfo uploadInfo = uploadList.take(); //阻塞队列
                    uploadList.put(uploadInfo);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }    
            }    
        }
    }    
     
    /**
     *  数据从队列拿出并处理
     */
     @Transactional
    public void dealWithInfo(int num) {
        try {
            for (int i=0;i < num;i++) {
                UploadInfo uploadInfo = uploadList.take();
                int result = uploadData(uploadInfo);
                if (result != 0 ) {
                    log.error("上传失败"  };
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }    
    }
        
    /**
     * 数据保存到数据库
     */
    public int uploadData(UploadInfo uploadInfo) throws SQLException,Exception  {
     
             result = TextBiz.getInstance().syncData(uploadInfo);
   
        return result;
    } 
        
}

猜你喜欢

转载自blog.csdn.net/dandelionoooo/article/details/81477634