Search in sources :

Example 1 with IbisJobDetail

use of nl.nn.adapterframework.scheduler.IbisJobDetail in project iaf by ibissource.

the class ShowScheduler method getJobData.

private Map<String, Object> getJobData(JobKey jobKey, boolean expanded) throws SchedulerException {
    Map<String, Object> jobData = new HashMap<String, Object>();
    Scheduler scheduler = getScheduler();
    String jobName = jobKey.getName();
    JobDetail job = scheduler.getJobDetail(jobKey);
    jobData.put("fullName", job.getKey().getGroup() + "." + job.getKey().getName());
    jobData.put("name", job.getKey().getName());
    jobData.put("group", job.getKey().getGroup());
    String description = "-";
    if (StringUtils.isNotEmpty(job.getDescription()))
        description = job.getDescription();
    jobData.put("description", description);
    jobData.put("stateful", job.isPersistJobDataAfterExecution() && job.isConcurrentExectionDisallowed());
    jobData.put("durable", job.isDurable());
    jobData.put("jobClass", job.getJobClass().getSimpleName());
    if (job instanceof IbisJobDetail) {
        jobData.put("type", ((IbisJobDetail) job).getJobType());
    }
    TriggerState state = scheduler.getTriggerState(TriggerKey.triggerKey(jobName, jobKey.getGroup()));
    jobData.put("state", state.name());
    jobData.put("triggers", getJobTriggers(scheduler.getTriggersOfJob(jobKey)));
    jobData.put("messages", getJobMessages(job));
    JobDataMap jobMap = job.getJobDataMap();
    jobData.put("properties", getJobData(jobMap));
    if (expanded) {
        IJob jobDef = (IJob) jobMap.get(ConfiguredJob.JOBDEF_KEY);
        if (jobDef instanceof DatabaseJob) {
            DatabaseJob dbJob = (DatabaseJob) jobDef;
            jobData.put("adapter", dbJob.getAdapterName());
            jobData.put("listener", dbJob.getJavaListener());
            jobData.put("message", dbJob.getMessage());
        }
        Locker locker = jobDef.getLocker();
        if (locker != null) {
            jobData.put("locker", true);
            jobData.put("lockkey", locker.getObjectId());
        } else {
            jobData.put("locker", false);
        }
    }
    return jobData;
}
Also used : TriggerState(org.quartz.Trigger.TriggerState) JobDetail(org.quartz.JobDetail) IbisJobDetail(nl.nn.adapterframework.scheduler.IbisJobDetail) JobDataMap(org.quartz.JobDataMap) Locker(nl.nn.adapterframework.util.Locker) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Scheduler(org.quartz.Scheduler) IJob(nl.nn.adapterframework.scheduler.job.IJob) DatabaseJob(nl.nn.adapterframework.scheduler.job.DatabaseJob) IbisJobDetail(nl.nn.adapterframework.scheduler.IbisJobDetail)

Example 2 with IbisJobDetail

use of nl.nn.adapterframework.scheduler.IbisJobDetail in project iaf by ibissource.

the class LoadDatabaseSchedulesJob method execute.

