1 package net.sf.appstatus.batch.jdbc;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.List;
6 import java.util.Properties;
7 import java.util.Vector;
8
9 import net.sf.appstatus.core.batch.IBatch;
10 import net.sf.appstatus.core.batch.IBatchManager;
11 import net.sf.appstatus.core.batch.IBatchProgressMonitor;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class JdbcBatchManager implements IBatchManager {
17
18 private static Logger logger = LoggerFactory
19 .getLogger(JdbcBatchManager.class);
20
21 private BatchDao batchDao;
22 List<IBatch> runningBatches = new Vector<IBatch>();
23 private int logInterval;
24
25 private int zombieInterval;
26
27 public void setBatchDao(BatchDao batchDao) {
28 this.batchDao = batchDao;
29 }
30
31 public void batchEnd(Batch batch) {
32 runningBatches.remove(batch);
33 }
34
35 public IBatch addBatch(String name, String group, String uuid) {
36 BdBatch bdBatch = new BdBatch();
37 bdBatch.setName(name);
38 bdBatch.setGroup(group);
39 bdBatch.setUuid(uuid);
40 bdBatch.setStartDate(new Date());
41 bdBatch.setStatus(Batch.STATUS_RUNNING);
42
43 IBatch b = new Batch(bdBatch);
44 ((Batch) b).setZombieInterval(zombieInterval);
45 int currentPosition = runningBatches.indexOf(b);
46 if (currentPosition >= 0) {
47
48 b = runningBatches.get(currentPosition);
49 } else {
50
51
52
53 runningBatches.add(b);
54
55 batchDao.save(bdBatch);
56 }
57
58 return b;
59 }
60
61 protected List<IBatch> convertToIBatch(List<BdBatch> bdBaches) {
62 List<IBatch> result = null;
63 if (bdBaches != null) {
64 result = new ArrayList<IBatch>();
65 for (BdBatch bdb : bdBaches) {
66 Batch b = new Batch(bdb);
67 b.setZombieInterval(zombieInterval);
68
69 result.add(b);
70 }
71 }
72 return result;
73 }
74
75 public List<IBatch> getErrorBatches() {
76 return convertToIBatch(batchDao.fetchError(25));
77 }
78
79 public List<IBatch> getFinishedBatches() {
80 return convertToIBatch(batchDao.fetchFinished(25));
81 }
82
83 public IBatchProgressMonitor getMonitor(IBatch batch) {
84 Batch b = (Batch) batch;
85 if (b.getProgressMonitor() == null) {
86 JdbcBatchProgressMonitor monitor = new JdbcBatchProgressMonitor(
87 batch.getUuid(), batch, batchDao);
88 monitor.setManager(this);
89 monitor.setWritingDelay(logInterval);
90 }
91 return b.getProgressMonitor();
92 }
93
94 public List<IBatch> getRunningBatches() {
95 return convertToIBatch(batchDao.fetchRunning(25));
96
97 }
98
99
100
101
102
103
104 public void removeAllBatches(int scope) {
105 switch (scope) {
106 case IBatchManager.REMOVE_OLD:
107 batchDao.deleteOldBatches(6);
108 break;
109
110 default:
111 batchDao.deleteSuccessBatches();
112 break;
113 }
114 }
115
116 public void removeBatch(String uuid) {
117 batchDao.deleteBatch(uuid);
118 }
119
120 public void setConfiguration(Properties configuration) {
121 try {
122 logInterval = Integer.parseInt(configuration
123 .getProperty("batch.logInterval"));
124 } catch (NumberFormatException e) {
125 logInterval = 1000;
126 }
127 logger.info("Batch log interval: {}ms", logInterval);
128
129 try {
130 zombieInterval = Integer.parseInt(configuration
131 .getProperty("batch.zombieInterval"));
132 } catch (NumberFormatException e) {
133 zombieInterval = 1000 * 60 * 10;
134 }
135 logger.info("Zombie interval: {}", zombieInterval);
136
137 }
138
139 public Properties getConfiguration() {
140 Properties props = new Properties();
141 props.put("batch.logInterval", String.valueOf(logInterval));
142 props.put("batch.zombieInterval", String.valueOf(zombieInterval));
143 return props;
144 }
145
146 public void init() {
147
148 batchDao.createDbIfNecessary();
149 }
150
151 public List<IBatch> getBatches(String group, String name) {
152 return convertToIBatch(batchDao.fetch(group, name, 25));
153 }
154 }