public Thread newThread()

in uimaj-as-core/src/main/java/org/apache/uima/aae/UimaAsPriorityBasedThreadFactory.java [114:230]


  public Thread newThread(final Runnable r) {
    Thread newThread = null;
    try {
      newThread = new Thread(theThreadGroup, new Runnable() {
        public void run() {
          if ( threadNamePrefix == null ) {
             if ( controller != null ) {
               threadNamePrefix = THREAD_POOL+poolId+"] "+controller.getComponentName() + " Process Thread";
             } else {
               threadNamePrefix = THREAD_POOL+poolId+"] ";
             }
          } 
          boolean interruptAllThreads = false;
          Thread.currentThread().setName( threadNamePrefix +" - "                 
                          + Thread.currentThread().getId());
          try {
            if (controller != null && 
            	controller instanceof PrimitiveAnalysisEngineController && 
            	!((PrimitiveAnalysisEngineController)controller).threadAssignedToAE())  {
              // call the controller to initialize next instance of AE. Once initialized this
              // AE instance process() method will only be called from this thread
			  UIMAFramework.getLogger(CLASS_NAME).logrb(Level.INFO, getClass().getName(),
					"UimaAsPriorityBasedThreadFactory.run()", UIMAEE_Constants.JMS_LOG_RESOURCE_BUNDLE,
					"UIMAEE_calling_ae_initialize__INFO", new Object[] {controller.getComponentName(),Thread.currentThread().getId()});
             
			  if ( !initFailed && !controller.getState().equals(ServiceState.FAILED) ) {
            	  try {
            		  ((PrimitiveAnalysisEngineController)controller).initializeAnalysisEngine();
            	  } catch( Exception e) {
            		  initFailed = true;
            		  e.printStackTrace();
            		  throw e;
            	  }
              } else {
            	  return; // there was failure previously so just return
              }
            }
            // runs forever until controll is stopped
            while (!controller.isStopped()) {
            	// block until a message arrives or timeout. On timeout, the pool returns null
            	MessageWrapper m = queue.poll(100,
            			TimeUnit.MILLISECONDS);
            	if (m == null) {
            		// nothing received, try again
            		continue;
            	}
            	// 'poison pill' sent when controller stops
            	if (m.getMessage() == null
            			&& m.getSemaphore() == null
            			&& m.getSession() == null) {
            		interruptAllThreads = true;
            		break;
            	} else {
            		// got a message, so process it by passing it on the input channel
            		ic.onMessage(m);
            	}
            }
            	
          } catch (Throwable e) {
            if ( !(e instanceof Exception) ) {
              //   try to log. If this is OOM, logging may not succeed and we
              //   get another OOM.
              try {
                UIMAFramework.getLogger(CLASS_NAME).logrb(Level.WARNING, getClass().getName(),
                        "UimaAsPriorityBasedThreadFactory", UIMAEE_Constants.JMS_LOG_RESOURCE_BUNDLE,
                        "UIMAEE_exception__WARNING", e);
                System.out.println(">>>>>>>>>>>>>>>>>>Exiting UIMA AS Process Due to Java Error "+e);
              } catch( Throwable t ) {
                 // Failed during logging. We are tight on memory. Just exit 
              }
              System.exit(-1);
              
            }
            return;
          } finally {
              if ( controller instanceof PrimitiveAnalysisEngineController_impl ) {
       			     UIMAFramework.getLogger(CLASS_NAME).logrb(Level.INFO, getClass().getName(),
        					"UimaAsPriorityBasedThreadFactory.run", UIMAEE_Constants.JMS_LOG_RESOURCE_BUNDLE,
        					"UIMAEE_process_thread_exiting__INFO", new Object[] {controller.getComponentName(),Thread.currentThread().getId()});
            	  ((PrimitiveAnalysisEngineController_impl)controller).destroyAE();
        			  UIMAFramework.getLogger(CLASS_NAME).logrb(Level.INFO, getClass().getName(),
         					"UimaAsPriorityBasedThreadFactory.run()", UIMAEE_Constants.JMS_LOG_RESOURCE_BUNDLE,
         					"UIMAEE_ae_instance_destroy_called__INFO", new Object[] {controller.getComponentName(),Thread.currentThread().getId()});
        			  if ( latchToCountNumberOfTerminatedThreads != null ) {
                  synchronized( latchToCountNumberOfTerminatedThreads ) {
                    latchToCountNumberOfTerminatedThreads.countDown();
                   }
        			  }
              }
              if ( interruptAllThreads ) {
            	  for( Thread t : tList ) {
            		  t.interrupt();
            	  }
              }
          }
        
        }
      });
    } catch (Exception e) {
      if (UIMAFramework.getLogger(CLASS_NAME).isLoggable(Level.WARNING)) {
        if ( controller != null ) {
          UIMAFramework.getLogger(CLASS_NAME).logrb(Level.WARNING, CLASS_NAME.getName(),
                  "UimaAsPriorityBasedThreadFactory", UIMAEE_Constants.JMS_LOG_RESOURCE_BUNDLE,
                  "UIMAEE_service_exception_WARNING", controller.getComponentName());
        }

        UIMAFramework.getLogger(CLASS_NAME).logrb(Level.WARNING, getClass().getName(),
                "UimaAsPriorityBasedThreadFactory", UIMAEE_Constants.JMS_LOG_RESOURCE_BUNDLE,
                "UIMAEE_exception__WARNING", e);
      }
    }
    if ( newThread != null ) {
      newThread.setDaemon(isDaemon);
      tList.add(newThread);
    }
    return newThread;
  }