@Override
 public void run() {
   isAppenderThread.set(Boolean.TRUE); // LOG4J2-485
   while (!shutdown) {
     Serializable s;
     try {
       s = queue.take();
       if (s != null && s instanceof String && SHUTDOWN.equals(s.toString())) {
         shutdown = true;
         continue;
       }
     } catch (final InterruptedException ex) {
       break; // LOG4J2-830
     }
     final Log4jLogEvent event = Log4jLogEvent.deserialize(s);
     event.setEndOfBatch(queue.isEmpty());
     final boolean success = callAppenders(event);
     if (!success && errorAppender != null) {
       try {
         errorAppender.callAppender(event);
       } catch (final Exception ex) {
         // Silently accept the error.
       }
     }
   }
   // Process any remaining items in the queue.
   LOGGER.trace(
       "AsyncAppender.AsyncThread shutting down. Processing remaining {} queue events.",
       queue.size());
   int count = 0;
   int ignored = 0;
   while (!queue.isEmpty()) {
     try {
       final Serializable s = queue.take();
       if (Log4jLogEvent.canDeserialize(s)) {
         final Log4jLogEvent event = Log4jLogEvent.deserialize(s);
         event.setEndOfBatch(queue.isEmpty());
         callAppenders(event);
         count++;
       } else {
         ignored++;
         LOGGER.trace("Ignoring event of class {}", s.getClass().getName());
       }
     } catch (final InterruptedException ex) {
       // May have been interrupted to shut down.
       // Here we ignore interrupts and try to process all remaining events.
     }
   }
   LOGGER.trace(
       "AsyncAppender.AsyncThread stopped. Queue has {} events remaining. "
           + "Processed {} and ignored {} events since shutdown started.",
       queue.size(),
       count,
       ignored);
 }
示例#2
0
  /**
   * When in portable mode (default) this method verifies that the container has reached the
   * specified minimal state. If it hasn't, an {@link IllegalStateException} is thrown. When in
   * non-portable mode this method is no-op.
   *
   * @param methodName
   * @param minimalState the minimal state
   * @throws IllegalStateException If the application initialization is not finished yet
   */
  private void checkContainerState(String methodName, ContainerState minimalState) {
    if (nonPortableMode) {
      return;
    }
    if (this.container == null) {
      this.container = Container.instance(manager);
    }

    ContainerState state = container.getState();
    if (SHUTDOWN.equals(state)) {
      throw BeanManagerLogger.LOG.methodNotAvailableAfterShutdown(methodName);
    }
    if (state.compareTo(minimalState) < 0) {
      throw BeanManagerLogger.LOG.methodNotAvailableDuringInitialization(methodName, state);
    }
  }