source: OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/impl/SimpleHarvester.java @ 1137

Last change on this file since 1137 was 1137, checked in by oschonef, 13 years ago
  • major re-factoring of Harvester, HarvestJob?, their implementations, etc.
  • initial version for parallel harvester
  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1package eu.clarin.cmdi.oai.harvester.impl;
2
3import java.net.URI;
4import java.util.concurrent.TimeUnit;
5
6import eu.clarin.cmdi.oai.harvester.HarvestHandler;
7import eu.clarin.cmdi.oai.harvester.HarvestJob;
8import eu.clarin.cmdi.oai.harvester.Harvester;
9import eu.clarin.cmdi.oai.harvester.HarvesterException;
10
11public 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
Note: See TracBrowser for help on using the repository browser.