0

I have a Restful Service that is calling an external application. This application is making my service hang. So when a user calls my service, it may take an hour due to this external application. The external application should only take a few seconds to execute. Otherwise, something has gone wrong. So I want the code in my service to execute for up to 30 seconds. If it passes the 30 second mark, I want to stop the service and restart it.

Here is what I am wanting:

    public static void main(String[] args){

    Thread thread = new Thread(){
        public void run(){
            System.out.println("Hello Thread");
        }
    };

    thread.start();

    //if thread is alive in 30 seconds, stop and retry

}

I am not wanting the code to execute every 30 seconds. I want to be able to stop the code execution and restart it from the beginning.

Here is the service:

@Path("/test")
@GET
@Produces(MediaType.APPLICATION_JSON )
public Response test(@QueryParam("input") String input) {
    //call external application... if it hangs, it will take more than 30 seconds...

}
Chris Bolton
  • 2,162
  • 4
  • 36
  • 75

2 Answers2

1

You're talking about a Timeout, right?

The way that I have commonly implemented the TimeoutCmd interface in this class is to close the InputStream you are trying to read. This will spark an exception and free up the thread that is waiting.

Other times, using Thread.interrupt can be effective at stopping a long-running thread, if it checks on the THread.interrupt value periodically

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • So I am wanting my clients to directly call my service and not instantiate anything. So I am having issues on how this can be implemented with my service. Maybe I should make an inner class? – Chris Bolton Jul 10 '15 at 19:55
0

http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

You can use a TimerTask to do this.

Paj
  • 11
  • 7
  • I've looked into trying this but no luck. Could you give an example of what you mean? – Chris Bolton Jul 10 '15 at 19:50
  • @ChrisBolton http://www.mkyong.com/java/jdk-timer-scheduler-example/ is a good example. Timer creates its own thread when you schedule it so you don't need to. – Paj Jul 10 '15 at 20:00
  • http://stackoverflow.com/questions/10029831/how-do-you-use-a-timertask-to-run-a-thread Another good example for you. – Paj Jul 10 '15 at 20:01
  • I am going to play with these and see if I can get them to work with my scenario. Thanks. The issue is that it is a web service that I want to incorporate a TimerTask. – Chris Bolton Jul 10 '15 at 20:06
  • Most of what I am seeing here is to run code every X amount of time. – Chris Bolton Jul 10 '15 at 20:16
  • You could use TimerTask to check if the thread is running and take appropriate action....Then after cancel the timer. .isAlive() on the thread checks it. It is complicated to just stop a running Thread but Thread.interrupt will probably do the trick for you and then you can just redo what you need to with a new thread. – Paj Jul 10 '15 at 20:37