Last active
October 28, 2016 21:20
-
-
Save cooniur/401261a7133aa81bf5a0648d9ba904d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
class IntegrationJobTriggerBeanProcessor implements BeanPostProcessor { | |
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |
private final IntegrationJobTriggerTaskInitializer triggerTaskInitializer; | |
private final IntegrationJobTriggerDefaultSchedules defaultSchedules; | |
private final IntegrationJobScheduleService jobScheduleService; | |
@Autowired | |
public IntegrationJobTriggerBeanProcessor(IntegrationJobTriggerTaskInitializer triggerTaskInitializer, | |
IntegrationJobTriggerDefaultSchedules defaultSchedules, | |
IntegrationJobScheduleService jobScheduleService) { | |
this.triggerTaskInitializer = triggerTaskInitializer; | |
this.defaultSchedules = defaultSchedules; | |
this.jobScheduleService = jobScheduleService; | |
} | |
@Override | |
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
return bean; | |
} | |
@Override | |
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
IntegrationJobTrigger jobTriggerAnnotation = bean.getClass().getAnnotation(IntegrationJobTrigger.class); | |
if (jobTriggerAnnotation == null) { | |
return bean; | |
} | |
if (!(bean instanceof Runnable)) { | |
throw new IllegalArgumentException(String.format("Bean %s must implement Runnable interface, object is %s.", beanName, bean)); | |
} | |
IntegrationJobName jobName = jobTriggerAnnotation.job(); | |
Object proxiedBean = bean.getClass().cast(this.createProxy(jobName, bean)); | |
this.triggerTaskInitializer.add(jobName, (Runnable) proxiedBean); | |
this.defaultSchedules.add(jobTriggerAnnotation); | |
logger.debug("Created proxy {} for trigger bean {} for integration job {}.", proxiedBean, bean, jobName); | |
return proxiedBean; | |
} | |
private Object createProxy(IntegrationJobName jobName, Object bean) { | |
ProxyFactory pf = new ProxyFactory(); | |
pf.addAdvice(new IntegrationJobTriggerInterceptor(jobName, this.jobScheduleService)); | |
pf.setTarget(bean); | |
return pf.getProxy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IntegrationJobTriggerInterceptor implements MethodInterceptor { | |
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |
private final IntegrationJobName jobName; | |
private final IntegrationJobScheduleService jobScheduleService; | |
public IntegrationJobTriggerInterceptor(IntegrationJobName jobName, IntegrationJobScheduleService jobScheduleService) { | |
this.jobName = jobName; | |
this.jobScheduleService = jobScheduleService; | |
} | |
@Override | |
public Object invoke(MethodInvocation invocation) throws Throwable { | |
IntegrationJobSchedule schedule = this.jobScheduleService.find(jobName); | |
if (schedule.isEnabled()) { | |
logger.debug("About to run trigger for job {}...", this.jobName); | |
invocation.proceed(); | |
} else { | |
logger.debug("Job {} is disabled, no trigger to run.", this.jobName); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment