Search in sources :

Example 1 with CronExpression

use of org.xbib.elasticsearch.common.cron.CronExpression in project elasticsearch-jdbc by jprante.

the class JDBCImporter method schedule.

private List<Future> schedule(Settings settings) {
    List<Future> futures = new LinkedList<>();
    if (threadPoolExecutor != null) {
        logger.info("already scheduled");
        return futures;
    }
    String[] schedule = settings.getAsArray("schedule");
    Long seconds = settings.getAsTime("interval", TimeValue.timeValueSeconds(0)).seconds();
    if (schedule != null && schedule.length > 0) {
        Thread thread = new Thread(this);
        CronThreadPoolExecutor cronThreadPoolExecutor = new CronThreadPoolExecutor(settings.getAsInt("threadpoolsize", 1));
        for (String cron : schedule) {
            futures.add(cronThreadPoolExecutor.schedule(thread, new CronExpression(cron)));
        }
        this.threadPoolExecutor = cronThreadPoolExecutor;
        logger.info("scheduled with cron expressions {}", Arrays.asList(schedule));
    } else if (seconds > 0L) {
        Thread thread = new Thread(this);
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(settings.getAsInt("threadpoolsize", 1));
        futures.add(scheduledThreadPoolExecutor.scheduleAtFixedRate(thread, 0L, seconds, TimeUnit.SECONDS));
        this.threadPoolExecutor = scheduledThreadPoolExecutor;
        logger.info("scheduled at fixed rate of {} seconds", seconds);
    }
    return futures;
}
Also used : CronThreadPoolExecutor(org.xbib.elasticsearch.common.cron.CronThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Future(java.util.concurrent.Future) CronExpression(org.xbib.elasticsearch.common.cron.CronExpression) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)1 Future (java.util.concurrent.Future)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 CronExpression (org.xbib.elasticsearch.common.cron.CronExpression)1 CronThreadPoolExecutor (org.xbib.elasticsearch.common.cron.CronThreadPoolExecutor)1