@Override
public void execute(IbisManager ibisManager) {
    Map<JobKey, IbisJobDetail> databaseJobDetails = new HashMap<JobKey, IbisJobDetail>();
    Scheduler scheduler = null;
    SchedulerHelper sh = getApplicationContext().getBean(SchedulerHelper.class);
    try {
        scheduler = sh.getScheduler();
        // Fill the databaseJobDetails Map with all IbisJobDetails that have been stored in the database
        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);
    }
    // Get all IbisSchedules that have been stored in the database
    FixedQuerySender qs = SpringUtils.createBean(getApplicationContext(), FixedQuerySender.class);
    qs.setDatasourceName(JndiDataSourceFactory.GLOBAL_DEFAULT_DATASOURCE_NAME);
    qs.setQuery("SELECT COUNT(*) FROM IBISSCHEDULES");
    try {
        qs.configure();
        qs.open();
        try (Connection conn = qs.getConnection()) {
            try (PreparedStatement stmt = conn.prepareStatement("SELECT JOBNAME,JOBGROUP,ADAPTER,RECEIVER,CRON,EXECUTIONINTERVAL,MESSAGE,LOCKER,LOCK_KEY FROM IBISSCHEDULES")) {
                try (ResultSet rs = stmt.executeQuery()) {
                    while (rs.next()) {
                        String jobName = rs.getString("JOBNAME");
                        String jobGroup = rs.getString("JOBGROUP");
                        String adapterName = rs.getString("ADAPTER");
                        String javaListener = 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);
                        Adapter adapter = ibisManager.getRegisteredAdapter(adapterName);
                        if (adapter == null) {
                            getMessageKeeper().add("unable to add schedule [" + key + "], adapter [" + adapterName + "] not found");
                            continue;
                        }
                        // Create a new JobDefinition so we can compare it with existing jobs
                        DatabaseJob jobdef = SpringUtils.createBean(adapter.getApplicationContext(), DatabaseJob.class);
                        jobdef.setCronExpression(cronExpression);
                        jobdef.setName(jobName);
                        jobdef.setInterval(interval);
                        jobdef.setJobGroup(jobGroup);
                        jobdef.setAdapterName(adapterName);
                        jobdef.setJavaListener(javaListener);
                        jobdef.setMessage(message);
                        if (hasLocker) {
                            Locker locker = SpringUtils.createBean(getApplicationContext(), Locker.class);
                            locker.setName(lockKey);
                            locker.setObjectId(lockKey);
                            locker.setDatasourceName(JndiDataSourceFactory.GLOBAL_DEFAULT_DATASOURCE_NAME);
                            jobdef.setLocker(locker);
                        }
                        try {
                            jobdef.configure();
                        } catch (ConfigurationException e) {
                            getMessageKeeper().add("unable to configure DatabaseJobDef [" + jobdef + "] with key [" + key + "]", e);
                        }
                        // If the job is found, find out if it is different from the existing one and update if necessarily
                        if (databaseJobDetails.containsKey(key)) {
                            IbisJobDetail oldJobDetails = databaseJobDetails.get(key);
                            if (!oldJobDetails.compareWith(jobdef)) {
                                log.debug("updating DatabaseSchedule [" + key + "]");
                                try {
                                    sh.scheduleJob(jobdef);
                                } catch (SchedulerException e) {
                                    getMessageKeeper().add("unable to update schedule [" + key + "]", e);
                                }
                            }
                            // Remove the key that has been found from the databaseJobDetails Map
                            databaseJobDetails.remove(key);
                        } else {
                            // The job was not found in the databaseJobDetails Map, which indicates it's new and has to be added
                            log.debug("add DatabaseSchedule [" + key + "]");
                            try {
                                sh.scheduleJob(jobdef);
                            } catch (SchedulerException e) {
                                getMessageKeeper().add("unable to add schedule [" + key + "]", e);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // Only catch database related exceptions!
        getMessageKeeper().add("unable to retrieve schedules from database", e);
    } finally {
        qs.close();
    }
    // Loop through all remaining databaseJobDetails, which were not present in the database. Since they have been removed, unschedule them!
    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);
        }
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) Locker(nl.nn.adapterframework.util.Locker) HashMap(java.util.HashMap) Scheduler(org.quartz.Scheduler) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Adapter(nl.nn.adapterframework.core.Adapter) IbisJobDetail(nl.nn.adapterframework.scheduler.IbisJobDetail) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SchedulerException(org.quartz.SchedulerException) JobKey(org.quartz.JobKey) SchedulerHelper(nl.nn.adapterframework.scheduler.SchedulerHelper) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ResultSet(java.sql.ResultSet) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 3 with IbisJobDetail

use of nl.nn.adapterframework.scheduler.IbisJobDetail in project iaf by ibissource.

the class ShowScheduler method deleteSchedules.

@DELETE
@RolesAllowed({ "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/schedules/{groupName}/job/{jobName}")
@Relation("schedules")
@Produces(MediaType.APPLICATION_JSON)
public Response deleteSchedules(@PathParam("jobName") String jobName, @PathParam("groupName") String groupName) throws ApiException {
    Scheduler scheduler = getScheduler();
    try {
        if (log.isInfoEnabled())
            log.info("delete job jobName [" + jobName + "] groupName [" + groupName + "] " + commandIssuedBy());
        JobKey jobKey = JobKey.jobKey(jobName, groupName);
        if (jobKey == null) {
            throw new ApiException("JobKey not found, unable to remove schedule");
        }
        IbisJobDetail jobDetail = (IbisJobDetail) scheduler.getJobDetail(jobKey);
        if (jobDetail.getJobType() == JobType.DATABASE) {
            boolean success = false;
            // remove from database
            FixedQuerySender qs = (FixedQuerySender) getIbisContext().createBeanAutowireByName(FixedQuerySender.class);
            qs.setDatasourceName(JndiDataSourceFactory.GLOBAL_DEFAULT_DATASOURCE_NAME);
            qs.setQuery("SELECT COUNT(*) FROM IBISSCHEDULES");
            try {
                qs.configure();
            } catch (ConfigurationException e) {
                throw new ApiException("Error creating FixedQuerySender bean to remove job from database", e);
            }
            try {
                qs.open();
                try (Connection conn = qs.getConnection()) {
                    String query = "DELETE FROM IBISSCHEDULES WHERE JOBNAME=? AND JOBGROUP=?";
                    try (PreparedStatement stmt = conn.prepareStatement(query)) {
                        stmt.setString(1, jobKey.getName());
                        stmt.setString(2, jobKey.getGroup());
                        success = stmt.executeUpdate() > 0;
                    }
                }
            } catch (SenderException | SQLException | JdbcException e) {
                throw new ApiException("error removing job from database", e);
            } finally {
                qs.close();
            }
            if (!success) {
                throw new ApiException("failed to remove job from database");
            }
        }
        // remove from memory
        scheduler.deleteJob(jobKey);
    } catch (SchedulerException e) {
        throw new ApiException("Failed to delete job", e);
    }
    return Response.status(Response.Status.OK).build();
}
Also used : SchedulerException(org.quartz.SchedulerException) SQLException(java.sql.SQLException) Scheduler(org.quartz.Scheduler) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) IbisJobDetail(nl.nn.adapterframework.scheduler.IbisJobDetail) JobKey(org.quartz.JobKey) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SenderException(nl.nn.adapterframework.core.SenderException) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces)

Aggregations

IbisJobDetail (nl.nn.adapterframework.scheduler.IbisJobDetail)3 Scheduler (org.quartz.Scheduler)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)2 Locker (nl.nn.adapterframework.util.Locker)2 JobKey (org.quartz.JobKey)2 SchedulerException (org.quartz.SchedulerException)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedHashMap (java.util.LinkedHashMap)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Adapter (nl.nn.adapterframework.core.Adapter)1 SenderException (nl.nn.adapterframework.core.SenderException)1 JdbcException (nl.nn.adapterframework.jdbc.JdbcException)1