DBHelper.java
1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.bsth.oplog.db;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.bsth.Application;
import com.bsth.oplog.Log;
import com.bsth.oplog.db.mysql.DBPersistence;
@Component
public class DBHelper implements CommandLineRunner{
private final static int maxBufSize = 1000;
private final static int fixedMinute = 10;
private static LinkedList<Log> buffer = new LinkedList<>();
@Autowired
private FixedTimePersistenceThread fixedTimeThread;
public void save(Log log){
buffer.add(log);
if(buffer.size() >= maxBufSize)
fixedTimeThread.start();
}
@Component
public static class FixedTimePersistenceThread extends Thread{
@Autowired
DBPersistence persistence;
@Override
public void run() {
//persistence.batchSave(buffer);
}
}
@Override
public void run(String... arg0) throws Exception {
Application.mainServices.scheduleWithFixedDelay(fixedTimeThread, fixedMinute, fixedMinute, TimeUnit.MINUTES);
}
}