Java tutorial
/** * Copyright (c) 2005-2009 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id$ */ package com.zonekey.ssm.common.log.queue; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import org.springframework.util.Assert; /** * ??Queue?Consumer. */ public abstract class PeriodConsumer extends QueueConsumer { protected int batchSize = 0; protected int period = 0; /** * ????. */ public void setBatchSize(int batchSize) { this.batchSize = batchSize; } /** * ??,??. */ public void setPeriod(int period) { this.period = period; } @PostConstruct public void checkSetting() { Assert.isTrue(batchSize > 0); Assert.isTrue(period > 0); } /** * ,???processMessageList()?. */ @SuppressWarnings("unchecked") public void run() { try { while (!Thread.currentThread().isInterrupted()) { List list = new ArrayList(batchSize); queue.drainTo(list, batchSize); processMessageList(list); if (!Thread.currentThread().isInterrupted()) { Thread.sleep(period); } } } catch (InterruptedException e) { // Ignore. } finally { //??. clean(); } } /** * ???. */ protected abstract void processMessageList(List<?> messageList); /** * ?. */ protected abstract void clean(); }