Java Articles

Advertisement

Java ScheduledExecutorService Example

by Ram Satish

Share

You have got certain services running and you want the services to issue heartbeat as long as they are alive.

In this article, I will show you an example of a single-threaded executor that can be scheduled to issue heartbeat at regular intervals.

ScheduledExecutorService is the executor whichthat can schedule commands to run after a given delay, or to execute periodically.

We will use Executors.newSingleThreadScheduledExecutor() to create a single-threaded executor.

Execute a task scheduled to run at periodically

Advertisement

Let’s first create a POJO representing service.

Service:

package com.javarticles.concurrency;

public class Service {
private String serviceName;

public Service(String serviceName) {
this.serviceName = serviceName;
}

public void sendHeartbeat() {
System.out.println("Service " + serviceName + ", hearbeat at "
+ System.currentTimeMillis());
}

public String toString() {
return "Service " + serviceName;
}

}

Here is our heartbeat task. It implements Runnable as we want it to be run in the Executor framework.
In run we iterate through each service and call sendHeartbeat(). You may want to send a JMS message here or update a table in database.

HeartbeatTask:

package com.javarticles.concurrency;

import java.util.ArrayList;
import java.util.List;

public class HeartbeatTask implements Runnable {

private List services = new ArrayList();

public void addService(Service service) {
services.add(service);
}

public void run() {
try {
for (Service service : services) {
service.sendHeartbeat();
}
} catch (Exception e) {
}
}

void shutdown() {
for (final Service service : services) {
System.out.println("Shutting down service " + service);
}
}

}

ScheduledExecutorService Example

We first create the ScheduledExecutorService. It relies on Executors class which is the factory class for Executor and what is Executor?
Executor is the object that executes submitted Runnable task. Our Runnable task is the heartbeat task.

In order to create ScheduledExecutorService, we call newSingleThreadScheduledExecutor(). We add the services to be monitored to the HeartbeatTask.

Next, we schedule the task to be run using scheduleAtFixedRate(). The second parameter is the initial delay. Here it is 0 and the third parameter is the the period between successive executions. It is 1 second in our case.

In the finally block, we shutdown the heartbeatTask and then the ScheduledExecutorService.

heartbeatTask.shutdown();
scheduledExecutorService.shutdownNow();
scheduledExecutorService.scheduleAtFixedRate(heartbeatTask, 0, 1,
TimeUnit.SECONDS);
ScheduledExecutorService scheduledExecutorService = Executors
.newSingleThreadScheduledExecutor();

ThreadScheduledExecutorExample:

package com.javarticles.concurrency;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadScheduledExecutorExample {
public static final void main(String[] args) throws InterruptedException {
ScheduledExecutorService scheduledExecutorService = Executors
.newSingleThreadScheduledExecutor();
HeartbeatTask heartbeatTask = new HeartbeatTask();
heartbeatTask.addService(new Service("JmsConsumer"));
heartbeatTask.addService(new Service("DatabaseUpdateService"));
try {
scheduledExecutorService.scheduleAtFixedRate(heartbeatTask, 0, 1,
TimeUnit.SECONDS);
Thread.sleep(8000);
} finally {
System.out.println("Shutdown scheduledExecutorService");
heartbeatTask.shutdown();
scheduledExecutorService.shutdownNow();
}
}
}

Output:

Service JmsConsumer, hearbeat at 1437048213864
Service DatabaseUpdateService, hearbeat at 1437048213864
Service JmsConsumer, hearbeat at 1437048214887
Service DatabaseUpdateService, hearbeat at 1437048214887
Service JmsConsumer, hearbeat at 1437048215890
Service DatabaseUpdateService, hearbeat at 1437048215890
Service JmsConsumer, hearbeat at 1437048216886
Service DatabaseUpdateService, hearbeat at 1437048216886
Service JmsConsumer, hearbeat at 1437048217886
Service DatabaseUpdateService, hearbeat at 1437048217886
Service JmsConsumer, hearbeat at 1437048218885
Service DatabaseUpdateService, hearbeat at 1437048218885
Service JmsConsumer, hearbeat at 1437048219865
Service DatabaseUpdateService, hearbeat at 1437048219865
Service JmsConsumer, hearbeat at 1437048220883
Service DatabaseUpdateService, hearbeat at 1437048220883
Shutdown scheduledExecutorService
Service JmsConsumer, hearbeat at 1437048221886
Service DatabaseUpdateService, hearbeat at 1437048221886
Shutting down service Service JmsConsumer
Shutting down service Service DatabaseUpdateService

Download the source code

This was an example about Java ScheduledExecutorService Example.

You can download the source code here: JavaScheduledExecutorServiceExample.zip

Share

Advertisement

Related

Advertisement

Latest

Advertisement