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