Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/wsplgen/Scheduler.java @ 1127:6b9877a9f6c1
Added infrastructure to start WSPLGEN calculations - the FloodMapState already start such calculations.
flys-artifacts/trunk@2639 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 13:12:05 +0000 |
parents | |
children | 368040e5c400 |
comparison
equal
deleted
inserted
replaced
1126:da4d8631fc46 | 1127:6b9877a9f6c1 |
---|---|
1 package de.intevation.flys.wsplgen; | |
2 | |
3 import java.util.Collections; | |
4 import java.util.LinkedList; | |
5 import java.util.List; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import de.intevation.flys.artifacts.model.WSPLGENJob; | |
10 | |
11 | |
12 public class Scheduler implements Runnable { | |
13 | |
14 public static final int MAX_WSPLGEN_PROCESSES = 1; | |
15 | |
16 | |
17 protected List<WSPLGENJob> jobs; | |
18 | |
19 | |
20 private static Scheduler INSTANCE; | |
21 | |
22 private static final Logger logger = Logger.getLogger(Scheduler.class); | |
23 | |
24 | |
25 | |
26 private Scheduler() { | |
27 jobs = Collections.synchronizedList(new LinkedList<WSPLGENJob>()); | |
28 } | |
29 | |
30 | |
31 public static Scheduler getInstance() { | |
32 if (INSTANCE == null) { | |
33 logger.info("Create new WSPLGEN Scheduler..."); | |
34 | |
35 INSTANCE = new Scheduler(); | |
36 new Thread(INSTANCE).start(); | |
37 } | |
38 | |
39 return INSTANCE; | |
40 } | |
41 | |
42 | |
43 public void addJob(WSPLGENJob job) { | |
44 synchronized(jobs) { | |
45 jobs.add(job); | |
46 | |
47 logger.info("New WSPLGEN job added."); | |
48 | |
49 jobs.notifyAll(); | |
50 } | |
51 } | |
52 | |
53 | |
54 public WSPLGENJob getJob() { | |
55 synchronized(jobs) { | |
56 if (!jobs.isEmpty()) { | |
57 return jobs.remove(0); | |
58 } | |
59 | |
60 return null; | |
61 } | |
62 } | |
63 | |
64 | |
65 public void run() { | |
66 logger.info("WSPLGEN Scheduler started."); | |
67 | |
68 for (;;) { | |
69 try { | |
70 doRun(); | |
71 } | |
72 catch (InterruptedException ie) { | |
73 logger.warn("Interrupt in WSPLGEN Scheduler -> restart it!"); | |
74 } | |
75 } | |
76 } | |
77 | |
78 | |
79 public void doRun() | |
80 throws InterruptedException | |
81 { | |
82 for (;;) { | |
83 final WSPLGENJob job = getJob(); | |
84 | |
85 if (job != null) { | |
86 logger.debug("Got new job to execute..."); | |
87 | |
88 Thread t = new Thread() { | |
89 public void run() { | |
90 JobExecutor executor = new JobExecutor(job); | |
91 executor.execute(); | |
92 } | |
93 }; | |
94 | |
95 t.start(); | |
96 t.join(); | |
97 } | |
98 else { | |
99 logger.info("No more jobs in Scheduler -> go sleep!"); | |
100 synchronized (jobs) { | |
101 jobs.wait(); | |
102 } | |
103 | |
104 logger.info("New jobs in Scheduler -> wake up!"); | |
105 } | |
106 } | |
107 } | |
108 } | |
109 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |