1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.epcis.repository.query;
22
23 import java.io.Serializable;
24 import java.util.Calendar;
25 import java.util.Date;
26
27 import javax.management.Notification;
28 import javax.management.NotificationListener;
29 import javax.management.timer.Timer;
30
31 import org.fosstrak.epcis.model.ImplementationException;
32 import org.fosstrak.epcis.model.QueryParams;
33 import org.fosstrak.epcis.soap.ImplementationExceptionResponse;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
38
39
40
41
42
43
44 public class QuerySubscriptionScheduled extends QuerySubscription implements NotificationListener, Serializable {
45
46 private static final long serialVersionUID = -5073503857856387167L;
47
48 private static final Log LOG = LogFactory.getLog(QuerySubscriptionScheduled.class);
49
50
51
52
53 protected Schedule schedule;
54
55
56
57
58 protected Boolean doItAgain = Boolean.TRUE;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public QuerySubscriptionScheduled(final String subscriptionID, final QueryParams queryParams, final String dest,
83 final Boolean reportIfEmpty, final Calendar initialRecordTime,
84 final Calendar lastTimeExecuted, final Schedule schedule, final String queryName)
85 throws ImplementationExceptionResponse {
86 super(subscriptionID, queryParams, dest, reportIfEmpty, initialRecordTime, lastTimeExecuted, queryName);
87 this.schedule = schedule;
88 if (LOG.isDebugEnabled()) {
89 Date nextSchedule = schedule.nextScheduledTime().getTime();
90 LOG.debug("Next scheduled time for the subscribed query is '" + nextSchedule + "'.");
91 LOG.debug("URI to which to send results for the subscribed query is " + dest.toString());
92 }
93 startThread();
94 }
95
96
97
98
99
100
101
102 private void startThread() throws ImplementationExceptionResponse {
103 Timer nextAction = new Timer();
104 nextAction.addNotificationListener(this, null, nextAction);
105
106 Date nextSchedule = schedule.nextScheduledTime().getTime();
107 nextAction.addNotification("SubscriptionSchedule", "Please do the query", null, nextSchedule);
108 nextAction.start();
109 }
110
111
112
113
114
115 public void stopSubscription() {
116 doItAgain = Boolean.FALSE;
117 }
118
119
120
121
122 protected void finalize() {
123 LOG.debug("A subscribed query has been garbage collected.");
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public void handleNotification(final Notification pNotification, final Object pHandback) {
139 if (pHandback == null) {
140 LOG.error("The timer stating the next scheduled query execution time is null!");
141 return;
142 }
143 Timer timer = (Timer) pHandback;
144
145 if (!doItAgain.booleanValue()) {
146 timer.stop();
147 } else {
148
149 executeQuery();
150 setNextScheduledExecutionTime(timer);
151 }
152 }
153
154
155
156
157
158
159
160
161 protected void setNextScheduledExecutionTime(final Timer timer) throws IllegalArgumentException {
162 try {
163 Date nextSchedule = schedule.nextScheduledTime().getTime();
164 LOG.debug("Next scheduled time for the subscribed query is '" + nextSchedule + "'.");
165 timer.addNotification("SubscriptionSchedule", "Please do the query", timer, nextSchedule);
166 } catch (ImplementationExceptionResponse e) {
167 String msg = "The next scheduled date for the subscribed query with ID '" + getSubscriptionID()
168 + "' cannot be evaluated: " + e.getMessage();
169 LOG.error(msg, e);
170 }
171 }
172
173 }