批量定时队列
可以根据设置,从队列中取出n条数据批量处理,不足n条的话等待m秒后处理数据。
package other
import cn.hutool.core.date.DateUtil
import cn.hutool.core.thread.ThreadUtil
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
/**
* 批量定时执行器
*/
class QueueProcessor {
private LinkedBlockingQueue<String> dataQueue;
def volatile receiveCount = 0
def volatile run = false
int sizePerTime=10
int intervalSecond=5
QueueProcessor(int sizePerTime,int intervalSecond) {
dataQueue = new LinkedBlockingQueue<>();
this.sizePerTime=sizePerTime
this.intervalSecond=intervalSecond
}
/**
* 添加数据到队列
* 支持多线程并发
* @param data
*/
void addData(String data) {
dataQueue.put(data);
}
/**
* 开始
*/
def startProcess() {
if (run) {
return
}
run=true
receiveCount=0
new Thread({
// 定期处理数据
def last = System.currentTimeMillis();
def list = []
while (run) {
def data = dataQueue.poll(2, TimeUnit.SECONDS)
if (data) {
receiveCount++
list.add(data)
}
if (list.size() >=sizePerTime || (list.size() > 0 && System.currentTimeMillis() - last > intervalSecond*1000)) {
println(DateUtil.now())
//批量处理数据
println(list)
list.clear()
last = System.currentTimeMillis();
}
}
}).start()
}
/**
* 停止
*/
def stopProcess(int sendCount){
while (true) {
if (this.receiveCount >= sendCount) {
run = false
break;
}
sleep(500)
}
}
static void main(String[] args) throws Exception {
QueueProcessor processor = new QueueProcessor(10,5);
//1.开启处理器
processor.startProcess()
def sendCount = new AtomicInteger()
def fs = (1..5).collect {thread->
return ThreadUtil.execAsync {
for (int i = 0; i < 100; i++) {
//2.加数据到处理器
processor.addData(thread+"-"+sendCount.incrementAndGet());
sleep(10)
}
println(thread+"结束")
}
}
fs.each {it.get()}
println("fs结束")
//3.关闭处理器
processor.stopProcess(sendCount.get())
println("结束")
}
}