我有一个正在调用外部应用程序的 Restful 服务。此应用程序使我的服务挂起。因此,当用户调用我的服务时,由于这个外部应用程序可能需要一个小时。外部应用程序只需几秒钟即可执行。否则,出了点问题。所以我希望我的服务中的代码最多执行 30 秒。如果超过 30 秒,我想停止服务并重新启动它。
这是我想要的:
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
}
我不希望代码每 30 秒执行一次。我希望能够停止代码执行并从头开始重新启动它。
这是服务:
@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...
}