01: /**
02: *
03: */package org.drools.concurrent;
04:
05: import java.io.Serializable;
06:
07: import org.drools.WorkingMemory;
08: import org.drools.util.concurrent.locks.BlockingQueue;
09: import org.drools.util.concurrent.locks.LinkedBlockingQueue;
10:
11: /**
12: * The CommandExecutor is a Producer/Consumer style classes that provides a queue of Commands
13: * in a LinkedBlockingQueue. This the run() method loops for continously until shutdown() is
14: * called.
15: *
16: */
17: public class CommandExecutor implements Runnable, Serializable {
18: private WorkingMemory workingMemory;
19: private BlockingQueue queue;
20:
21: private volatile boolean run;
22:
23: public CommandExecutor(WorkingMemory workingMemory) {
24: this .workingMemory = workingMemory;
25: this .queue = new LinkedBlockingQueue();
26: }
27:
28: /**
29: * Allows the looping run() method to execute.
30: *
31: */
32: public void shutdown() {
33: this .run = false;
34: }
35:
36: /**
37: * Submit a Command for execution
38: *
39: * @param command
40: *
41: * @return
42: * return the Future
43: */
44: public Future submit(Command command) {
45: this .queue.offer(command);
46: // we know our commands also implement Future
47: return (Future) command;
48: }
49:
50: public void run() {
51: this .run = true;
52: while (this .run) {
53: try {
54: Command executor = (Command) this .queue.take();
55: executor.execute(this .workingMemory);
56: } catch (InterruptedException e) {
57: return;
58: }
59: }
60: }
61: }
|