Search in sources :

Example 1 with JobUpdateListener

use of org.apache.sling.jobs.JobUpdateListener in project sling by apache.

the class JobQueueConsumerFactory method onMessage.

@Override
public void onMessage(Types.QueueName queueName, Map<String, Object> message) throws RequeueMessageException {
    final Job job = new JobImpl(new JobUpdateImpl(message));
    ((JobConsumer) jobManager).execute(job, new JobUpdateListener() {

        @Override
        public void update(@Nonnull JobUpdate update) {
            if (update.getId() != job.getId() || !ALLOWED_COMMANDS.contains(update.getCommand())) {
                throw new IllegalArgumentException("Not allowed to update other jobs or issue reserved commands when updating the state of a running job.");
            }
            topicManager.publish(update.getQueue().asTopicName(), update.getCommand().asCommandName(), Utils.toMapValue(update));
        }
    }, new JobCallback() {

        @Override
        public void callback(Job finalJobState) {
            if (finalJobState.getId() != job.getId()) {
                throw new IllegalArgumentException("Final Job state ID must match initial JobState ID");
            }
            JobUpdate finalJobUpdate = finalJobState.newJobUpdateBuilder().command(JobUpdate.JobUpdateCommand.UPDATE_JOB).putAll(finalJobState.getProperties()).build();
            topicManager.publish(finalJobUpdate.getQueue().asTopicName(), finalJobUpdate.getCommand().asCommandName(), Utils.toMapValue(finalJobUpdate));
        }
    });
}
Also used : JobUpdateListener(org.apache.sling.jobs.JobUpdateListener) JobUpdate(org.apache.sling.jobs.JobUpdate) JobConsumer(org.apache.sling.jobs.JobConsumer) Job(org.apache.sling.jobs.Job) JobCallback(org.apache.sling.jobs.JobCallback)

Aggregations

Job (org.apache.sling.jobs.Job)1 JobCallback (org.apache.sling.jobs.JobCallback)1 JobConsumer (org.apache.sling.jobs.JobConsumer)1 JobUpdate (org.apache.sling.jobs.JobUpdate)1 JobUpdateListener (org.apache.sling.jobs.JobUpdateListener)1