Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/wsplgen/Scheduler.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 453d2d0c4258 |
children |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.wsplgen; | |
2 | |
3 import java.util.HashMap; | |
4 import java.util.Map; | |
5 import java.util.concurrent.Future; | |
6 import java.util.concurrent.ScheduledThreadPoolExecutor; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import de.intevation.artifacts.CallContext; | |
11 import de.intevation.flys.artifacts.model.map.WSPLGENJob; | |
12 | |
13 | |
14 /** | |
15 * The Scheduler is used to retrieve new WSPLGENJob. The incoming jobs are added | |
16 * to a ScheduledThreadPoolExecutor. This thread pool has a number of worker | |
17 * threads that processes the WSPLGENJobs. The number of worker threads can be | |
18 * set using a System property <i>wsplgen.max.threads</i> ; its default value is | |
19 * 1. | |
20 * | |
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
22 */ | |
23 public class Scheduler { | |
24 | |
25 private class FutureJob { | |
26 public Future future; | |
27 public WSPLGENJob job; | |
28 | |
29 public FutureJob(Future future, WSPLGENJob job) { | |
30 this.future = future; | |
31 this.job = job; | |
32 } | |
33 } | |
34 | |
35 public static final int MAX_WSPLGEN_PROCESSES = | |
36 Integer.getInteger("wsplgen.max.threads", 1); | |
37 | |
38 | |
39 protected ScheduledThreadPoolExecutor pool; | |
40 protected Map<String, FutureJob> jobs; | |
41 | |
42 | |
43 private static Scheduler INSTANCE; | |
44 | |
45 private static final Logger logger = Logger.getLogger(Scheduler.class); | |
46 | |
47 | |
48 | |
49 private Scheduler() { | |
50 jobs = new HashMap<String, FutureJob>(); | |
51 pool = new ScheduledThreadPoolExecutor(MAX_WSPLGEN_PROCESSES); | |
52 } | |
53 | |
54 | |
55 public static Scheduler getInstance() { | |
56 if (INSTANCE == null) { | |
57 logger.info("Create new WSPLGEN Scheduler..."); | |
58 | |
59 INSTANCE = new Scheduler(); | |
60 } | |
61 | |
62 return INSTANCE; | |
63 } | |
64 | |
65 | |
66 public void addJob(final WSPLGENJob job) { | |
67 synchronized (jobs) { | |
68 WSPLGENFuture f = new WSPLGENFuture(new WSPLGENCallable(this, job)); | |
69 pool.execute(f); | |
70 | |
71 jobs.put(job.getArtifact().identifier(), new FutureJob(f, job)); | |
72 | |
73 logger.info("New WSPLGEN job successfully added."); | |
74 } | |
75 } | |
76 | |
77 | |
78 /** | |
79 * Cancels a running (or queued) job. | |
80 * | |
81 * @param jobId The id of the job (which is the identifier of an Artifact). | |
82 */ | |
83 public void cancelJob(String jobId) { | |
84 logger.debug("Search job in queue: " + jobId); | |
85 | |
86 synchronized (jobs) { | |
87 FutureJob fj = jobs.get(jobId); | |
88 | |
89 if (fj != null) { | |
90 logger.info("Try to cancel job: " + jobId); | |
91 | |
92 fj.future.cancel(true); | |
93 | |
94 removeJob(jobId); | |
95 | |
96 fj.job.getCallContext().afterBackground( | |
97 CallContext.STORE); | |
98 | |
99 logger.info("Canceled job: " + jobId); | |
100 } | |
101 } | |
102 } | |
103 | |
104 | |
105 protected void removeJob(String id) { | |
106 synchronized (jobs) { | |
107 jobs.remove(id); | |
108 } | |
109 } | |
110 } | |
111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |