Author: bhavani
Date: Fri Dec  3 21:24:15 2010
New Revision: 1042012

URL: http://svn.apache.org/viewvc?rev=1042012&view=rev
Log:
UIMA-1964 fixes to shutting down the process when Java controller is killed.

Modified:
    uima/uimacpp/trunk/src/utils/deployCppService.cpp
    uima/uimacpp/trunk/src/utils/deployCppService.hpp

Modified: uima/uimacpp/trunk/src/utils/deployCppService.cpp
URL: 
http://svn.apache.org/viewvc/uima/uimacpp/trunk/src/utils/deployCppService.cpp?rev=1042012&r1=1042011&r2=1042012&view=diff
==============================================================================
--- uima/uimacpp/trunk/src/utils/deployCppService.cpp (original)
+++ uima/uimacpp/trunk/src/utils/deployCppService.cpp Fri Dec  3 21:24:15 2010
@@ -88,37 +88,46 @@ int main(int argc, char* argv[]) {
     aeService.startProcessingThreads();
 
     /*start receiving messages*/ 
-    cout << __FILE__ << __LINE__  << " Start receiving messages " << endl;
+    cerr << __FILE__ << __LINE__  << " Start receiving messages " << endl;
     aeService.start();
 
-    cout << __FILE__ << " UIMA C++ Service " << serviceDesc.getQueueName() << 
" at " <<
+    //check if java process still there
+   
+
+    cerr << __FILE__ << " UIMA C++ Service " << serviceDesc.getQueueName() << 
" at " <<
     serviceDesc.getBrokerURL() << " Ready to process..." << endl;
     
     /* connect to java proxy if called from java */  
     apr_thread_t *thread=0;
     apr_threadattr_t *thd_attr=0;
+
     if (cs) {
       apr_threadattr_create(&thd_attr, pool);
       rv = apr_thread_create(&thread, thd_attr, handleCommands, 0, pool);
       assert(rv == APR_SUCCESS);
       //rv = apr_thread_join(&rv, thread);
       //assert(rv == APR_SUCCESS);
+      apr_thread_mutex_lock(singleton_pMonitor->cond_mutex);
+      apr_thread_cond_wait(singleton_pMonitor->cond, 
singleton_pMonitor->cond_mutex);
+      apr_thread_mutex_unlock(singleton_pMonitor->cond_mutex);
+
     }  else {
+      uima::ResourceManager::getInstance().getLogger().logError(" not from 
java running ");
       apr_thread_t *stdinthread=0;
       rv = apr_thread_create(&stdinthread, thd_attr, readstdin, 0, pool);
     }  
 
-    //wait 
-    apr_thread_mutex_lock(singleton_pMonitor->cond_mutex);
-    apr_thread_cond_wait(singleton_pMonitor->cond, 
singleton_pMonitor->cond_mutex);
-    apr_thread_mutex_unlock(singleton_pMonitor->cond_mutex);    
-    
+    //wait
+/**    if (cs) { 
+      apr_thread_mutex_lock(singleton_pMonitor->cond_mutex);
+      apr_thread_cond_wait(singleton_pMonitor->cond, 
singleton_pMonitor->cond_mutex);
+      apr_thread_mutex_unlock(singleton_pMonitor->cond_mutex);    
+    }
+**/
     if (singleton_pMonitor->getQuiesceAndStop()) {
       cerr << __FILE__ << " " << serviceDesc.getServiceName() << " Quiesce 
started. " << endl;
-
       //quiesce 
       aeService.quiesceAndStop();
-      
       cerr << __FILE__ << " " << serviceDesc.getServiceName() << " quiesced. " 
<< endl;
     } else {
       cerr << __FILE__ << " Shutdown started. " << endl;

Modified: uima/uimacpp/trunk/src/utils/deployCppService.hpp
URL: 
http://svn.apache.org/viewvc/uima/uimacpp/trunk/src/utils/deployCppService.hpp?rev=1042012&r1=1042011&r2=1042012&view=diff
==============================================================================
--- uima/uimacpp/trunk/src/utils/deployCppService.hpp (original)
+++ uima/uimacpp/trunk/src/utils/deployCppService.hpp Fri Dec  3 21:24:15 2010
@@ -748,14 +748,18 @@ static void signal_handler(int signum) {
   stringstream str;
   str << __FILE__ << __LINE__ << " Received Signal: " << signum;
   cerr << str.str() << endl;
-  //singleton_pMonitor->shutdown();
-  singleton_pMonitor->setQuiesceAndStop();
+  singleton_pMonitor->shutdown();
+  //terminateService();
+  //activemq::library::ActiveMQCPP::shutdownLibrary();
+  // exit(0);
+
+  //singleton_pMonitor->setQuiesceAndStop();
 }
 
 
 static int terminateService() {
 
-  cout << "deployCppService::terminateService" << endl;
+  cout << __FILE__ << __LINE__ << "::terminateService" << endl;
 
   if (cs) {
     apr_socket_close(cs);
@@ -865,18 +869,33 @@ static void* APR_THREAD_FUNC handleComma
   //to controller.
   string msg = "0";
   apr_size_t len = msg.length();
+  apr_socket_timeout_set(cs, 5000000);
   rv = apr_socket_send(cs, msg.c_str(), &len);
+  if (rv != APR_SUCCESS || len != msg.length() ) {
+    singleton_pMonitor->shutdown();
+    return 0;
+  }
   len = 1;
   apr_socket_send(cs,"\n", &len);
-  cout << "sent 0 to controller " << endl;
+  //cout << "sent 0 to controller " << endl;
   //receive JMX, admin requests from controller 
   char buf[16];
   memset(buf,0,16);
   len = 16;
-  while ( (rv = apr_socket_recv(cs, buf, &len)) != APR_EOF) {
+  apr_socket_timeout_set(cs,-1);
+  bool doloop = true;
+  //while ( (rv = apr_socket_recv(cs, buf, &len)) != APR_EOF) {
+  while (doloop) {
+    rv = apr_socket_recv(cs,buf,&len);
+    if (APR_STATUS_IS_TIMEUP(rv) ) continue;    
+    if (APR_STATUS_IS_EOF(rv)  || APR_STATUS_IS_ECONNRESET(rv) ) {
+      cerr << __FILE__ << __LINE__ << "::handleCommands Connection broken... 
apr_status=" << rv << endl;
+      doloop = false;
+      continue;
+    }
     string command = buf;
     memset(buf,0,16);
-    cout << len << " apr_socket_recv command=" << command << endl;
+    cerr << rv << " " << len << " apr_socket_recv command=" << command << endl;
     len=16;
     if (command.compare("GETSTATS")==0) {
       
//singleton_pLogger->log(LogStream::EnMessage,"deployCppService","getStats","retrieving
 stats",0);
@@ -897,8 +916,10 @@ static void* APR_THREAD_FUNC handleComma
     } else {
       if (rv != APR_SUCCESS) {
         singleton_pMonitor->shutdown();
+        doloop = false;
         break;
-      } else {
+      }   
+      else {
         char * c = new char[256];
         apr_strerror(rv, c, 255);
         stringstream str;
@@ -909,9 +930,9 @@ static void* APR_THREAD_FUNC handleComma
       }
     }
   }
-  apr_thread_exit(thd, APR_SUCCESS);
-  cout << "deployCppService::handleCommand() calling shutdown. " << endl;
+  cout << __FILE__ << __LINE__ << "::handleCommand() calling shutdown. " << 
endl;
   singleton_pMonitor->shutdown();
+  apr_thread_exit(thd,APR_SUCCESS);
   return NULL;
 }
 


Reply via email to