org.quartz.Calendar Java Examples

The following examples show how to use org.quartz.Calendar. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PointbaseDelegate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Update a calendar.
 * </p>
 * 
 * @param conn
 *          the DB Connection
 * @param calendarName
 *          the name for the new calendar
 * @param calendar
 *          the calendar
 * @return the number of rows updated
 * @throws IOException
 *           if there were problems serializing the calendar
 */
@Override           
public int updateCalendar(Connection conn, String calendarName,
        Calendar calendar) throws IOException, SQLException {
    //log.debug( "Updating calendar " + calendarName + " : " + calendar );
    ByteArrayOutputStream baos = serializeObject(calendar);
    byte buf[] = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    PreparedStatement ps = null;

    try {
        ps = conn.prepareStatement(rtp(UPDATE_CALENDAR));
        ps.setBinaryStream(1, bais, buf.length);
        ps.setString(2, calendarName);

        return ps.executeUpdate();
    } finally {
        closeStatement(ps);
    }
}
 
Example #2
Source File: AnnualCalendar.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Redefine a certain day to be excluded (true) or included (false).
 * </p>
 */
public void setDayExcluded(java.util.Calendar day, boolean exclude) {
    if (exclude) {
        if (isDayExcluded(day)) {
            return;
        }

        excludeDays.add(day);
        dataSorted = false;
    } else {
        if (!isDayExcluded(day)) {
            return;
        }

        removeExcludedDay(day, true);
    }
}
 
Example #3
Source File: AnnualCalendar.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Determine the next time (in milliseconds) that is 'included' by the
 * Calendar after the given time. Return the original value if timeStamp is
 * included. Return 0 if all days are excluded.
 * </p>
 * 
 * <p>
 * Note that this Calendar is only has full-day precision.
 * </p>
 */
public long getNextIncludedTime(long timeStamp) {
    // Call base calendar implementation first
    long baseTime = super.getNextIncludedTime(timeStamp);
    if ((baseTime > 0) && (baseTime > timeStamp)) {
        timeStamp = baseTime;
    }

    // Get timestamp for 00:00:00
    java.util.Calendar day = getStartOfDayJavaCalendar(timeStamp);
    if (isDayExcluded(day) == false) { 
        return timeStamp; // return the original value
    }

    while (isDayExcluded(day) == true) {
        day.add(java.util.Calendar.DATE, 1);
    }

    return day.getTime().getTime();
}
 
Example #4
Source File: AnnualCalendar.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/** 
 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 */
public int compare(Object arg0, Object arg1) {
    java.util.Calendar c1 = (java.util.Calendar) arg0;
    java.util.Calendar c2 = (java.util.Calendar) arg1;
    
    int month1 = c1.get(java.util.Calendar.MONTH);
    int month2 = c2.get(java.util.Calendar.MONTH);
    
    int day1 = c1.get(java.util.Calendar.DAY_OF_MONTH);
    int day2 = c2.get(java.util.Calendar.DAY_OF_MONTH);
    
    if (month1 < month2) {
    	return -1;
    }
    if (month1 > month2) {
    	return 1; 
    }
    if (day1 < day2) {
    	return -1;
    }
    if (day1 > day2) {
    	return 1;
    }
    return 0;
}
 
Example #5
Source File: WeeklyCalendar.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Determine whether the given time (in milliseconds) is 'included' by the
 * Calendar.
 * </p>
 *
 * <p>
 * Note that this Calendar is only has full-day precision.
 * </p>
 */
@Override
public boolean isTimeIncluded(long timeStamp) {
    if (excludeAll == true) {
        return false;
    }

    // Test the base calendar first. Only if the base calendar not already
    // excludes the time/date, continue evaluating this calendar instance.
    if (super.isTimeIncluded(timeStamp) == false) { return false; }

    java.util.Calendar cl = createJavaCalendar(timeStamp);
    int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);

    return !(isDayExcluded(wday));
}
 
Example #6
Source File: StoreCalendarTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void storeCalendar() throws Exception {
    final String calendarName = "weekdayCalendar";
    Calendar calendar = getCalendar();

    jobStore.storeCalendar(calendarName, calendar, false, false);

    final String calendarHashKey = schema.calendarHashKey(calendarName);
    Map<String, String> calendarMap = jedis.hgetAll(calendarHashKey);

    assertThat(calendarMap, hasKey("calendar_class"));
    assertEquals(calendar.getClass().getName(), calendarMap.get("calendar_class"));
    assertThat(calendarMap, hasKey("calendar_json"));

    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> calendarJson = mapper.readValue(calendarMap.get("calendar_json"), new TypeReference<HashMap<String, Object>>() {
    });
    assertThat(calendarJson, hasKey("description"));
    assertEquals("Only run on weekdays.", calendarJson.get("description"));
}
 
Example #7
Source File: HolidayCalendar.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Determine the next time (in milliseconds) that is 'included' by the
 * Calendar after the given time.
 * </p>
 * 
 * <p>
 * Note that this Calendar is only has full-day precision.
 * </p>
 */
@Override
public long getNextIncludedTime(long timeStamp) {

    // Call base calendar implementation first
    long baseTime = super.getNextIncludedTime(timeStamp);
    if ((baseTime > 0) && (baseTime > timeStamp)) {
        timeStamp = baseTime;
    }

    // Get timestamp for 00:00:00
    java.util.Calendar day = getStartOfDayJavaCalendar(timeStamp);
    while (isTimeIncluded(day.getTime().getTime()) == false) {
        day.add(java.util.Calendar.DATE, 1);
    }

    return day.getTime().getTime();
}
 
Example #8
Source File: SimpleTriggerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Called when the <code>{@link Scheduler}</code> has decided to 'fire'
 * the trigger (execute the associated <code>Job</code>), in order to
 * give the <code>Trigger</code> a chance to update itself for its next
 * triggering (if any).
 * </p>
 * 
 * @see #executionComplete(JobExecutionContext, JobExecutionException)
 */
@Override
public void triggered(Calendar calendar) {
    timesTriggered++;
    previousFireTime = nextFireTime;
    nextFireTime = getFireTimeAfter(nextFireTime);

    while (nextFireTime != null && calendar != null
            && !calendar.isTimeIncluded(nextFireTime.getTime())) {
        
        nextFireTime = getFireTimeAfter(nextFireTime);

        if(nextFireTime == null)
            break;
        
        //avoid infinite loop
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(nextFireTime);
        if (c.get(java.util.Calendar.YEAR) > YEAR_TO_GIVEUP_SCHEDULING_AT) {
            nextFireTime = null;
        }
    }
}
 
Example #9
Source File: AnnualCalendar.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public int compare(java.util.Calendar c1, java.util.Calendar c2) {
  
  int month1 = c1.get(java.util.Calendar.MONTH);
  int month2 = c2.get(java.util.Calendar.MONTH);
  
  int day1 = c1.get(java.util.Calendar.DAY_OF_MONTH);
  int day2 = c2.get(java.util.Calendar.DAY_OF_MONTH);
  
  if (month1 < month2) {
      return -1;
  }
  if (month1 > month2) {
      return 1; 
  }
  if (day1 < day2) {
      return -1;
  }
  if (day1 > day2) {
      return 1;
  }
  return 0;
}
 
Example #10
Source File: JobStoreSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void doUpdateOfMisfiredTrigger(Connection conn, OperableTrigger trig, boolean forceState, String newStateIfNotComplete, boolean recovering) throws JobPersistenceException {
    Calendar cal = null;
    if (trig.getCalendarName() != null) {
        cal = retrieveCalendar(conn, trig.getCalendarName());
    }

    schedSignaler.notifyTriggerListenersMisfired(trig);

    trig.updateAfterMisfire(cal);

    if (trig.getNextFireTime() == null) {
        storeTrigger(conn, trig,
            null, true, STATE_COMPLETE, forceState, recovering);
        schedSignaler.notifySchedulerListenersFinalized(trig);
    } else {
        storeTrigger(conn, trig, null, true, newStateIfNotComplete,
                forceState, false);
    }
}
 
Example #11
Source File: SchedulerServiceImpl.java    From uflo with Apache License 2.0 6 votes vote down vote up
public Calendar buildCalendar(List<CalendarInfo> infos) {
	MultipleCalendar mulCalendar=null;
	Collection<CalendarProvider> providers=applicationContext.getBeansOfType(CalendarProvider.class).values();
	for(CalendarInfo info:infos){
		for(CalendarProvider provider:providers){
			Calendar calendar=provider.getCalendar(info.getId());
			if(calendar!=null){
				if(mulCalendar==null){
					mulCalendar=new MultipleCalendar();;
				}
				mulCalendar.addCalendar((BaseCalendar)calendar);
			}
		}
	}
	return mulCalendar;
}
 
Example #12
Source File: MonthlyCalendar.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Determine whether the given time (in milliseconds) is 'included' by the
 * Calendar.
 * </p>
 *
 * <p>
 * Note that this Calendar is only has full-day precision.
 * </p>
 */
@Override
public boolean isTimeIncluded(long timeStamp) {
    if (excludeAll == true) {
        return false;
    }

    // Test the base calendar first. Only if the base calendar not already
    // excludes the time/date, continue evaluating this calendar instance.
    if (super.isTimeIncluded(timeStamp) == false) { return false; }

    java.util.Calendar cl = createJavaCalendar(timeStamp);
    int day = cl.get(java.util.Calendar.DAY_OF_MONTH);

    return !(isDayExcluded(day));
}
 
Example #13
Source File: AbstractRedisStorage.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
/**
 * Determine whether or not the given trigger has misfired.
 * If so, notify the {@link org.quartz.spi.SchedulerSignaler} and update the trigger.
 * @param trigger the trigger to check for misfire
 * @param jedis a thread-safe Redis connection
 * @return false if the trigger has misfired; true otherwise
 * @throws JobPersistenceException
 */
protected boolean applyMisfire(OperableTrigger trigger, T jedis) throws JobPersistenceException {
    long misfireTime = System.currentTimeMillis();
    if(misfireThreshold > 0){
        misfireTime -= misfireThreshold;
    }
    final Date nextFireTime = trigger.getNextFireTime();
    if(nextFireTime == null || nextFireTime.getTime() > misfireTime
            || trigger.getMisfireInstruction() == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY){
        return false;
    }

    Calendar calendar = null;
    if(trigger.getCalendarName() != null){
        calendar = retrieveCalendar(trigger.getCalendarName(), jedis);
    }
    signaler.notifyTriggerListenersMisfired((OperableTrigger) trigger.clone());

    trigger.updateAfterMisfire(calendar);

    storeTrigger(trigger, true, jedis);
    if(trigger.getNextFireTime() == null){
        setTriggerState(RedisTriggerState.COMPLETED, (double) System.currentTimeMillis(), redisSchema.triggerHashKey(trigger.getKey()), jedis);
        signaler.notifySchedulerListenersFinalized(trigger);
    }
    else if(nextFireTime.equals(trigger.getNextFireTime())){
        return false;
    }
    return true;
}
 
Example #14
Source File: JobQuartzAutoConfiguration.java    From spring-boot-starter-micro-job with Apache License 2.0 5 votes vote down vote up
public JobQuartzAutoConfiguration(QuartzProperties properties, JobServerProperties jobServerProperties, ObjectProvider<SchedulerFactoryBeanCustomizer> customizers, ObjectProvider<JobDetail[]> jobDetails, ObjectProvider<Map<String, Calendar>> calendars, ObjectProvider<Trigger[]> triggers, ApplicationContext applicationContext) {
    this.properties = properties;
    this.jobServerProperties = jobServerProperties;
    this.customizers = customizers;
    this.jobDetails = (JobDetail[]) jobDetails.getIfAvailable();
    this.calendars = (Map) calendars.getIfAvailable();
    this.triggers = (Trigger[]) triggers.getIfAvailable();
    this.applicationContext = applicationContext;
    // 初始化job属性配置信息
    initJobProperties(this.properties);
}
 
Example #15
Source File: TriggerFiredBundle.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
public TriggerFiredBundle(JobDetail job, Trigger trigger, Calendar cal,
        boolean jobIsRecovering, Date fireTime, Date scheduledFireTime,
        Date prevFireTime, Date nextFireTime) {
    this.job = job;
    this.trigger = trigger;
    this.cal = cal;
    this.jobIsRecovering = jobIsRecovering;
    this.fireTime = fireTime;
    this.scheduledFireTime = scheduledFireTime;
    this.prevFireTime = prevFireTime;
    this.nextFireTime = nextFireTime;
}
 
Example #16
Source File: StoreCalendarTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test
public void retrieveCalendar() throws Exception {
    final String calendarName = "weekdayCalendar";
    Calendar calendar = getCalendar();
    jobStore.storeCalendar(calendarName, calendar, false, false);

    Calendar retrievedCalendar = jobStore.retrieveCalendar(calendarName);

    assertEquals(calendar.getClass(), retrievedCalendar.getClass());
    assertEquals(calendar.getDescription(), retrievedCalendar.getDescription());
    long currentTime = System.currentTimeMillis();
    assertEquals(calendar.getNextIncludedTime(currentTime), retrievedCalendar.getNextIncludedTime(currentTime));
}
 
Example #17
Source File: ScheduleService.java    From elasticsearch-quartz with Apache License 2.0 5 votes vote down vote up
public Calendar getCalendar(final String calName) {
    try {
        return scheduler.getCalendar(calName);
    } catch (final SchedulerException e) {
        throw new QuartzSchedulerException(e);
    }
}
 
Example #18
Source File: JobStoreImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Helper to find a calendar by name.
 */
@Nullable
private Calendar findCalendar(final ODatabaseDocumentTx db, final String name) {
  CalendarEntity calendarEntity = calendarEntityAdapter.readByName(db, name);
  if (calendarEntity != null) {
    return calendarEntity.getValue();
  }
  return null;
}
 
Example #19
Source File: ApiBootQuartzAutoConfiguration.java    From api-boot with Apache License 2.0 5 votes vote down vote up
public ApiBootQuartzAutoConfiguration(ApiBootQuartzProperties properties, ObjectProvider<SchedulerFactoryBeanCustomizer> customizers, JobDetail[] jobDetails, Map<String, Calendar> calendars, Trigger[] triggers, ApplicationContext applicationContext) {
    this.properties = properties;
    this.customizers = customizers;
    this.jobDetails = jobDetails;
    this.calendars = calendars;
    this.triggers = triggers;
    this.applicationContext = applicationContext;
}
 
Example #20
Source File: RemoteScheduler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
 * </p>
 */
public void addCalendar(String calName, Calendar calendar, boolean replace, boolean updateTriggers)
    throws SchedulerException {
    try {
        getRemoteScheduler().addCalendar(calName, calendar,
                replace, updateTriggers);
    } catch (RemoteException re) {
        throw invalidateHandleCreateException(
                "Error communicating with remote scheduler.", re);
    }
}
 
Example #21
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
protected boolean applyMisfire(OperableTrigger trigger, Jedis jedis) throws JobPersistenceException {
   long misfireTime = System.currentTimeMillis();
   if (getMisfireThreshold() > 0)
       misfireTime -= getMisfireThreshold();       

   Date triggerNextFireTime = trigger.getNextFireTime();
   if (triggerNextFireTime == null || triggerNextFireTime.getTime() > misfireTime 
           || trigger.getMisfireInstruction() == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY) { 
       return false; 
   }

   Calendar cal = null;
   if (trigger.getCalendarName() != null)
       cal = retrieveCalendar(trigger.getCalendarName(), jedis);       

   signaler.notifyTriggerListenersMisfired((OperableTrigger)trigger.clone());

   trigger.updateAfterMisfire(cal);
   
   if (triggerNextFireTime.equals(trigger.getNextFireTime()))
	   return false;

   storeTrigger(trigger, true, jedis);
   if (trigger.getNextFireTime() == null) { // Trigger completed
	   setTriggerState(RedisTriggerState.COMPLETED, (double)System.currentTimeMillis(), createTriggerHashKey(trigger.getKey().getGroup(), trigger.getKey().getName()));
	   signaler.notifySchedulerListenersFinalized(trigger);
   }
   
   return true;
}
 
Example #22
Source File: DefaultClusteredJobStore.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
boolean applyMisfire(TriggerWrapper tw) throws JobPersistenceException {
  long misfireTime = System.currentTimeMillis();
  if (getMisfireThreshold() > 0) {
    misfireTime -= getMisfireThreshold();
  }

  Date tnft = tw.getNextFireTime();
  if (tnft == null || tnft.getTime() > misfireTime
      || tw.getMisfireInstruction() == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY) { return false; }

  Calendar cal = null;
  if (tw.getCalendarName() != null) {
    cal = retrieveCalendar(tw.getCalendarName());
  }

  signaler.notifyTriggerListenersMisfired(tw.getTriggerClone());

  tw.updateAfterMisfire(cal, triggerFacade);

  if (tw.getNextFireTime() == null) {
    tw.setState(TriggerState.COMPLETE, terracottaClientId, triggerFacade);
    signaler.notifySchedulerListenersFinalized(tw.getTriggerClone());
    timeTriggers.remove(tw);
  } else if (tnft.equals(tw.getNextFireTime())) { return false; }

  return true;
}
 
Example #23
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Store calendar in redis.
 *
 * @param name the name
 * @param calendar the calendar
 * @param replaceExisting the replace existing
 * @param updateTriggers the update triggers
 * @param jedis thread-safe redis connection
 * @throws ObjectAlreadyExistsException the object already exists exception
 * @throws JobPersistenceException
 */
private void storeCalendar(String name, Calendar calendar,
		boolean replaceExisting, boolean updateTriggers, Jedis jedis)
		throws ObjectAlreadyExistsException, JobPersistenceException {
	
	String calendarHashKey = createCalendarHashKey(name);
	if (jedis.exists(calendarHashKey) && !replaceExisting)
		throw new ObjectAlreadyExistsException(calendarHashKey + " already exists");
	
	Gson gson = new Gson();			
     Map<String, String> calendarHash = new HashMap<>();
	calendarHash.put(CALENDAR_CLASS, calendar.getClass().getName());
	calendarHash.put(CALENDAR_SERIALIZED, gson.toJson(calendar));
	
	jedis.hmset(calendarHashKey, calendarHash);
	jedis.sadd(CALENDARS_SET, calendarHashKey);
	
	if (updateTriggers) {
		String calendarTriggersSetkey = createCalendarTriggersSetKey(name);
		Set<String> triggerHasjKeys = jedis.smembers(calendarTriggersSetkey);
		for (String triggerHashKey : triggerHasjKeys) {				
			OperableTrigger trigger = retrieveTrigger(new TriggerKey(triggerHashKey.split(":")[2], triggerHashKey.split(":")[1]), jedis);
			long removed = jedis.zrem(RedisTriggerState.WAITING.getKey(), triggerHashKey);
			trigger.updateWithNewCalendar(calendar, getMisfireThreshold());
			if (removed == 1)
				setTriggerState(RedisTriggerState.WAITING, (double)trigger.getNextFireTime().getTime(), triggerHashKey);				
		}
	}
}
 
Example #24
Source File: ApiBootQuartzAutoConfiguration.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
public ApiBootQuartzAutoConfiguration(ApiBootQuartzProperties properties, ObjectProvider<SchedulerFactoryBeanCustomizer> customizers, JobDetail[] jobDetails, Map<String, Calendar> calendars, Trigger[] triggers, ApplicationContext applicationContext) {
    this.properties = properties;
    this.customizers = customizers;
    this.jobDetails = jobDetails;
    this.calendars = calendars;
    this.triggers = triggers;
    this.applicationContext = applicationContext;
}
 
Example #25
Source File: RAMJobStore.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
protected boolean applyMisfire(TriggerWrapper tw) {

        long misfireTime = System.currentTimeMillis();
        if (getMisfireThreshold() > 0) {
            misfireTime -= getMisfireThreshold();
        }

        Date tnft = tw.trigger.getNextFireTime();
        if (tnft == null || tnft.getTime() > misfireTime) { 
            return false; 
        }

        Calendar cal = null;
        if (tw.trigger.getCalendarName() != null) {
            cal = retrieveCalendar(null, tw.trigger.getCalendarName());
        }

        signaler.notifyTriggerListenersMisfired((Trigger)tw.trigger.clone());

        tw.trigger.updateAfterMisfire(cal);

        if (tw.trigger.getNextFireTime() == null) {
            tw.state = TriggerWrapper.STATE_COMPLETE;
            signaler.notifySchedulerListenersFinalized(tw.trigger);
            synchronized (lock) {
                timeTriggers.remove(tw);
            }
        } else if (tnft.equals(tw.trigger.getNextFireTime())) {
            return false;
        }

        return true;
    }
 
Example #26
Source File: WeeklyCalendar.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Check if all week days are excluded. That is no day is included.
 * </p>
 *
 * @return boolean
 */
public boolean areAllDaysExcluded() {
    return
        isDayExcluded(java.util.Calendar.SUNDAY) &&
        isDayExcluded(java.util.Calendar.MONDAY) &&
        isDayExcluded(java.util.Calendar.TUESDAY) &&
        isDayExcluded(java.util.Calendar.WEDNESDAY) &&
        isDayExcluded(java.util.Calendar.THURSDAY) &&
        isDayExcluded(java.util.Calendar.FRIDAY) &&
        isDayExcluded(java.util.Calendar.SATURDAY);
}
 
Example #27
Source File: RedisClusterStorage.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
/**
 * Store a {@link Calendar}
 *
 * @param name            the name of the calendar
 * @param calendar        the calendar object to be stored
 * @param replaceExisting if true, any existing calendar with the same name will be overwritten
 * @param updateTriggers  if true, any existing triggers associated with the calendar will be updated
 * @param jedis           a thread-safe Redis connection
 * @throws JobPersistenceException
 */
@Override
public void storeCalendar(String name, Calendar calendar, boolean replaceExisting, boolean updateTriggers, JedisClusterCommandsWrapper jedis) throws JobPersistenceException {
    final String calendarHashKey = redisSchema.calendarHashKey(name);
    if (!replaceExisting && jedis.exists(calendarHashKey)) {
        throw new ObjectAlreadyExistsException(String.format("Calendar with key %s already exists.", calendarHashKey));
    }
    Map<String, String> calendarMap = new HashMap<>();
    calendarMap.put(CALENDAR_CLASS, calendar.getClass().getName());
    try {
        calendarMap.put(CALENDAR_JSON, mapper.writeValueAsString(calendar));
    } catch (JsonProcessingException e) {
        throw new JobPersistenceException("Unable to serialize calendar.", e);
    }

    jedis.hmset(calendarHashKey, calendarMap);
    jedis.sadd(redisSchema.calendarsSet(), calendarHashKey);

    if (updateTriggers) {
        final String calendarTriggersSetKey = redisSchema.calendarTriggersSetKey(name);
        Set<String> triggerHashKeys = jedis.smembers(calendarTriggersSetKey);
        for (String triggerHashKey : triggerHashKeys) {
            OperableTrigger trigger = retrieveTrigger(redisSchema.triggerKey(triggerHashKey), jedis);
            long removed = jedis.zrem(redisSchema.triggerStateKey(RedisTriggerState.WAITING), triggerHashKey);
            trigger.updateWithNewCalendar(calendar, misfireThreshold);
            if (removed == 1) {
                setTriggerState(RedisTriggerState.WAITING, (double) trigger.getNextFireTime().getTime(), triggerHashKey, jedis);
            }
        }
    }
}
 
Example #28
Source File: JobStoreImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Determine if trigger has misfired.
 */
private boolean applyMisfire(final ODatabaseDocumentTx db, final TriggerEntity triggerEntity) {
  log.trace("Checking for misfire: {}", triggerEntity);

  OperableTrigger trigger = triggerEntity.getValue();

  long misfireTime = getMisfireTime();

  Date nextFireTime = trigger.getNextFireTime();
  if (nextFireTime == null || nextFireTime.getTime() > misfireTime ||
      trigger.getMisfireInstruction() == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY) {
    return false;
  }

  // resolve trigger calender if there is one
  Calendar calendar = null;
  if (trigger.getCalendarName() != null) {
    calendar = findCalendar(db, trigger.getCalendarName());
  }

  signaler.notifyTriggerListenersMisfired(trigger);
  trigger.updateAfterMisfire(calendar);

  if (trigger.getNextFireTime() == null) {
    triggerEntity.setState(COMPLETE);
    triggerEntityAdapter.editEntity(db, triggerEntity);
    signaler.notifySchedulerListenersFinalized(trigger);
  }
  else if (nextFireTime.equals(trigger.getNextFireTime())) {
    return false;
  }

  return true;
}
 
Example #29
Source File: StoreCalendarTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test(expected = JobPersistenceException.class)
public void storeCalendarNoReplace() throws Exception {
    final String calendarName = "weekdayCalendar";
    Calendar calendar = getCalendar();
    jobStore.storeCalendar(calendarName, calendar, false, false);
    jobStore.storeCalendar(calendarName, calendar, false, false);
}
 
Example #30
Source File: QuartzScheduler.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Get the <code>{@link Calendar}</code> instance with the given name.
 * </p>
 */
public Calendar getCalendar(SchedulingContext ctxt, String calName)
    throws SchedulerException {
    validateState();

    return resources.getJobStore().retrieveCalendar(ctxt, calName);
}