Hi,
here is my code to backup index files with Lucene Replicator,but It doesn't 
work well, No files were backuped.
Could you check my code and give me your advice?


public class IndexFiles {


        private static Directory dir;
        private static Path bakPath;
        private static LocalReplicator replicator;


        public static LocalReplicator getInstance() {
                if (replicator == null) {
                        replicator = new LocalReplicator();
                }
                return replicator;
        }
        public static Directory getDirInstance() {
                if (dir == null) {
                        try {
                                dir = FSDirectory.open(Paths.get("/tmp/index"));
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                return dir;
        }
        public static Path getPathInstance() {
                if (bakPath == null) {
                        bakPath = Paths.get("/tmp/indexBak");
                }
                return bakPath;
        }


        
        /** Index all text files under a directory. */
        public static void main(String[] args) {
                String id = "-oderfilssdhsjs";
                String title = "????????";
                String body = 
"??????????????????????????????????????????????????????????????????????????????????3??2??????"
                                + 
"????????????????????????????????????????????????????????????????????????????????????????????????"
 + "????????????????????????????22??????????????????????????????";
                try {
                        // Directory dir = 
FSDirectory.open(Paths.get(indexPath));
                        Analyzer analyzer = new IKAnalyzer(true);
                        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
                        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
                        SnapshotDeletionPolicy snapshotter = new 
SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
                        iwc.setIndexDeletionPolicy(snapshotter);
                        IndexWriter writer = new 
IndexWriter(IndexFiles.getDirInstance(), iwc);// the
                        LocalReplicator replicator = IndexFiles.getInstance();


                        Document doc = new Document();
                        Field articleId = new StringField("id", id, 
Field.Store.YES);
                        doc.add(articleId);
                        Field articleTitle = new TextField("title", title, 
Field.Store.YES);
                        doc.add(articleTitle);
                        Field articleBody = new TextField("body", body, 
Field.Store.NO);
                        doc.add(articleBody);
                        Field tag1 = new TextField("tags", "????", 
Field.Store.NO);
                        doc.add(tag1);
                        // Field tag2 = new TextField("tags", "????", 
Field.Store.NO);
                        // doc.add(tag2);
                        // Field tag3 = new TextField("tags", "????", 
Field.Store.NO);
                        // doc.add(tag3);
                        // Field tag4 = new TextField("tags", "??????", 
Field.Store.NO);
                        // doc.add(tag4);


                        writer.updateDocument(new Term("id", id), doc);
                        writer.commit();
                        ReplicatorThread p = new ReplicatorThread(); 
                        new Thread(p, "ReplicatorThread").start();
                        replicator.publish(new IndexRevision(writer));
                        Thread.sleep(50000);
                        writer.close();
                } catch (IOException e) {
                        System.out.println(" caught a " + e.getClass() + "\n 
with message: " + e.getMessage());
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }
}


class ReplicatorThread implements Runnable {


        public void run() {
                Callable<Boolean> callback = null; 
                ReplicationHandler handler = null;
                try {
                        handler = new 
IndexReplicationHandler(IndexFiles.getDirInstance(), callback);
                        SourceDirectoryFactory factory = new 
PerSessionDirectoryFactory(IndexFiles.getPathInstance());
                        ReplicationClient client = new 
ReplicationClient(IndexFiles.getInstance(), handler, factory);
                        client.updateNow();
                        try {
                                Thread.sleep(10000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } 
                System.out.println("death");
        }
}

Best Regards.
Jean Ju

Reply via email to