Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.axonframework.eventhandling.scheduling.java;
 
 
An org.axonframework.eventhandling.scheduling.EventScheduler implementation that uses Java's ScheduledExecutorService as scheduling and triggering mechanism.

Note that this mechanism is non-persistent. Scheduled tasks will be lost when the JVM is shut down, unless special measures have been taken to prevent that. For more flexible and powerful scheduling options, see org.axonframework.eventhandling.scheduling.quartz.QuartzEventScheduler.

 
 public class SimpleEventScheduler implements EventScheduler {
 
     private static final Logger logger = LoggerFactory.getLogger(SimpleEventScheduler.class);
 
     private final ScheduledExecutorService executorService;
     private final EventBus eventBus;
     private final EventTriggerCallback eventTriggerCallback;

    
Initialize the SimpleEventScheduler using the given executorService as trigger and execution mechanism, and publishes events to the given eventBus.

Parameters:
executorService The backing ScheduledExecutorService
eventBus The Event Bus on which Events are to be published
 
     public SimpleEventScheduler(ScheduledExecutorService executorServiceEventBus eventBus) {
         this(executorServiceeventBusnew NoOpEventTriggerCallback());
     }

    
Initialize the SimpleEventScheduler using the given executorService as trigger and execution mechanism, and publishes events to the given eventBus. The eventTriggerCallback is invoked just before and after publication of a scheduled event.

Parameters:
executorService The backing ScheduledExecutorService
eventBus The Event Bus on which Events are to be published
eventTriggerCallback To notify when an event is published
 
     public SimpleEventScheduler(ScheduledExecutorService executorServiceEventBus eventBus,
                                 EventTriggerCallback eventTriggerCallback) {
         Assert.notNull(executorService"The ScheduledExecutorService may not be null");
         Assert.notNull(eventBus"The EventBus may not be null");
 
         this. = executorService;
         this. = eventBus;
         this. = eventTriggerCallback;
     }
 
     @Override
     public ScheduleToken schedule(DateTime triggerDateTimeApplicationEvent event) {
         return schedule(new Duration(nulltriggerDateTime), event);
     }
 
     @Override
     public ScheduleToken schedule(Duration triggerDurationApplicationEvent event) {
         ScheduledFuture<?> future = .schedule(new PublishEventTask(event),
                                                              triggerDuration.getMillis(),
                                                              .);
         return new SimpleScheduleToken(future);
     }
 
     @Override
     public ScheduleToken schedule(ScheduledEvent event) {
        return schedule(event.getScheduledTime(), event);
    }
    @Override
    public void cancelSchedule(ScheduleToken scheduleToken) {
        if (!SimpleScheduleToken.class.isInstance(scheduleToken)) {
            throw new IllegalArgumentException("The given ScheduleToken was not provided by this scheduler.");
        }
        SimpleScheduleToken token = (SimpleScheduleTokenscheduleToken;
        token.getFuture().cancel(false);
    }
    private class PublishEventTask implements Runnable {
        private final ApplicationEvent event;
        public PublishEventTask(ApplicationEvent event) {
            this. = event;
        }
        @Override
        public void run() {
            try {
                .publish();
            } catch (RuntimeException e) {
                .afterPublicationFailure(e);
                throw e;
            }
        }
    }
    private static class NoOpEventTriggerCallback implements EventTriggerCallback {
        @Override
        public void beforePublication(ApplicationEvent event) {
        }
        @Override
        public void afterPublicationSuccess() {
        }
        @Override
        public void afterPublicationFailure(RuntimeException cause) {
            .error("An exception occurred while processing a scheduled Event."cause);
        }
    }
New to GrepCode? Check out our FAQ X