Demand: In the development of business reports, you need to read data from the MySQL database operations and then writes to the database, using a timed task to run the batch.
Analysis: ① both performance, ② MySQL Oracle is not so easy, powerful stored procedure. In summary, using a thread pool batch submitted programs to insert MySQL database, for the update operation, such as paging operation in Java end, the program is also supported. code show as below:
1 private void batchDeal (List data, int batchNum) throws InterruptedException {
2 int TOTÁLNÍ = data.size ();
3 int pageno = TOTÁLNÍ% batchNum == 0? TOTÁLNÍ / batchNum: TOTÁLNÍ / batchNum + 1 ;
4 ThreadPoolExecutor executive = new ScheduledThreadPoolExecutor (pageno);
5 try {
6 CountDownLatch countDownLatch = new CountDownLatch (pageno);
7 List necks = null ;
8 intfromIndex, toIndex;
. 9 for ( int I = 0; I <pageNum; I ++ ) {
10 fromIndex = I * batchNum;
. 11 toIndex = Math.min (totalnum, fromIndex + batchNum);
12 is subData = data.subList (fromIndex, toIndex );
13 is ImportTask Task = new new ImportTask (subData, CountDownLatch);
14 Executor.execute (Task);
15 }
16 // main thread must be called immediately after starting CountDownLatch.await other threads () method,
17 // so the main thread operating on this method will block until other threads to complete their task.
18 // counter value is equal to 0, the main thread can pass await () method resume execution of their tasks.
. 19 CountDownLatch.await ();
20 is logger.info ( "data operation is completed before beginning the other traffic!" );
21 is } the finally {
22 is // Close the thread pool, release resources
23 is executor.shutdown ();
24 }
25 }
26 is
27 class ImportTask the implements the Runnable {
28 Private List List;
29 Private a CountDownLatch CountDownLatch;
30
31 is publicImportTask (Data List, a CountDownLatch CountDownLatch) {
32 the this .list = Data;
33 is the this .countDownLatch = CountDownLatch;
34 is }
35
36 @Override
37 [ public void RUN () {
38 is IF ( null ! = List) {
39 // business logic , such as bulk or insert Update
40 logger.info ( "data is now operating} {" , List);
41 is }
42 is // thread that issues the task completion signal
43 is countDownLatch.countDown ();
44 is }
45 }
在第1行中,List data表示传入的数据,batchNum表示每一次处理的数量,例如500条等。