1 package org.opendaylight.mwtn.index.impl;
4 import java.io.IOException;
5 import java.util.concurrent.Executors;
6 import java.util.concurrent.ScheduledExecutorService;
7 import java.util.concurrent.ScheduledFuture;
8 import java.util.concurrent.TimeUnit;
10 import org.opendaylight.mwtn.base.database.HtDatabaseNode;
11 import org.opendaylight.mwtn.base.database.HtDatabaseUpdateFile;
12 import org.opendaylight.mwtn.base.database.HtDatabaseUpdateFile.EsUpdateObject;
13 import org.opendaylight.mwtn.base.database.HtDatabaseUpdateFile.FileReadCallback;
14 import org.opendaylight.mwtn.base.database.HtDatabaseWebAPIClient;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 public class IndexUpdateService implements AutoCloseable {
20 private static final Logger LOG = LoggerFactory.getLogger(IndexUpdateService.class);
22 protected static final String FILENAME = "etc/elasticsearch_update.zip";
24 private final HtDatabaseWebAPIClient webClient;
26 private final boolean autoremove=true;
28 @SuppressWarnings("unused")
29 private ScheduledFuture<?> taskHandle;
30 private final ScheduledExecutorService scheduler;
31 private final HtDatabaseNode database;
33 private final FileReadCallback onReadUpdateFile = new FileReadCallback()
36 public void read(EsUpdateObject obj,String filename) {
38 IndexUpdateService.this.webClient.sendRequest(obj.Uri, obj.Method, obj.Body);
39 LOG.info("run database update file "+filename);
40 } catch (IOException e) {
41 LOG.warn("problem for request "+obj.Uri);
46 public void onerror(String filename,IOException e) {
47 LOG.warn("problem reading content file "+filename+ " :"+e.getMessage());
51 private final Runnable checkForUpdateTask = new Runnable() {
55 File f=new File(FILENAME);
58 LOG.debug("found update file "+f.getAbsolutePath());
60 HtDatabaseUpdateFile updateFile=new HtDatabaseUpdateFile(FILENAME);
61 if(updateFile.readFiles(onReadUpdateFile))
63 LOG.info("update successful");
66 if(IndexUpdateService.this.autoremove)
68 LOG.debug("autodelete updatefile");
72 } catch (IOException e) {
73 LOG.warn("problem with update file:"+e.getMessage());
82 public IndexUpdateService(HtDatabaseNode database, String esNodeserverName, String esClusterName, String esNodeName) {
83 this.database = database;
84 this.webClient = new HtDatabaseWebAPIClient();
85 this.scheduler = Executors.newSingleThreadScheduledExecutor();
89 this.taskHandle = this.scheduler.scheduleAtFixedRate(checkForUpdateTask, 0, 120, TimeUnit.SECONDS);
93 this.scheduler.shutdown();
96 public void close() throws Exception {