Skip to content

Commit de72251

Browse files
committed
Revert to string time representation and DateFormat
1 parent c910c06 commit de72251

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

policy/src/main/java/org/apache/brooklyn/policy/action/AbstractScheduledEffectorPolicy.java

+23-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package org.apache.brooklyn.policy.action;
2020

21+
import java.text.DateFormat;
22+
import java.text.ParseException;
23+
import java.text.SimpleDateFormat;
2124
import java.time.LocalTime;
2225
import java.util.Calendar;
2326
import java.util.Date;
@@ -56,6 +59,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
5659

5760
private static final Logger LOG = LoggerFactory.getLogger(AbstractScheduledEffectorPolicy.class);
5861

62+
private static final String TIME_FORMAT = "HH:mm:ss";
63+
private static final DateFormat FORMATTER = SimpleDateFormat.getTimeInstance();
64+
5965
public static final ConfigKey<String> EFFECTOR = ConfigKeys.builder(String.class)
6066
.name("effector")
6167
.description("The effector to be executed by this policy")
@@ -69,9 +75,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
6975
.defaultValue(ImmutableMap.<String, Object>of())
7076
.build();
7177

72-
public static final ConfigKey<Date> TIME = ConfigKeys.builder(Date.class)
78+
public static final ConfigKey<String> TIME = ConfigKeys.builder(String.class)
7379
.name("time")
74-
.description("An optional time when this policy should be first executed")
80+
.description("An optional time when this policy should be first executed, formatted as HH:mm:ss")
7581
.build();
7682

7783
public static final ConfigKey<Duration> WAIT = ConfigKeys.builder(Duration.class)
@@ -113,15 +119,22 @@ protected Effector<?> getEffector() {
113119
return effector.get();
114120
}
115121

116-
protected Duration getWaitUntil(Date time) {
117-
Calendar now = Calendar.getInstance();
118-
Calendar when = Calendar.getInstance();
119-
when.setTime(time);
120-
when.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
121-
if (when.before(now)) {
122-
when.add(Calendar.DATE, 1);
122+
protected Duration getWaitUntil(String time) {
123+
try {
124+
Calendar now = Calendar.getInstance();
125+
Calendar when = Calendar.getInstance();
126+
boolean formatted = time.contains(":"); // FIXME deprecated TimeDuration coercion
127+
Date parsed = formatted ? FORMATTER.parse(time) : new Date(Long.parseLong(time) * 1000);
128+
when.setTime(parsed);
129+
when.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
130+
if (when.before(now)) {
131+
when.add(Calendar.DATE, 1);
132+
}
133+
return Duration.millis(Math.max(0, when.getTimeInMillis() - now.getTimeInMillis()));
134+
} catch (ParseException | NumberFormatException e) {
135+
LOG.warn("{}: Time should be formatted as {}: {}", new Object[] { this, TIME_FORMAT, e.getMessage() });
136+
throw Exceptions.propagate(e);
123137
}
124-
return Duration.millis(Math.max(0, when.getTimeInMillis() - now.getTimeInMillis()));
125138
}
126139

127140
@Override

policy/src/main/java/org/apache/brooklyn/policy/action/PeriodicEffectorPolicy.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void onEvent(SensorEvent<Object> event) {
9292
Boolean start = (Boolean) event.getValue();
9393
if (start && running.compareAndSet(false, true)) {
9494
Duration period = Preconditions.checkNotNull(config().get(PERIOD), "The period must be configured for this policy");
95-
Date time = config().get(TIME);
95+
String time = config().get(TIME);
9696
Duration wait = config().get(WAIT);
9797
if (time != null) {
9898
wait = getWaitUntil(time);
@@ -103,7 +103,6 @@ public void onEvent(SensorEvent<Object> event) {
103103
LOG.debug("{}: Scheduling {} every {} in {}", new Object[] { PeriodicEffectorPolicy.this, effector.getName(),
104104
Time.fromDurationToTimeStringRounded().apply(period), Time.fromDurationToTimeStringRounded().apply(wait) });
105105
executor.scheduleAtFixedRate(PeriodicEffectorPolicy.this, wait.toMilliseconds(), period.toMilliseconds(), TimeUnit.MILLISECONDS);
106-
LOG.debug("{}: Scheduled", PeriodicEffectorPolicy.this);
107106
}
108107
}
109108
}

policy/src/main/java/org/apache/brooklyn/policy/action/ScheduledEffectorPolicy.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setEntity(final EntityLocal entity) {
6969
subscriptions().subscribe(entity, INVOKE_IMMEDIATELY, handler);
7070
subscriptions().subscribe(entity, INVOKE_AT, handler);
7171

72-
Date time = config().get(TIME);
72+
String time = config().get(TIME);
7373
Duration wait = config().get(WAIT);
7474
if (time != null) {
7575
scheduleAt(time);
@@ -78,7 +78,7 @@ public void setEntity(final EntityLocal entity) {
7878
}
7979
}
8080

81-
protected void scheduleAt(Date time) {
81+
protected void scheduleAt(String time) {
8282
Duration wait = getWaitUntil(time);
8383
LOG.debug("{}: Scheduling {} at {} (in {})", new Object[] { this, effector.getName(), time, Time.fromDurationToTimeStringRounded().apply(wait) });
8484
executor.schedule(this, wait.toMilliseconds(), TimeUnit.MILLISECONDS);
@@ -90,7 +90,7 @@ public void onEvent(SensorEvent<Object> event) {
9090
synchronized (mutex) {
9191
LOG.debug("{}: Got event {}", ScheduledEffectorPolicy.this, event);
9292
if (event.getSensor().getName().equals(INVOKE_AT.getName())) {
93-
Date time = (Date) event.getValue();
93+
String time = (String) event.getValue();
9494
if (time != null) {
9595
scheduleAt(time);
9696
}

0 commit comments

Comments
 (0)