Implementing timeout for a synchronous method

Sometimes, a java call blocks forever. This may cause serious problems like blokking processing or stuck threads. In these cases, you can choose to set a timeout to break the execution and eventually roll back the transaction.

To call a Java synchronous method with a timeout, you can use Executorservice as showed below.

package com.nuon.infostroom.process;
import java.util.Random;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.TimeoutException;
import com.nuon.infostroom.cts.ws.CTSApplicationException;

import com.nuon.infostroom.cts.ws.CTSSystemException;
class ExceptionThrower {

public static <R> R throwUnchecked(Throwable t) {

return ExceptionThrower.<RuntimeException, R> trhow0(t);

}
@SuppressWarnings(“unchecked”)

private static <E extends Throwable, R> R trhow0(Throwable t) throws E {

throw (E) t;

}

}
class TestApplicationException1 extends Exception {

private static final long serialVersionUID = 1L;
public TestApplicationException1(String string) {

super(string);

}

};
class TestApplicationException2 extends Exception {

private static final long serialVersionUID = 1L;
public TestApplicationException2(String string) {

super(string);

}

};
class TestApplicationTimeoutException extends Exception {

private static final long serialVersionUID = 1L;
public TestApplicationTimeoutException(String string) {

super(string);

};

}
public class SynchronousTimeoutTester {
public static final long SYNC_METHOD_TIMEOUT_IN_MILLISECONDS = 2000L;

private final ExecutorService executorService = Executors.newSingleThreadExecutor();
public static void main(String[] args) {

SynchronousTimeoutTester tester = new SynchronousTimeoutTester();

for (int i = 0; i < 10; i++) {

try {

System.out.println(“Result sync call: ” + tester.getAsynchTest());

}

catch (TestApplicationException1 e) {

System.out.println(“catched as TestApplicationException1: ” + e);

}

catch (TestApplicationException2 e) {

System.out.println(“catched as TestApplicationException2: ” + e);

}

catch (TestApplicationTimeoutException e) {

System.out.println(“catched as TestApplicationTimeoutException: ” + e);

}

catch (InterruptedException e) {

System.out.println(“catched as InterruptedException: ” + e);

}

catch (Exception e) {

System.out.println(“catched as Exception: ” + e);

}

}
tester.shutdown();

}
private void shutdown() {

executorService.shutdown();

try {

executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

}

catch (InterruptedException e) {

System.out.println(“Error stopping threadpool:” + e);

}

}
private Integer testAsynch() throws TestApplicationException1, TestApplicationException2, InterruptedException {

Random random = new Random();

switch (random.nextInt(10)) {

case 0:

return 0;

case 1:

throw new TestApplicationException1(“thrown TestApplicationException1”);

case 2:

throw new TestApplicationException2(“thrown TestApplicationException2”);

case 3:

Thread.sleep(10000L);

return -1;

case 4:

throw new RuntimeException(“thrown Exception”);

default:

return random.nextInt(10);

}

}
private Integer getAsynchTest() throws TestApplicationException1, TestApplicationException2, Exception {

Integer dummy = null;
Future<Integer> testAsynchF = executorService.submit(

new Callable<Integer>() {

public Integer call() throws Exception {

return testAsynch();

}

});
try {

dummy = testAsynchF.get(SynchronousTimeoutTester.SYNC_METHOD_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS);

}

catch (ExecutionException e1) {

System.out.println(“in getAsynchTest: ExecutionException: ” + e1);

ExceptionThrower.throwUnchecked(e1.getCause());

}

catch (TimeoutException e1) {

System.out.println(“in getAsynchTest: TimeoutException: ” + e1);

throw new TestApplicationTimeoutException(“TimeoutException” + e1);

}

catch (InterruptedException e1) {

System.out.println(“in getAsynchTest: InterruptedException: ” + e1);

throw new Exception(e1);

}
return dummy;

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s