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.quartz;
 
 
EventScheduler implementation that delegates scheduling and triggering to a Quartz Scheduler.

Author(s):
Allard Buijze
Since:
0.7
 
 
     private static final Logger logger = LoggerFactory.getLogger(QuartzEventScheduler.class);
     private static final String JOB_NAME_PREFIX = "event-";
     private static final String DEFAULT_GROUP_NAME = "AxonFramework-Events";
     private Scheduler scheduler;
     private EventBus eventBus;
     private volatile boolean initialized;
 
     @Override
     public ScheduleToken schedule(DateTime triggerDateTimeApplicationEvent event) {
         Assert.state("Scheduler is not yet initialized");
         Object owner = event.getSource();
         String jobIdentifier =  + event.getEventIdentifier().toString();
         QuartzScheduleToken tr = new QuartzScheduleToken(jobIdentifier);
         try {
             JobDetail jobDetail = new JobDetail(jobIdentifierFireEventJob.class);
             jobDetail.getJobDataMap().put(.event);
             jobDetail.setDescription(String.format("%s, scheduled by %s.",
                                                    event.getClass().getName(),
                                                    owner.toString()));
             .scheduleJob(jobDetailnew SimpleTrigger(event.getEventIdentifier().toString(),
                                                                triggerDateTime.toDate()));
         } catch (SchedulerException e) {
             throw new SchedulingException("An error occurred while setting a timer for a saga"e);
         }
         return tr;
     }
 
     @Override
     public ScheduleToken schedule(Duration triggerDurationApplicationEvent event) {
         return schedule(new DateTime().plus(triggerDuration), event);
     }
 
     @Override
     public ScheduleToken schedule(ScheduledEvent event) {
         return schedule(event.getScheduledTime(), event);
     }
 
     @Override
     public void cancelSchedule(ScheduleToken scheduleToken) {
         if (!QuartzScheduleToken.class.isInstance(scheduleToken)) {
             throw new IllegalArgumentException("The given ScheduleToken was not provided by this scheduler.");
         }
         Assert.state("Scheduler is not yet initialized");
 
         QuartzScheduleToken reference = (QuartzScheduleTokenscheduleToken;
         try {
             if (!.deleteJob(reference.getJobIdentifier(), reference.getGroupIdentifier())) {
                 .warn("The job belonging to this token could not be deleted.");
             }
         } catch (SchedulerException e) {
             throw new SchedulingException("An error occurred while cancelling a timer for a saga"e);
         }
    }

    
Initializes the QuartzEventScheduler. Will make the configured Event Bus available to the Quartz Scheduler

Throws:
org.quartz.SchedulerException if an error occurs preparing the Quartz Scheduler for use.
    public void initialize() throws SchedulerException {
        Assert.notNull("A Scheduler must be provided.");
        Assert.notNull("An EventBus must be provided.");
         = true;
    }

    
Sets the backing Quartz Scheduler for this timer.

Parameters:
scheduler the backing Quartz Scheduler for this timer
    @Resource
    public void setScheduler(Scheduler scheduler) {
        this. = scheduler;
    }

    
Sets the event bus to which scheduled events need to be published.

Parameters:
eventBus the event bus to which scheduled events need to be published.
    @Resource
    public void setEventBus(EventBus eventBus) {
        this. = eventBus;
    }

    
Sets the group identifier to use when scheduling jobs with Quartz. Defaults to "AxonFramework-Events".

Parameters:
groupIdentifier the group identifier to use when scheduling jobs with Quartz
    public void setGroupIdentifier(String groupIdentifier) {
        this. = groupIdentifier;
    }

    
Sets the callback to invoke before and after publication of a scheduled event.

Parameters:
eventTriggerCallback the callback to invoke before and after publication of a scheduled event
    public void setEventTriggerCallback(EventTriggerCallback eventTriggerCallback) {
        this. = eventTriggerCallback;
    }
New to GrepCode? Check out our FAQ X