java - using Apache HttpClient in a new thread, I don't know if it finished running -


i faced strange problem. hope find out reason. code:

    public static void asyncsend(final roomnotification notification, final int retrytimes) {      thread thread = new thread(new runnable() {         @override         public void run() {             boolean finish = false;             try {                 objectmapper mapper = new objectmapper();                 string messagestring = mapper.writevalueasstring(notification);                 logger.info("json send hipchat :{}", messagestring);     #1          content content = request.post("https://api.hipchat.com/v2/room/<hidden>/notification?auth_token=<hidden>")                         .bodystring(messagestring, contenttype.application_json)                         .execute().returncontent();     #2          logger.info("hipchat return:{}", content.asstring());                 finish = true;             } catch (clientprotocolexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();             } {     #3          logger.info("send hipchat {}:\n{}", finish ? "successfully" : "unsuccessfully", notification.message);                 if (finish) {                     return;                 }                 //如果失败,且还有重试次数就重新发送                 if (retrytimes > 0) {                     logger.info("retry sending hipchat, retry times remain:{}\nmessage:{}", retrytimes, notification.getmessage());                     try {                         thread.sleep(10000); //重试前暂停10秒                     } catch (interruptedexception e) {                         e.printstacktrace();                     }                     asyncsend(notification, retrytimes - 1);                 } else {                     logger.info("no retry times remain, send hipchat unsuccessfully.\nmessage:{}", notification.getmessage());                 }             }         }     });      thread.start(); } 

the problem that, line @ neither #2 nor #3 run. mean thread crashed without exception, or line @ #1 never finished running? code @ line #1 reference apache httpcomponents.

i analysised log now, output shows below:

[wwwuser@mixi-mantou ~]$ grep "json send hipchat" tomcat-mixi/logs/catalina.out | wc -l 216 [wwwuser@mixi-mantou ~]$ grep "send hipchat successfully" tomcat-mixi/logs/catalina.out | wc -l 197 [wwwuser@mixi-mantou ~]$ grep "send hipchat unsuccessfully" tomcat-mixi/logs/catalina.out | wc -l 14 

so problem occured 5(216-197-14) times. wish me!

unless received error severe block not execute, problem request.post never returned.

there several ways can find out happening. of these should done while program still running, threads doing call should have ended.

  1. check open tcp connections: under both windows or unix use "netstat" in shell or command prompt. command shows current tcp connections between computer , server. check whether there more open connection there should be.

  2. create full stack dump: under unix find out pid of java process (using "ps"), issue "kill -quit [pid]" (replacing [pid] pid found). under windows can more complicated: if started java program command line, press ctrl+break. or use jstack (see https://blogs.oracle.com/pcmreddy/entry/using_jstack_on_windows). writes stdout stack trace of running threads. check whether threads still in request.post call.

you can while application still executing. have distinguish hanging threads running ok , happen in request.post call. might check tcp connections and/or stack traces few times, seconds in between, see has changed. might give threads names containing start time, easier detect long-running threads in thread dumps.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -