Project

General

Profile

1
package eu.dnetlib.openaire.directindex.api;
2

    
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
import org.apache.solr.client.solrj.impl.CloudSolrClient;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.beans.factory.annotation.Value;
8

    
9
import javax.annotation.Resource;
10
import java.util.concurrent.Executors;
11
import java.util.concurrent.ScheduledExecutorService;
12
import java.util.concurrent.TimeUnit;
13

    
14
public class ResultSubmitterService {
15

    
16
    private static final Log log = LogFactory.getLog(ResultSubmitterService.class);
17

    
18
    @Autowired
19
    private IndexDSRetriever indexDSRetriever;
20

    
21
    @Resource(name = "indexClientManager")
22
    private IndexClientManager clientManager;
23

    
24
    /**
25
     * Autocommit feature activation flag
26
     */
27
    @Value(value = "${openaire.api.directindex.autocommit.active}")
28
    private boolean autocommitactive;
29

    
30
    /**
31
     * Autocommit frequency (Seconds)
32
     */
33
    @Value(value = "${openaire.api.directindex.autocommit.frequency}")
34
    private long commitfrquency = 60;
35

    
36
    private ScheduledExecutorService executor;
37

    
38
    public ResultSubmitterService() {
39
        executor = Executors.newSingleThreadScheduledExecutor();
40
        updateCommitSchedule();
41
    }
42

    
43
    private void updateCommitSchedule() {
44
        log.info("updating commit schedule");
45

    
46
        executor.scheduleAtFixedRate(() -> {
47
            if (isAutocommitactive()) {
48
                try {
49
                    IndexDsInfo info = indexDSRetriever.calculateCurrentIndexDsInfo();
50
                    CloudSolrClient client = clientManager.getClient(info);
51
                    log.info("performing commit on " + info.getColl());
52
                    client.commit();
53
                } catch (Throwable e) {
54
                    log.error("unable to perform commit", e);
55
                }
56
            }
57
        }, 0, getCommitfrquency(), TimeUnit.SECONDS);
58
    }
59

    
60
    public boolean isAutocommitactive() {
61
        return autocommitactive;
62
    }
63

    
64
    public synchronized void setAutocommitactive(boolean autocommitactive) {
65
        this.autocommitactive = autocommitactive;
66
    }
67

    
68
    public long getCommitfrquency() {
69
        return commitfrquency;
70
    }
71

    
72
    public synchronized void setCommitfrquency(long commitfrquency) {
73
        this.commitfrquency = commitfrquency;
74
    }
75

    
76
}
(8-8/8)