1 | package eu.clarin.cmdi.oai.harvester.impl; |
---|
2 | |
---|
3 | import java.net.URI; |
---|
4 | import java.util.concurrent.TimeUnit; |
---|
5 | |
---|
6 | import eu.clarin.cmdi.oai.harvester.HarvestHandler; |
---|
7 | import eu.clarin.cmdi.oai.harvester.HarvestJob; |
---|
8 | import eu.clarin.cmdi.oai.harvester.Harvester; |
---|
9 | import eu.clarin.cmdi.oai.harvester.HarvesterException; |
---|
10 | |
---|
11 | public final class SimpleHarvester extends AbstractHarvester { |
---|
12 | private HarvestWorker worker; |
---|
13 | private long nextId = 0; |
---|
14 | |
---|
15 | private SimpleHarvester() { |
---|
16 | this.worker = new HarvestWorker(this); |
---|
17 | } |
---|
18 | |
---|
19 | @Override |
---|
20 | public HarvestJob createJob(URI repositoryURI, HarvestHandler handler) { |
---|
21 | if (repositoryURI == null) { |
---|
22 | throw new IllegalArgumentException("repositoryURI == null"); |
---|
23 | } |
---|
24 | long jobId; |
---|
25 | synchronized (this) { |
---|
26 | jobId = ++nextId; |
---|
27 | } // synchronized |
---|
28 | return new HarvestJobImpl(this, jobId, repositoryURI, handler); |
---|
29 | } |
---|
30 | |
---|
31 | public static Harvester newInstance() { |
---|
32 | return new SimpleHarvester(); |
---|
33 | } |
---|
34 | |
---|
35 | @Override |
---|
36 | public void shutdown() { |
---|
37 | } |
---|
38 | |
---|
39 | @Override |
---|
40 | void doRunJob(HarvestJobImpl job) throws HarvesterException { |
---|
41 | if (job.getState() != HarvestJob.State.NEW) { |
---|
42 | throw new IllegalStateException("job is already in state: " + |
---|
43 | job.getState()); |
---|
44 | } |
---|
45 | |
---|
46 | job.setState(HarvestJob.State.RUNNING); |
---|
47 | |
---|
48 | while (job.isRunning()) { |
---|
49 | worker.process(job, 15); |
---|
50 | long delay = -1; |
---|
51 | while (job.isRunning() && |
---|
52 | ((delay = job.getDelay(TimeUnit.MILLISECONDS)) > 0)) { |
---|
53 | try { |
---|
54 | System.err.println(job.getId() + ": (" + |
---|
55 | job.getNetworkRequestCount() + ") waiting " + |
---|
56 | delay + " ..."); |
---|
57 | Thread.sleep(delay); |
---|
58 | } catch (InterruptedException e) { |
---|
59 | } |
---|
60 | delay = job.getDelay(TimeUnit.MILLISECONDS); |
---|
61 | } // while (wait loop) |
---|
62 | } // while |
---|
63 | } |
---|
64 | |
---|
65 | @Override |
---|
66 | void doCancelJob(HarvestJobImpl job) throws HarvesterException { |
---|
67 | throw new HarvesterException("cancel() is not supported"); |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | protected HarvestJob doFindJob(String id) throws HarvesterException { |
---|
72 | throw new HarvesterException("findJob() is not supported"); |
---|
73 | } |
---|
74 | |
---|
75 | } // class SimpleHarvester |
---|