0

I am trying to do this thing, but not getting how? The code requests that Network Service send SMS Billing information; the Network Service's Responses are recorded when billing is succeeds or fails. Sometimes it takes long time to get the Response and I want to cancel/pause that process and re-send the request with new number.

long before = System.currentTimeMillis();
String resp = new SmsConnection().doResponseRequest(sms);
long totalResponseTime=((System.currentTimeMillis() - before )/1000);

I can only record totalResponseTime and sometime it takes 50-100 seconds to get the response from Service. Is there any way where I can say

"If resp is taking more than 15 seconds cancel/pause this Request and re-send another at the same time. After we receive the response we will process that request."

I need something like a TimeOut Option for receiving the response. Please suggest.

Thanks,

dsummersl
  • 6,588
  • 50
  • 65
Madan Madan
  • 674
  • 1
  • 11
  • 28

3 Answers3

0

You can probably use ExecutorService, by extracting new SmsConnection().doResponseRequest(sms); into Callable. You can see an example here.

Community
  • 1
  • 1
Dmitry Kuskov
  • 1,001
  • 6
  • 10
  • ..Thanks. This application is already in ExecutorService. I just want to cancel this part if it takes too much time to get the response. – Madan Madan Apr 29 '13 at 10:59
0

Use Callable:

Future<String> futureResponse = service.submit(new YourCallable); //service is the ExecutorService that you use
//this will block for 15 seconds here
String result = futureResponse.get(15, TimeUnit.SECONDS); //this needs to wrapped in a try catch with TimeoutException
if(result == null){
    //You did not get the result in 15 seconds
    futureResponse.cancel(true);//kill this task

    //schedule a new task
    service.submit(new YouCallable());

}
Eugene
  • 117,005
  • 15
  • 201
  • 306
0

Here is the simple solution for my problem! Thanks Eugene and Dmitry for supplying Hint and Suggestion! I created a method for receiving response and then called this method inside synchronized(this) {} block!!

 public String getResponse(final StsSmsSubmit sms) throws InterruptedException, ExecutionException
    {
        String response="";
       Callable<String> responsecode=new Callable<String>() {


        @Override
        public String call() throws Exception {

           final String resp = new StsSmsConnection().doRequest(sms);
           return resp;
        }
    };
       ExecutorService service=Executors.newSingleThreadExecutor();
         Future task=service.submit(responsecode);
         try{
         response=(String) task.get(30, TimeUnit.SECONDS);
    }
    catch(TimeoutException tE)
    {
       System.out.println("TimeOutException Occurred  "+tE.getMessage()+"at "+ new Date());
       log.fatal("TimeOut Exception is catched. at "+ new Date());
       response="TimeOut";
    }
         service.shutdown();


       return response;

    }
Madan Madan
  • 674
  • 1
  • 11
  • 28