简体   繁体   中英

How to use ScheduledExecutorService to run the job periodically with multiple services

I am attempting to executing a email job for every minute. Job has 5 services. Each 5 services should run in parallel.

Using ExecutorService :

ExecutorService service = null;
if (serviceMap != null && serviceMap.size() > 0) {

    for (;;) {
        service = Executors.newFixedThreadPool(serviceMap.size());  // Here Service Map size will be 5
        for (Map.Entry entry : serviceMap.entrySet()) {
            service.submit(new EmailService(conn, mailParam));
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}

Using ScheduledExecutorService :

ScheduledExecutorService scheduledExecutorService = null;
if (serviceMap != null && serviceMap.size() > 0) {
    scheduledExecutorService = Executors.newFixedThreadPool(serviceMap.size());  // Here Service Map size will be 5
    for (Map.Entry entry : serviceMap.entrySet()) {

        ScheduledFuture scheduledFuture =
                scheduledExecutorService.schedule(new EmailService(conn, mailParam),
                60,
                TimeUnit.SECONDS);
        System.out.println("result = " + scheduledFuture.get());
    }

    scheduledExecutorService.shutdown();
}

If I use ExecutorService, for every minute I shutdown the service and execute the service again. Is it all right?

If I use ScheduledExecutorService, I couldn't be able to write code to execute service in parallel and couldn't be able to run the job for every minute.

Above is my code snippet. Please help me to resolve it.

First of all, you should call newScheduledThreadPool() that creates ScheduledExecutorService , which is the correct scheduling executor service.
Then you should use [scheduleAtFixedRate][1] method to schedule a task every minute with initial delay 60 seconds.

ScheduledExecutorService scheduledExecutorService = null;
if (serviceMap != null && serviceMap.size() > 0) {
    scheduledExecutorService = Executors.newScheduledThreadPool(serviceMap.size());  // Here Service Map size will be 5
    for (Map.Entry entry : serviceMap.entrySet()) {
        scheduledExecutorService.scheduleAtFixedRate(new EmailService(conn, mailParam),
                60,
                60,
                TimeUnit.SECONDS);
    }
}

Good question !!

You have to separate it as two classes.

One is to start scheduler. Another one for execute the service which implements Callable or Runnable interface.

scheduledExecutorService.schedule() is different from service.submit().

schedule() is used to schedule a process whereas submit is used for submitting the services.

Start Scheduler Class :

public static void main(String[] args) {
        Class2 runnable = new Class2() {
          @Override
          public void run() {
            // call method that has ExecuteService Impl
          }
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(runnable, 0, 60, TimeUnit.SECONDS);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM