core/src/main/java/nl/nn/adapterframework/scheduler/JobDef.java
/**
	 * 1. This method first stores all database jobs that can are found in the Quartz Scheduler in a Map.
	 * 2. It then loops through all records found in the database.
	 * 3. If the job is found, remove it from the Map and compares it with the already existing scheduled job. 
	 *    Only if they differ, it overwrites the current job.
	 *    If it is not present it add the job to the scheduler.
	 * 4. Once it's looped through all the database jobs, loop through the remaining jobs in the Map.
	 *    Since they have been removed from the database, remove them from the Quartz Scheduler
	 */
1. This method first stores all database jobs that can are found in the Quartz Scheduler in a Map.
2. It then loops through all records found in the database.
3. If the job is found, remove it from the Map and compares it with the already existing scheduled job.
Only if they differ, it overwrites the current job.
If it is not present it add the job to the scheduler.
4. Once it's looped through all the database jobs, loop through the remaining jobs in the Map.
Since they have been removed from the database, remove them from the Quartz Scheduler
1 . This method first stores all database jobs that can are found in the Quartz Scheduler in a Map . 2 . It then loops through all records found in the database . 3 . If the job is found remove it from the Map and compares it with the already existing scheduled job . Only if they differ it overwrites the current job . If it is not present it add the job to the scheduler . 4 . Once it ' s looped through all the database jobs loop through the remaining jobs in the Map . Since they have been removed from the database remove them from the Quartz Scheduler
private void loadDatabaseSchedules(IbisManager ibisManager) {
		if(!(ibisManager instanceof DefaultIbisManager)) {
			getMessageKeeper().add("manager is not an instance of DefaultIbisManager", MessageKeeperLevel.ERROR);
			return;
		}
		Map<JobKey, IbisJobDetail> databaseJobDetails = new HashMap<JobKey, IbisJobDetail>();
		Scheduler scheduler = null;
		SchedulerHelper sh = null;
		try {
			sh = ((DefaultIbisManager) ibisManager).getSchedulerHelper();
			scheduler = sh.getScheduler();
			Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.anyJobGroup());
			for(JobKey jobKey : jobKeys) {
				IbisJobDetail detail = (IbisJobDetail) scheduler.getJobDetail(jobKey);
				if(detail.getJobType() == JobType.DATABASE) {
					databaseJobDetails.put(detail.getKey(), detail);
				}
			}
		} catch (SchedulerException e) {
			getMessageKeeper().add("unable to retrieve jobkeys from scheduler", e);
		}
		String configJmsRealm = JmsRealmFactory.getInstance().getFirstDatasourceJmsRealm();
		FixedQuerySender qs = (FixedQuerySender) ibisManager.getIbisContext().createBeanAutowireByName(FixedQuerySender.class);
		qs.setJmsRealm(configJmsRealm);
		qs.setQuery("SELECT COUNT(*) FROM IBISSCHEDULES");
		Connection conn = null;
		ResultSet rs = null;
		try {
			qs.configure();
			qs.open();
			conn = qs.getConnection();
			PreparedStatement stmt = conn.prepareStatement("SELECT JOBNAME,JOBGROUP,ADAPTER,RECEIVER,CRON,EXECUTIONINTERVAL,MESSAGE,LOCKER,LOCK_KEY FROM IBISSCHEDULES");
			rs = stmt.executeQuery();
			while(rs.next()) {
				String jobName = rs.getString("JOBNAME");
				String jobGroup = rs.getString("JOBGROUP");
				String adapterName = rs.getString("ADAPTER");
				String receiverName = rs.getString("RECEIVER");
				String cronExpression = rs.getString("CRON");
				int interval = rs.getInt("EXECUTIONINTERVAL");
				String message = rs.getString("MESSAGE");
				boolean hasLocker = rs.getBoolean("LOCKER");
				String lockKey = rs.getString("LOCK_KEY");
				JobKey key = JobKey.jobKey(jobName, jobGroup);
				DatabaseJobDef jobdef = new DatabaseJobDef();
				jobdef.setCronExpression(cronExpression);
				jobdef.setName(jobName);
				jobdef.setInterval(interval);
				jobdef.setJobGroup(jobGroup);
				jobdef.setAdapterName(adapterName);
				jobdef.setReceiverName(receiverName);
				jobdef.setMessage(message);
				if(hasLocker) {
					Locker locker = (Locker) ibisManager.getIbisContext().createBeanAutowireByName(Locker.class);
					locker.setName(lockKey);
					locker.setObjectId(lockKey);
					locker.setJmsRealm(configJmsRealm);
					jobdef.setLocker(locker);
				}
				try {
					jobdef.configure();
				} catch (ConfigurationException e) {
					getMessageKeeper().add("unable to configure DatabaseJobDef ["+jobdef+"] with key ["+key+"]", e);
				}
				if(databaseJobDetails.containsKey(key)) {
					IbisJobDetail oldJobDetails = databaseJobDetails.get(key);
					if(!oldJobDetails.compareWith(jobdef)) {
						log.debug("updating DatabaseSchedule ["+key+"]");
						try {
							sh.scheduleJob(ibisManager, jobdef);
						} catch (SchedulerException e) {
							getMessageKeeper().add("unable to update schedule ["+key+"]", e);
						}
					}
					databaseJobDetails.remove(key);
				} else {
					log.debug("add DatabaseSchedule ["+key+"]");
					try {
						sh.scheduleJob(ibisManager, jobdef);
					} catch (SchedulerException e) {
						getMessageKeeper().add("unable to add schedule ["+key+"]", e);
					}
				}
			}
		} catch (Exception e) { 
			getMessageKeeper().add("unable to retrieve schedules from database", e);
		} finally {
			JdbcUtil.fullClose(conn, rs);
			qs.close();
		}
		for(JobKey key : databaseJobDetails.keySet()) {
			log.debug("delete DatabaseSchedule ["+key+"]");
			try {
				scheduler.deleteJob(key);
			} catch (SchedulerException e) {
				getMessageKeeper().add("unable to remove schedule ["+key+"]", e);
			}
		}
	}
private void loadDatabaseSchedules ( IbisManager ibisManager ) { if ( ! ( ibisManager instanceof DefaultIbisManager ) ) { getMessageKeeper ( ) . add ( "manager is not an instance of DefaultIbisManager" , MessageKeeperLevel . ERROR ) ; return ; } Map < JobKey , IbisJobDetail > databaseJobDetails = new HashMap < JobKey , IbisJobDetail > ( ) ; Scheduler scheduler = null ; SchedulerHelper sh = null ; try { sh = ( ( DefaultIbisManager ) ibisManager ) . getSchedulerHelper ( ) ; scheduler = sh . getScheduler ( ) ; Set < JobKey > jobKeys = scheduler . getJobKeys ( GroupMatcher . anyJobGroup ( ) ) ; for ( JobKey jobKey : jobKeys ) { IbisJobDetail detail = ( IbisJobDetail ) scheduler . getJobDetail ( jobKey ) ; if ( detail . getJobType ( ) == JobType . DATABASE ) { databaseJobDetails . put ( detail . getKey ( ) , detail ) ; } } } catch ( SchedulerException e ) { getMessageKeeper ( ) . add ( "unable to retrieve jobkeys from scheduler" , e ) ; } String configJmsRealm = JmsRealmFactory . getInstance ( ) . getFirstDatasourceJmsRealm ( ) ; FixedQuerySender qs = ( FixedQuerySender ) ibisManager . getIbisContext ( ) . createBeanAutowireByName ( FixedQuerySender . class ) ; qs . setJmsRealm ( configJmsRealm ) ; qs . setQuery ( "SELECT COUNT(*) FROM IBISSCHEDULES" ) ; Connection conn = null ; ResultSet rs = null ; try { qs . configure ( ) ; qs . open ( ) ; conn = qs . getConnection ( ) ; PreparedStatement stmt = conn . prepareStatement ( "SELECT JOBNAME,JOBGROUP,ADAPTER,RECEIVER,CRON,EXECUTIONINTERVAL,MESSAGE,LOCKER,LOCK_KEY FROM IBISSCHEDULES" ) ; rs = stmt . executeQuery ( ) ; while ( rs . next ( ) ) { String jobName = rs . getString ( "JOBNAME" ) ; String jobGroup = rs . getString ( "JOBGROUP" ) ; String adapterName = rs . getString ( "ADAPTER" ) ; String receiverName = rs . getString ( "RECEIVER" ) ; String cronExpression = rs . getString ( "CRON" ) ; int interval = rs . getInt ( "EXECUTIONINTERVAL" ) ; String message = rs . getString ( "MESSAGE" ) ; boolean hasLocker = rs . getBoolean ( "LOCKER" ) ; String lockKey = rs . getString ( "LOCK_KEY" ) ; JobKey key = JobKey . jobKey ( jobName , jobGroup ) ; DatabaseJobDef jobdef = new DatabaseJobDef ( ) ; jobdef . setCronExpression ( cronExpression ) ; jobdef . setName ( jobName ) ; jobdef . setInterval ( interval ) ; jobdef . setJobGroup ( jobGroup ) ; jobdef . setAdapterName ( adapterName ) ; jobdef . setReceiverName ( receiverName ) ; jobdef . setMessage ( message ) ; if ( hasLocker ) { Locker locker = ( Locker ) ibisManager . getIbisContext ( ) . createBeanAutowireByName ( Locker . class ) ; locker . setName ( lockKey ) ; locker . setObjectId ( lockKey ) ; locker . setJmsRealm ( configJmsRealm ) ; jobdef . setLocker ( locker ) ; } try { jobdef . configure ( ) ; } catch ( ConfigurationException e ) { getMessageKeeper ( ) . add ( "unable to configure DatabaseJobDef [" + jobdef + "] with key [" + key + "]" , e ) ; } if ( databaseJobDetails . containsKey ( key ) ) { IbisJobDetail oldJobDetails = databaseJobDetails . get ( key ) ; if ( ! oldJobDetails . compareWith ( jobdef ) ) { log . debug ( "updating DatabaseSchedule [" + key + "]" ) ; try { sh . scheduleJob ( ibisManager , jobdef ) ; } catch ( SchedulerException e ) { getMessageKeeper ( ) . add ( "unable to update schedule [" + key + "]" , e ) ; } } databaseJobDetails . remove ( key ) ; } else { log . debug ( "add DatabaseSchedule [" + key + "]" ) ; try { sh . scheduleJob ( ibisManager , jobdef ) ; } catch ( SchedulerException e ) { getMessageKeeper ( ) . add ( "unable to add schedule [" + key + "]" , e ) ; } } } } catch ( Exception e ) { getMessageKeeper ( ) . add ( "unable to retrieve schedules from database" , e ) ; } finally { JdbcUtil . fullClose ( conn , rs ) ; qs . close ( ) ; } for ( JobKey key : databaseJobDetails . keySet ( ) ) { log . debug ( "delete DatabaseSchedule [" + key + "]" ) ; try { scheduler . deleteJob ( key ) ; } catch ( SchedulerException e ) { getMessageKeeper ( ) . add ( "unable to remove schedule [" + key + "]" , e ) ; } } }
1.
1 .
// Fill the databaseJobDetails Map with all IbisJobDetails that have been stored in the database // Get all IbisSchedules that have been stored in the database //Create a new JobDefinition so we can compare it with existing jobs // If the job is found, find out if it is different from the existing one and update if necessarily // Remove the key that has been found from the databaseJobDetails Map // The job was not found in the databaseJobDetails Map, which indicates it's new and has to be added // Only catch database related exceptions! // Loop through all remaining databaseJobDetails, which were not present in the database. Since they have been removed, unschedule them!