@@ -0,0 +1,2078 @@
+package com.s24.redjob.worker;%0A%0Aimport com.fasterxml.jackson.annotation.JsonTypeName;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.config.BeanDefinition;%0Aimport org.springframework.beans.factory.config.ConfigurableListableBeanFactory;%0Aimport org.springframework.util.Assert;%0A%0A/**%0A * %7B@link JobRunnerFactory%7D using a Spring application context%0A * to retrieve job runners by the %7B@link JsonTypeName%7D of jobs.%0A * The job runners are expected to be implement %7B@link Runnable%7D%0A * and to be prototypes providing a one argument constructor accepting the job.%0A * Because they are prototypes, job runners are allowed to have state.%0A */%0Apublic class SpringJobRunnerFactory implements JobRunnerFactory %7B%0A    /**%0A     * Bean factory.%0A     */%0A    @Autowired%0A    private ConfigurableListableBeanFactory beanFactory;%0A%0A    @Override%0A    public Runnable runnerFor(Object job) %7B%0A        Assert.notNull(job, %22Pre-condition violated: job != null.%22);%0A%0A        JsonTypeName type = job.getClass().getAnnotation(JsonTypeName.class);%0A        Assert.notNull(type, %22Pre-condition violated: Job has a @JsonTypeName.%22);%0A%0A        // Check for bean existence and that bean is a prototype.%0A        // Beans have to be prototypes, because they are configured with the job vars below.%0A        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(type.value());%0A        if (!BeanDefinition.SCOPE_PROTOTYPE.equals(beanDefinition.getScope())) %7B%0A            throw new IllegalArgumentException(String.format(%0A                    %22Job runners have to be prototypes, but job runner %25s has scope %25s.%22,%0A                    type.value(), beanDefinition.getScope()));%0A        %7D%0A%0A        Object runner = beanFactory.getBean(type.value(), job);%0A        if (!(runner instanceof Runnable)) %7B%0A            throw new IllegalArgumentException(String.format(%0A                    %22Job runners need to be Runnable, but job runner %25s (%25s) is not.%22,%0A                    type.value(), runner.getClass().getName()));%0A        %7D%0A%0A        return (Runnable) runner;%0A    %7D%0A%7D%0A

Add Spring based job runner factory