From de7225180c6a0996cd47075a155c2f84455f242c Mon Sep 17 00:00:00 2001 From: Andrew Donald Kennedy Date: Thu, 6 Apr 2017 01:15:08 +0100 Subject: [PATCH] Revert to string time representation and DateFormat --- .../AbstractScheduledEffectorPolicy.java | 33 +++++++++++++------ .../policy/action/PeriodicEffectorPolicy.java | 3 +- .../action/ScheduledEffectorPolicy.java | 6 ++-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/policy/src/main/java/org/apache/brooklyn/policy/action/AbstractScheduledEffectorPolicy.java b/policy/src/main/java/org/apache/brooklyn/policy/action/AbstractScheduledEffectorPolicy.java index 2efa56ff56..eaf5883aff 100644 --- a/policy/src/main/java/org/apache/brooklyn/policy/action/AbstractScheduledEffectorPolicy.java +++ b/policy/src/main/java/org/apache/brooklyn/policy/action/AbstractScheduledEffectorPolicy.java @@ -18,6 +18,9 @@ */ package org.apache.brooklyn.policy.action; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.time.LocalTime; import java.util.Calendar; import java.util.Date; @@ -56,6 +59,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp private static final Logger LOG = LoggerFactory.getLogger(AbstractScheduledEffectorPolicy.class); + private static final String TIME_FORMAT = "HH:mm:ss"; + private static final DateFormat FORMATTER = SimpleDateFormat.getTimeInstance(); + public static final ConfigKey EFFECTOR = ConfigKeys.builder(String.class) .name("effector") .description("The effector to be executed by this policy") @@ -69,9 +75,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp .defaultValue(ImmutableMap.of()) .build(); - public static final ConfigKey TIME = ConfigKeys.builder(Date.class) + public static final ConfigKey TIME = ConfigKeys.builder(String.class) .name("time") - .description("An optional time when this policy should be first executed") + .description("An optional time when this policy should be first executed, formatted as HH:mm:ss") .build(); public static final ConfigKey WAIT = ConfigKeys.builder(Duration.class) @@ -113,15 +119,22 @@ protected Effector getEffector() { return effector.get(); } - protected Duration getWaitUntil(Date time) { - Calendar now = Calendar.getInstance(); - Calendar when = Calendar.getInstance(); - when.setTime(time); - when.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE)); - if (when.before(now)) { - when.add(Calendar.DATE, 1); + protected Duration getWaitUntil(String time) { + try { + Calendar now = Calendar.getInstance(); + Calendar when = Calendar.getInstance(); + boolean formatted = time.contains(":"); // FIXME deprecated TimeDuration coercion + Date parsed = formatted ? FORMATTER.parse(time) : new Date(Long.parseLong(time) * 1000); + when.setTime(parsed); + when.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE)); + if (when.before(now)) { + when.add(Calendar.DATE, 1); + } + return Duration.millis(Math.max(0, when.getTimeInMillis() - now.getTimeInMillis())); + } catch (ParseException | NumberFormatException e) { + LOG.warn("{}: Time should be formatted as {}: {}", new Object[] { this, TIME_FORMAT, e.getMessage() }); + throw Exceptions.propagate(e); } - return Duration.millis(Math.max(0, when.getTimeInMillis() - now.getTimeInMillis())); } @Override diff --git a/policy/src/main/java/org/apache/brooklyn/policy/action/PeriodicEffectorPolicy.java b/policy/src/main/java/org/apache/brooklyn/policy/action/PeriodicEffectorPolicy.java index 43a302cddd..d1322244d8 100644 --- a/policy/src/main/java/org/apache/brooklyn/policy/action/PeriodicEffectorPolicy.java +++ b/policy/src/main/java/org/apache/brooklyn/policy/action/PeriodicEffectorPolicy.java @@ -92,7 +92,7 @@ public void onEvent(SensorEvent event) { Boolean start = (Boolean) event.getValue(); if (start && running.compareAndSet(false, true)) { Duration period = Preconditions.checkNotNull(config().get(PERIOD), "The period must be configured for this policy"); - Date time = config().get(TIME); + String time = config().get(TIME); Duration wait = config().get(WAIT); if (time != null) { wait = getWaitUntil(time); @@ -103,7 +103,6 @@ public void onEvent(SensorEvent event) { LOG.debug("{}: Scheduling {} every {} in {}", new Object[] { PeriodicEffectorPolicy.this, effector.getName(), Time.fromDurationToTimeStringRounded().apply(period), Time.fromDurationToTimeStringRounded().apply(wait) }); executor.scheduleAtFixedRate(PeriodicEffectorPolicy.this, wait.toMilliseconds(), period.toMilliseconds(), TimeUnit.MILLISECONDS); - LOG.debug("{}: Scheduled", PeriodicEffectorPolicy.this); } } } diff --git a/policy/src/main/java/org/apache/brooklyn/policy/action/ScheduledEffectorPolicy.java b/policy/src/main/java/org/apache/brooklyn/policy/action/ScheduledEffectorPolicy.java index bfa931b1c4..3be57b9fe8 100644 --- a/policy/src/main/java/org/apache/brooklyn/policy/action/ScheduledEffectorPolicy.java +++ b/policy/src/main/java/org/apache/brooklyn/policy/action/ScheduledEffectorPolicy.java @@ -69,7 +69,7 @@ public void setEntity(final EntityLocal entity) { subscriptions().subscribe(entity, INVOKE_IMMEDIATELY, handler); subscriptions().subscribe(entity, INVOKE_AT, handler); - Date time = config().get(TIME); + String time = config().get(TIME); Duration wait = config().get(WAIT); if (time != null) { scheduleAt(time); @@ -78,7 +78,7 @@ public void setEntity(final EntityLocal entity) { } } - protected void scheduleAt(Date time) { + protected void scheduleAt(String time) { Duration wait = getWaitUntil(time); LOG.debug("{}: Scheduling {} at {} (in {})", new Object[] { this, effector.getName(), time, Time.fromDurationToTimeStringRounded().apply(wait) }); executor.schedule(this, wait.toMilliseconds(), TimeUnit.MILLISECONDS); @@ -90,7 +90,7 @@ public void onEvent(SensorEvent event) { synchronized (mutex) { LOG.debug("{}: Got event {}", ScheduledEffectorPolicy.this, event); if (event.getSensor().getName().equals(INVOKE_AT.getName())) { - Date time = (Date) event.getValue(); + String time = (String) event.getValue(); if (time != null) { scheduleAt(time); }