1
2
3
4 package net.sf.provisioner.core;
5
6 import net.sf.provisioner.adapters.SQLAdapter;
7 import net.sf.provisioner.config.Configuration;
8
9 import org.apache.log4j.Logger;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class Producer {
25
26
27 Logger logger = Logger.getLogger(getClass());
28
29 public boolean isRunning = false;
30 long timeInterval;
31 String configPath;
32 int maxConsumers;
33
34 SQLAdapter operationQueue;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public Producer(long timeInterval, String configPath, int maxConsumers) {
52
53 this.timeInterval = timeInterval;
54 this.configPath = configPath;
55 this.maxConsumers = maxConsumers;
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public void Start(Configuration config) throws Exception{
74
75
76
77
78 this.operationQueue = new SQLAdapter(config.db);
79
80 try {
81
82 this.isRunning = true;
83
84 while (isRunning) {
85
86 logger.info("Retrieving pending operations...");
87 Operation[] operations = this.operationQueue.getPendingOperations(this.maxConsumers);
88
89 for (int i = 0; i < this.maxConsumers; i++) {
90
91 if (!operations[i].getId().equalsIgnoreCase("0")) {
92
93 logger.info("Create consumer to process operation number: " + operations[i].getId());
94 Consumer consumer = new Consumer(operations[i], config);
95 }
96 }
97 try {
98 Thread.sleep(this.timeInterval);
99 }
100 catch (Exception e) {
101 throw e;
102 }
103 }
104
105 } catch (Exception e) {
106
107 throw e;
108 }
109
110 finally {
111
112 this.isRunning = false;
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public void Stop() {
132 this.isRunning = false;
133 }
134 }