comparison src/goodjava/lucene/backup/BackupIndexWriter.java @ 1504:f443542d8650

threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 May 2020 11:13:16 -0600
parents 8a7b6b32c691
children d80395468b4e
comparison
equal deleted inserted replaced
1503:74c534de211f 1504:f443542d8650
7 import java.util.List; 7 import java.util.List;
8 import java.util.ArrayList; 8 import java.util.ArrayList;
9 import java.util.Map; 9 import java.util.Map;
10 import java.util.HashMap; 10 import java.util.HashMap;
11 import java.util.Arrays; 11 import java.util.Arrays;
12 import java.util.concurrent.Executors;
13 import java.util.concurrent.ExecutorService;
12 import javax.net.ssl.SSLSocketFactory; 14 import javax.net.ssl.SSLSocketFactory;
13 import javax.net.ssl.SSLSocket; 15 import javax.net.ssl.SSLSocket;
14 import goodjava.io.IoUtils; 16 import goodjava.io.IoUtils;
15 import goodjava.rpc.RpcClient; 17 import goodjava.rpc.RpcClient;
16 import goodjava.rpc.RpcCall; 18 import goodjava.rpc.RpcCall;
27 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class); 29 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class);
28 public static String[] backupDomains; 30 public static String[] backupDomains;
29 private final String name; 31 private final String name;
30 private final File dir; 32 private final File dir;
31 private boolean isSyncPending = false; 33 private boolean isSyncPending = false;
34 private final ExecutorService exec = Executors.newSingleThreadExecutor();
32 35
33 public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name) throws IOException { 36 public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name) throws IOException {
34 super(indexWriter,logDir); 37 super(indexWriter,logDir);
35 if( backupDomains == null ) 38 if( backupDomains == null )
36 throw new RuntimeException("must set backupDomains"); 39 throw new RuntimeException("must set backupDomains");
38 File f = new File(System.getProperty("java.io.tmpdir")); 41 File f = new File(System.getProperty("java.io.tmpdir"));
39 dir = new File(f,"goodjava.lucene/"+name); 42 dir = new File(f,"goodjava.lucene/"+name);
40 IoUtils.mkdirs(dir); 43 IoUtils.mkdirs(dir);
41 } 44 }
42 45
46 public synchronized void close() throws IOException {
47 super.close();
48 exec.shutdown();
49 }
50
43 public synchronized void commit() throws IOException { 51 public synchronized void commit() throws IOException {
44 super.commit(); 52 super.commit();
45 //sync(); 53 //sync();
46 if( !isSyncPending ) { 54 if( !isSyncPending ) {
47 threadPool.execute(sync); 55 exec.execute(sync);
48 isSyncPending = true; 56 isSyncPending = true;
49 } 57 }
50 } 58 }
51 59
52 public void runSync() { 60 public void runSync() {
53 sync.run(); 61 try {
62 exec.submit(sync).get();
63 } catch(Exception e) {
64 throw new RuntimeException(e);
65 }
54 } 66 }
55 67
56 private final Runnable sync = new Runnable() { 68 private final Runnable sync = new Runnable() {
57 public synchronized void run() { 69 public void run() {
58 try { 70 try {
59 sync(); 71 sync();
60 } catch(IOException e) { 72 } catch(IOException e) {
61 throw new RuntimeException(e); 73 throw new RuntimeException(e);
62 } 74 }