Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Convert to jsr-330.
Browse files Browse the repository at this point in the history
Allow beforeRun() and afterRun() to throw exceptions.
  • Loading branch information
jdillon committed Feb 7, 2013
1 parent 7942a53 commit 89aad19
Showing 1 changed file with 23 additions and 8 deletions.
Expand Up @@ -19,7 +19,6 @@
import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.codehaus.plexus.component.annotations.Requirement;
import org.sonatype.nexus.scheduling.events.NexusTaskEventStarted;
import org.sonatype.nexus.scheduling.events.NexusTaskEventStoppedCanceled;
import org.sonatype.nexus.scheduling.events.NexusTaskEventStoppedDone;
Expand All @@ -33,14 +32,16 @@
import org.sonatype.sisu.goodies.eventbus.EventBus;
import com.google.common.base.Throwables;

import javax.inject.Inject;

import static com.google.common.base.Preconditions.checkState;

public abstract class AbstractNexusTask<T>
extends AbstractSchedulerTask<T>
implements NexusTask<T>
{

public static final long A_DAY = 24L * 60L * 60L * 1000L;

@Requirement
private EventBus eventBus;

protected AbstractNexusTask()
Expand All @@ -66,6 +67,18 @@ protected AbstractNexusTask( final EventBus eventBus, final String name )
this.eventBus = eventBus;
}

protected EventBus getEventBus()
{
checkState( eventBus != null );
return eventBus;
}

@Inject
public void setEventBus( final EventBus eventBus )
{
this.eventBus = eventBus;
}

protected void notifyEventListeners( final Event<?> event )
{
eventBus.post( event );
Expand Down Expand Up @@ -151,7 +164,7 @@ public final T call()

// fire event
final NexusTaskEventStarted<T> startedEvent = new NexusTaskEventStarted<T>( this );
eventBus.post( startedEvent );
getEventBus().post(startedEvent);

T result = null;

Expand All @@ -169,13 +182,13 @@ public final T call()
{
getLogger().info( getLoggedMessage( "canceled", started ) );

eventBus.post( new NexusTaskEventStoppedCanceled<T>( this, startedEvent ) );
getEventBus().post(new NexusTaskEventStoppedCanceled<T>(this, startedEvent));
}
else
{
getLogger().info( getLoggedMessage( "finished", started ) );

eventBus.post( new NexusTaskEventStoppedDone<T>( this, startedEvent ) );
getEventBus().post(new NexusTaskEventStoppedDone<T>(this, startedEvent));
}

afterRun();
Expand All @@ -191,7 +204,7 @@ public final T call()
getLogger().info( getLoggedMessage( "canceled", started ) );

// just return, nothing happened just task cancelled
eventBus.post( new NexusTaskEventStoppedCanceled<T>( this, startedEvent ) );
getEventBus().post(new NexusTaskEventStoppedCanceled<T>(this, startedEvent));

return null;
}
Expand All @@ -200,7 +213,7 @@ public final T call()
getLogger().warn( getLoggedMessage( "failed", started ), e );

// notify that there was a failure
eventBus.post( new NexusTaskEventStoppedFailed<T>( this, startedEvent, e ) );
getEventBus().post(new NexusTaskEventStoppedFailed<T>(this, startedEvent, e));

Throwables.propagateIfInstanceOf( e, Exception.class );
throw Throwables.propagate( e );
Expand All @@ -226,6 +239,7 @@ protected String getLoggedMessage( final String action, final long started )
}

protected void beforeRun()
throws Exception
{
// override if needed
}
Expand All @@ -234,6 +248,7 @@ protected abstract T doRun()
throws Exception;

protected void afterRun()
throws Exception
{
// override if needed
}
Expand Down

0 comments on commit 89aad19

Please sign in to comment.