Skip to content

Instantly share code, notes, and snippets.

@cooniur
Last active October 28, 2016 21:20
Show Gist options
  • Save cooniur/401261a7133aa81bf5a0648d9ba904d3 to your computer and use it in GitHub Desktop.
Save cooniur/401261a7133aa81bf5a0648d9ba904d3 to your computer and use it in GitHub Desktop.
@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();
}
}
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