You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2007/11/05 14:28:14 UTC
svn commit: r591988 - in /incubator/sling/trunk/event/src/main:
java/org/apache/sling/event/impl/EventHelper.java
java/org/apache/sling/event/impl/TimedEventHandler.java
resources/SLING-INF/nodetypes/event.cnd
Author: cziegeler
Date: Mon Nov 5 05:28:13 2007
New Revision: 591988
URL: http://svn.apache.org/viewvc?rev=591988&view=rev
Log:
Implement updating of expired date based timed events (SLING-96)
Modified:
incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/EventHelper.java
incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
incubator/sling/trunk/event/src/main/resources/SLING-INF/nodetypes/event.cnd
Modified: incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/EventHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/EventHelper.java?rev=591988&r1=591987&r2=591988&view=diff
==============================================================================
--- incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/EventHelper.java (original)
+++ incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/EventHelper.java Mon Nov 5 05:28:13 2007
@@ -32,6 +32,9 @@
public static final String NODE_PROPERTY_JOBID = "slingevent:id";
public static final String NODE_PROPERTY_ACTIVE = "slingevent:active";
public static final String NODE_PROPERTY_FINISHED = "slingevent:finished";
+ public static final String NODE_PROPERTY_TE_EXPRESSION = "slingevent:expression";
+ public static final String NODE_PROPERTY_TE_DATE = "slingevent:date";
+ public static final String NODE_PROPERTY_TE_PERIOD = "slingevent:period";
public static final String EVENTS_NODE_TYPE = "slingevent:Events";
public static final String EVENT_NODE_TYPE = "slingevent:Event";
Modified: incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java?rev=591988&r1=591987&r2=591988&view=diff
==============================================================================
--- incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java (original)
+++ incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java Mon Nov 5 05:28:13 2007
@@ -19,6 +19,7 @@
package org.apache.sling.event.impl;
import java.io.Serializable;
+import java.util.Calendar;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
@@ -57,6 +58,10 @@
extends AbstractRepositoryEventHandler
implements Job {
+ protected static final String JOB_TOPIC = "topic";
+
+ protected static final String JOB_CONFIG = "config";
+
/** @scr.reference */
protected Scheduler scheduler;
@@ -167,7 +172,7 @@
protected Object run(Node node) throws RepositoryException {
final String jobId = scheduleInfo.getJobId();
// if there is a node, we know that there is exactly one node
- final Node foundNode = queryJob(jobId);
+ Node foundNode = queryJob(jobId);
if ( scheduleInfo.isStopEvent() ) {
// if this is a stop event, we should remove the node from the repository
// if there is no node someone else was faster and we can ignore this
@@ -185,13 +190,30 @@
}
// we only write the event if this is a local one
if ( EventUtil.isLocal(event) ) {
+ // if the node is present, we check if the timed event is based on
+ // a date and has already expired
+ if ( foundNode != null && scheduleInfo.date != null && foundNode.hasProperty(EventHelper.NODE_PROPERTY_TE_DATE) ) {
+ final Calendar oldScheduledDate = foundNode.getProperty(EventHelper.NODE_PROPERTY_TE_DATE).getDate();
+ final Calendar now = Calendar.getInstance();
+ if ( oldScheduledDate.compareTo(now) <= 0 ) {
+ // try to remove the node
+ try {
+ foundNode.remove();
+ parentNode.save();
+ foundNode = null;
+ } catch (LockException le) {
+ // if someone else has the lock this is fine
+ }
+ }
+ }
+
// if node is not present, we'll write it, lock it and schedule the event
if ( foundNode == null ) {
final Node eventNode = writeEvent(event);
return eventNode.lock(false, true);
}
// node is already in repository, this is an error as we don't support updates
- // of timed events!
+ // of timed events! (stopping and recreating is the way to go)
logger.error("Timed event is already scheduled: " + event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_TOPIC) + " (" + scheduleInfo.getJobId() + ")");
}
return null;
@@ -246,30 +268,30 @@
final Map<String, Serializable> config = new HashMap<String, Serializable>();
// copy properties
final Hashtable<String, Object> properties = new Hashtable<String, Object>();
- config.put("topic", (String)event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_TOPIC));
+ config.put(JOB_TOPIC, (String)event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_TOPIC));
final String[] names = event.getPropertyNames();
if ( names != null ) {
for(int i=0; i<names.length; i++) {
properties.put(names[i], event.getProperty(names[i]));
}
}
- config.put("config", properties);
+ config.put(JOB_CONFIG, properties);
try {
if ( scheduleInfo.expression != null ) {
if ( this.logger.isDebugEnabled() ) {
- this.logger.debug("Adding timed event " + config.get("topic") + "(" + scheduleInfo.getJobId() + ")" + " with cron expression " + scheduleInfo.expression);
+ this.logger.debug("Adding timed event " + config.get(JOB_TOPIC) + "(" + scheduleInfo.getJobId() + ")" + " with cron expression " + scheduleInfo.expression);
}
this.scheduler.addJob(scheduleInfo.getJobId(), this, config, scheduleInfo.expression, false);
} else if ( scheduleInfo.period != null ) {
if ( this.logger.isDebugEnabled() ) {
- this.logger.debug("Adding timed event " + config.get("topic") + "(" + scheduleInfo.getJobId() + ")" + " with period " + scheduleInfo.period);
+ this.logger.debug("Adding timed event " + config.get(JOB_TOPIC) + "(" + scheduleInfo.getJobId() + ")" + " with period " + scheduleInfo.period);
}
this.scheduler.addPeriodicJob(scheduleInfo.getJobId(), this, config, scheduleInfo.period, false);
} else {
// then it must be date
if ( this.logger.isDebugEnabled() ) {
- this.logger.debug("Adding timed event " + config.get("topic") + "(" + scheduleInfo.getJobId() + ")" + " with date " + scheduleInfo.date);
+ this.logger.debug("Adding timed event " + config.get(JOB_TOPIC) + "(" + scheduleInfo.getJobId() + ")" + " with date " + scheduleInfo.date);
}
this.scheduler.fireJobAt(scheduleInfo.getJobId(), this, config, scheduleInfo.date);
}
@@ -340,8 +362,8 @@
* @see org.apache.sling.scheduler.Job#execute(org.apache.sling.scheduler.JobContext)
*/
public void execute(JobContext context) {
- final String topic = (String) context.getConfiguration().get("topic");
- final Dictionary<Object, Object> properties = (Dictionary<Object, Object>) context.getConfiguration().get("config");
+ final String topic = (String) context.getConfiguration().get(JOB_TOPIC);
+ final Dictionary<Object, Object> properties = (Dictionary<Object, Object>) context.getConfiguration().get(JOB_CONFIG);
if ( this.eventAdmin != null ) {
try {
this.eventAdmin.postEvent(new Event(topic, properties));
@@ -369,15 +391,20 @@
while ( result.hasNext() ) {
final Node eventNode = result.nextNode();
if ( !eventNode.isLocked() ) {
- final Event event = this.readEvent(eventNode);
- final EventInfo info = new EventInfo();
- info.event = event;
- info.nodePath = eventNode.getPath();
try {
- this.queue.put(info);
- } catch (InterruptedException e) {
- // we ignore this exception as this should never occur
- this.ignoreException(e);
+ final Event event = this.readEvent(eventNode);
+ final EventInfo info = new EventInfo();
+ info.event = event;
+ info.nodePath = eventNode.getPath();
+ try {
+ this.queue.put(info);
+ } catch (InterruptedException e) {
+ // we ignore this exception as this should never occur
+ this.ignoreException(e);
+ }
+ } catch (RepositoryException re) {
+ // if reading an event fails, we ignore this
+ this.ignoreException(re);
}
}
}
@@ -392,6 +419,17 @@
eventNode.setProperty(EventHelper.NODE_PROPERTY_TOPIC, (String)event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_TOPIC));
final ScheduleInfo info = new ScheduleInfo(event);
eventNode.setProperty(EventHelper.NODE_PROPERTY_JOBID, info.getJobId());
+ if ( info.date != null ) {
+ final Calendar c = Calendar.getInstance();
+ c.setTime(info.date);
+ eventNode.setProperty(EventHelper.NODE_PROPERTY_TE_DATE, c);
+ }
+ if ( info.expression != null ) {
+ eventNode.setProperty(EventHelper.NODE_PROPERTY_TE_EXPRESSION, info.expression);
+ }
+ if ( info.period != null ) {
+ eventNode.setProperty(EventHelper.NODE_PROPERTY_TE_PERIOD, info.period.longValue());
+ }
}
/**
Modified: incubator/sling/trunk/event/src/main/resources/SLING-INF/nodetypes/event.cnd
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/event/src/main/resources/SLING-INF/nodetypes/event.cnd?rev=591988&r1=591987&r2=591988&view=diff
==============================================================================
--- incubator/sling/trunk/event/src/main/resources/SLING-INF/nodetypes/event.cnd (original)
+++ incubator/sling/trunk/event/src/main/resources/SLING-INF/nodetypes/event.cnd Mon Nov 5 05:28:13 2007
@@ -45,5 +45,8 @@
[slingevent:TimedEvent] > slingevent:Event, mix:lockable
- slingevent:processor (string)
- slingevent:id (string)
+ - slingevent:expression (string)
+ - slingevent:date (date)
+ - slingevent:period (long)