1 package net.sf.appstatus.batch.jdbc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import net.sf.appstatus.core.batch.AbstractBatchProgressMonitor;
20 import net.sf.appstatus.core.batch.IBatch;
21 import net.sf.appstatus.core.batch.IBatchProgressMonitor;
22 import net.sf.appstatus.core.batch.IBatchProgressMonitorExt;
23
24 import org.apache.commons.lang3.builder.ToStringBuilder;
25 import org.springframework.util.StringUtils;
26
27
28
29
30
31
32
33 public class JdbcBatchProgressMonitor extends AbstractBatchProgressMonitor implements IBatchProgressMonitorExt {
34 private JdbcBatchManager manager;
35
36 BatchDao batchDao;
37
38 private long lastDbSave;
39
40
41
42
43
44
45
46
47
48
49
50 private JdbcBatchProgressMonitor(String executionId, JdbcBatchProgressMonitor parent, int parentWork, Batch batch,
51 BatchDao bachDao) {
52 super(executionId, parent, parentWork, batch);
53 this.batchDao = bachDao;
54 }
55
56
57
58
59
60
61
62 public JdbcBatchProgressMonitor(String executionId, IBatch batch, BatchDao bachDao) {
63 super(executionId, batch);
64 this.batchDao = bachDao;
65
66 }
67
68 @Override
69 public void done() {
70 super.done();
71 updateDb(true);
72 }
73
74 @Override
75 public void fail(String reason) {
76 super.fail(reason);
77 updateDb(true);
78 }
79
80 @Override
81 public void fail(String reason, Throwable t) {
82 super.fail(reason, t);
83 updateDb(true);
84 }
85
86 @Override
87 public void reject(String itemId, String reason) {
88 super.reject(itemId, reason);
89 updateDb(true);
90 }
91
92 @Override
93 public void reject(String[] itemIds, String reason) {
94 super.reject(itemIds, reason);
95 updateDb(true);
96 }
97
98 @Override
99 public void reject(String[] itemIds, String reason, Throwable e) {
100 super.reject(itemIds, reason, e);
101 updateDb(true);
102 }
103
104 @Override
105 public Batch getBatch() {
106 return (Batch) super.getBatch();
107 }
108
109 @Override
110 public void beginTask(String name, String description, int totalWork) {
111 super.beginTask(name, description, totalWork);
112 updateDb(true);
113 }
114
115 @Override
116 protected JdbcBatchProgressMonitor getMainMonitor() {
117 return (JdbcBatchProgressMonitor) super.getMainMonitor();
118 }
119
120 private boolean isLoggable(long lastWriteTimestamp) {
121 if (System.currentTimeMillis() - lastWriteTimestamp > getWritingDelay()) {
122 return true;
123 }
124 return false;
125 }
126
127 @Override
128 public void message(String message) {
129 super.message(message);
130
131 updateDb(true);
132
133 }
134
135 @Override
136 protected IBatchProgressMonitor newInstance(int work) {
137 return new JdbcBatchProgressMonitor(executionId, this, work, getBatch(), batchDao);
138 }
139
140 private String readableStatus() {
141 if (!getMainMonitor().isDone()) {
142 return IBatch.STATUS_RUNNING;
143 }
144
145 if (getMainMonitor().isSuccess()) {
146 return IBatch.STATUS_SUCCESS;
147 }
148
149 return IBatch.STATUS_FAILURE;
150 }
151
152 @Override
153 public void reject(String itemId, String reason, Throwable e) {
154 super.reject(itemId, reason, e);
155 updateDb(true);
156 }
157
158 @Override
159 public void setCurrentItem(Object item) {
160 super.setCurrentItem(item);
161
162 updateDb(false);
163 }
164
165 @Override
166 protected void onBatchEnd() {
167 getMainMonitor().getManager().batchEnd(getBatch());
168 }
169
170 private void updateDb(boolean force) {
171
172 if (force || isLoggable(lastDbSave)) {
173 try {
174 lastDbSave = System.currentTimeMillis();
175 getBatch().getBdBatch().setStatus(readableStatus());
176
177
178 String dbCurrentItem = null;
179 if( currentItem != null ){
180
181 String toString =currentItem.toString();
182 dbCurrentItem =toString.substring(0, Math.min(254, toString.length()));
183 }
184 getBatch().getBdBatch().setCurrentItem(dbCurrentItem);
185
186 if (!org.apache.commons.lang3.StringUtils.isEmpty(getMainMonitor().getLastMessage())
187 && getMainMonitor().getLastMessage().length() > 1024) {
188 getBatch().getBdBatch().setLastMessage(getMainMonitor().getLastMessage().substring(0, 1023));
189 } else {
190 getBatch().getBdBatch().setLastMessage(getMainMonitor().getLastMessage());
191 }
192 getBatch().getBdBatch().setStartDate(getMainMonitor().getStartDate());
193 getBatch().getBdBatch().setEndDate(getMainMonitor().getEndDate());
194 getBatch().getBdBatch().setCurrentTask(taskName);
195 getBatch().getBdBatch().setProgress(
196 getMainMonitor().getProgress() == -1f ? -1 : getMainMonitor().getProgress() * 100f
197 / getMainMonitor().getTotalWork());
198 getBatch().getBdBatch().setLastUpdate(getMainMonitor().getLastUpdate());
199 getBatch().getBdBatch().setSuccess(getMainMonitor().isSuccess());
200 getBatch().getBdBatch().setReject(
201 StringUtils.collectionToDelimitedString(getMainMonitor().getRejectedItems(), "|"));
202 getBatch().getBdBatch().setItemCount(getMainMonitor().getItemCount());
203 batchDao.update(getBatch().getBdBatch());
204 } catch (Exception e) {
205 getLogger().error("Error when updating batch table {}",
206 ToStringBuilder.reflectionToString(getBatch().getBdBatch()), e);
207 }
208 }
209 }
210
211
212
213
214 @Override
215 public void worked(int work) {
216 super.worked(work);
217
218 updateDb(false);
219
220 }
221
222 protected void setManager(JdbcBatchManager manager) {
223 this.manager = manager;
224 }
225
226 protected JdbcBatchManager getManager() {
227 return manager;
228 }
229 }