use of io.helidon.scheduling.Task in project helidon by oracle.
the class SchedulingCdiExtension method invoke.
void invoke(@Observes @Priority(PLATFORM_AFTER + 4000) @Initialized(ApplicationScoped.class) Object event, BeanManager beanManager) {
ScheduledThreadPoolSupplier scheduledThreadPoolSupplier = ScheduledThreadPoolSupplier.builder().threadNamePrefix(schedulingConfig.get("thread-name-prefix").asString().orElse("scheduled-")).config(schedulingConfig).build();
for (AnnotatedMethod<?> am : methods) {
Class<?> aClass = am.getDeclaringType().getJavaClass();
Bean<?> bean = beans.get(am);
Object beanInstance = lookup(bean, beanManager);
ScheduledExecutorService executorService = scheduledThreadPoolSupplier.get();
executors.add(executorService);
Method method = am.getJavaMember();
if (!method.trySetAccessible()) {
throw new DeploymentException(String.format("Scheduled method %s#%s is not accessible!", method.getDeclaringClass().getName(), method.getName()));
}
if (am.isAnnotationPresent(FixedRate.class) && am.isAnnotationPresent(Scheduled.class)) {
throw new DeploymentException(String.format("Scheduled method %s#%s can have only one scheduling annotation.", method.getDeclaringClass().getName(), method.getName()));
}
Config methodConfig = config.get(aClass.getName() + "." + method.getName() + ".schedule");
if (am.isAnnotationPresent(FixedRate.class)) {
FixedRate annotation = am.getAnnotation(FixedRate.class);
long initialDelay = methodConfig.get("initial-delay").asLong().orElseGet(annotation::initialDelay);
long delay = methodConfig.get("delay").asLong().orElseGet(annotation::value);
TimeUnit timeUnit = methodConfig.get("time-unit").asString().map(TimeUnit::valueOf).orElseGet(annotation::timeUnit);
Task task = Scheduling.fixedRateBuilder().executor(executorService).initialDelay(initialDelay).delay(delay).timeUnit(timeUnit).task(inv -> invokeWithOptionalParam(beanInstance, method, inv)).build();
LOGGER.log(Level.FINE, () -> String.format("Method %s#%s scheduled to be executed %s", aClass.getSimpleName(), method.getName(), task.description()));
} else if (am.isAnnotationPresent(Scheduled.class)) {
Scheduled annotation = am.getAnnotation(Scheduled.class);
String cron = methodConfig.get("cron").asString().orElseGet(() -> resolvePlaceholders(annotation.value(), config));
boolean concurrent = methodConfig.get("concurrent").asBoolean().orElseGet(annotation::concurrentExecution);
Task task = Scheduling.cronBuilder().executor(executorService).concurrentExecution(concurrent).expression(cron).task(inv -> invokeWithOptionalParam(beanInstance, method, inv)).build();
LOGGER.log(Level.FINE, () -> String.format("Method %s#%s scheduled to be executed %s", aClass.getSimpleName(), method.getName(), task.description()));
}
}
}
Aggregations