/**
  * Actual writing occurs here.
  *
  * @param logEvent The LogEvent.
  */
 @Override
 public void append(LogEvent logEvent) {
   if (!isStarted()) {
     throw new IllegalStateException("AsyncAppender " + getName() + " is not active");
   }
   if (!(logEvent instanceof Log4jLogEvent)) {
     if (!(logEvent instanceof RingBufferLogEvent)) {
       return; // only know how to Serialize Log4jLogEvents and RingBufferLogEvents
     }
     logEvent = ((RingBufferLogEvent) logEvent).createMemento();
   }
   logEvent.getMessage().getFormattedMessage(); // LOG4J2-763: ask message to freeze parameters
   final Log4jLogEvent coreEvent = (Log4jLogEvent) logEvent;
   boolean appendSuccessful = false;
   if (blocking) {
     if (isAppenderThread.get() == Boolean.TRUE && queue.remainingCapacity() == 0) {
       // LOG4J2-485: avoid deadlock that would result from trying
       // to add to a full queue from appender thread
       coreEvent.setEndOfBatch(false); // queue is definitely not empty!
       appendSuccessful = thread.callAppenders(coreEvent);
     } else {
       final Serializable serialized = Log4jLogEvent.serialize(coreEvent, includeLocation);
       try {
         // wait for free slots in the queue
         queue.put(serialized);
         appendSuccessful = true;
       } catch (final InterruptedException e) {
         // LOG4J2-1049: Some applications use Thread.interrupt() to send
         // messages between application threads. This does not necessarily
         // mean that the queue is full. To prevent dropping a log message,
         // quickly try to offer the event to the queue again.
         // (Yes, this means there is a possibility the same event is logged twice.)
         //
         // Finally, catching the InterruptedException means the
         // interrupted flag has been cleared on the current thread.
         // This may interfere with the application's expectation of
         // being interrupted, so when we are done, we set the interrupted
         // flag again.
         appendSuccessful = queue.offer(serialized);
         if (!appendSuccessful) {
           LOGGER.warn(
               "Interrupted while waiting for a free slot in the AsyncAppender LogEvent-queue {}",
               getName());
         }
         // set the interrupted flag again.
         Thread.currentThread().interrupt();
       }
     }
   } else {
     appendSuccessful = queue.offer(Log4jLogEvent.serialize(coreEvent, includeLocation));
     if (!appendSuccessful) {
       error("Appender " + getName() + " is unable to write primary appenders. queue is full");
     }
   }
   if (!appendSuccessful && errorAppender != null) {
     errorAppender.callAppender(coreEvent);
   }
 }
Example #2
0
 public static final void offerReadLatency(long latencyNanos, boolean pread) {
   if (pread) {
     fsPreadLatenciesNanos.offer(latencyNanos); // might be silently dropped, if the queue is full
     preadOps.incrementAndGet();
     preadTimeNano.addAndGet(latencyNanos);
   } else {
     fsReadLatenciesNanos.offer(latencyNanos); // might be silently dropped, if the queue is full
     readTimeNano.addAndGet(latencyNanos);
     readOps.incrementAndGet();
   }
 }
  public static void getBlogs() throws Exception {

    BlockingQueue<String> queue = new ArrayBlockingQueue<String>(numCrawler * 4);

    CrawlerC[] crawler = new CrawlerC[numCrawler];
    for (int i = 0; i < crawler.length; i++) {
      crawler[i] = new CrawlerC(queue);
      crawler[i].start();
    }

    ResultSet rs = null;
    int offset = 1;
    while (true) {
      offset += 100;
      myStm.executeQuery("SELECT blogID from blogs where country = 'BR' LIMIT " + offset + ",100");
      System.out.println("\n---" + offset + "---");

      rs = myStm.getResultSet();
      try {
        if (!rs.first()) break;
        if (false) break;
        while (rs.next()) {
          // System.out.println(rs.getString("blogID"));
          if (!queue.offer(rs.getString("blogID"), 60, TimeUnit.SECONDS)) {
            System.out.println("Offer.Timeout");
          }
        }
      } catch (Exception e) {
      }
    }

    queue.clear();
    for (int i = 0; i < crawler.length; i++) queue.put(CrawlerC.NO_MORE_WORK);
    for (int i = 0; i < crawler.length; i++) crawler[i].join();
  }
 @Override
 public void run() {
   while (true) {
     Socket socket = null;
     try {
       logger.info("Server listening on " + this.serverSocket.getLocalPort());
       socket = this.serverSocket.accept();
       while (true) {
         byte[] data = decoder.deserialize(socket.getInputStream());
         queue.offer(new String(data));
       }
     } catch (SoftEndOfStreamException e) {
       // normal close
     } catch (IOException e) {
       try {
         if (socket != null) {
           socket.close();
         }
       } catch (IOException e1) {
       }
       logger.error(e.getMessage());
       if (this.stopped) {
         logger.info("Server stopped on " + this.serverSocket.getLocalPort());
         break;
       }
     }
   }
 }
 public void offer(Object event) {
   if (queue.offer(event)) {
     stats.registerEventEnqueued();
   } else {
     stats.registerEventDropped();
   }
 }
 private void acceptMessage(byte[] bytes) {
   try {
     received.offer((Message) serializer.deserialize(bytes));
   } catch (IOException | ClassCastException e) {
     logger.trace("Got some trash");
   }
 }
Example #7
0
  public void loadImage(
      Context context, RemoteControlClient remoteControl, MusicDirectory.Entry entry) {
    if (largeUnknownImage != null
        && ((BitmapDrawable) largeUnknownImage).getBitmap().isRecycled()) {
      createLargeUnknownImage(context);
    }

    if (entry == null || entry.getCoverArt() == null) {
      setUnknownImage(remoteControl);
      return;
    }

    Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), imageSizeLarge));
    if (bitmap != null && !bitmap.isRecycled()) {
      Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
      setImage(remoteControl, drawable);
      return;
    }

    setUnknownImage(remoteControl);
    queue.offer(
        new Task(
            context,
            entry,
            imageSizeLarge,
            imageSizeLarge,
            false,
            new RemoteControlClientTaskHandler(remoteControl)));
  }
Example #8
0
  public void loadImage(View view, MusicDirectory.Entry entry, boolean large, boolean crossfade) {
    if (largeUnknownImage != null
        && ((BitmapDrawable) largeUnknownImage).getBitmap().isRecycled()) {
      createLargeUnknownImage(view.getContext());
    }

    if (entry == null || entry.getCoverArt() == null) {
      setUnknownImage(view, large);
      return;
    }

    int size = large ? imageSizeLarge : imageSizeDefault;
    Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size));
    if (bitmap != null && !bitmap.isRecycled()) {
      final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
      setImage(view, drawable, large);
      if (large) {
        nowPlaying = bitmap;
      }
      return;
    }

    if (!large) {
      setUnknownImage(view, large);
    }
    queue.offer(
        new Task(
            view.getContext(),
            entry,
            size,
            imageSizeLarge,
            large,
            new ViewTaskHandler(view, crossfade)));
  }
Example #9
0
  // 重置在指定时间内未完成的任务
  protected void checkTaskStatus() {
    Iterator<String> taskIds = statusPool.keySet().iterator();

    while (taskIds.hasNext()) {
      String taskId = taskIds.next();

      JobTaskStatus taskStatus = statusPool.get(taskId);
      JobTask jobTask = jobTaskPool.get(taskId);

      if (taskStatus == JobTaskStatus.DOING
          && jobTask.getStartTime() != 0
          && System.currentTimeMillis() - jobTask.getStartTime()
              >= jobTask.getTaskRecycleTime() * 1000) {
        if (statusPool.replace(taskId, JobTaskStatus.DOING, JobTaskStatus.UNDO)) {
          jobTask.setStatus(JobTaskStatus.UNDO);
          undoTaskQueue.offer(jobTask);
          jobTask.getRecycleCounter().incrementAndGet();

          if (logger.isWarnEnabled())
            logger.warn(
                "Task : " + jobTask.getTaskId() + " can't complete in time, it be recycle.");
        }
      }
    }
  }
 public void dispatch(Session session, Runnable event) {
   Worker worker = null;
   synchronized (mainLock) {
     worker = getWorker(session);
   }
   if (Thread.currentThread() == worker) {
     dispatcher.dispatch(session, event);
   } else {
     BlockingQueue queue = worker.queue;
     if (!queue.offer(event)) {
       // flow control
       if (elapsedTime.getElapsedTime() >= 1000) {
         elapsedTime.reset();
         log.warn("dispatcher flow control " + queue.size());
       }
       try {
         // queue.put(event);
         log.info(
             "dispatcher:active workor size="
                 + activeWorkers.length
                 + " and currentConcurrent="
                 + currentConcurrent);
         event.run();
       } catch (Throwable e) { // protect catch
         log.error(e, e);
       }
     }
   }
 }
Example #11
0
 public void addToQueue(Buffer data) throws Exception {
   if (canRun())
     if (!send_queue.offer(data, server.sock_conn_timeout, TimeUnit.MILLISECONDS))
       server.log.warn(
           "%s: discarding message because TCP send_queue is full and hasn't been releasing for %d ms",
           server.local_addr, server.sock_conn_timeout);
 }
Example #12
0
  private void submit(String name, String value, String type, String tags) {
    StringBuilder sb = new StringBuilder(name);

    sb.append(':').append(value).append('|').append(type);
    appendTags(tags, sb);
    q.offer(sb.toString());
  }
Example #13
0
 private void quit() {
   try {
     queue.offer(quitToken, 60, TimeUnit.SECONDS);
   } catch (InterruptedException e) {
     logger.warn(e.getMessage(), e);
   }
 }
 /**
  * Append a message to the list. If the list is full, the first message will be popped off of it.
  *
  * @param m log to be inserted
  */
 public synchronized void appendMessage(final LogCatMessage m) {
   if (mQ.remainingCapacity() == 0) {
     /* make space by removing the first entry */
     mQ.poll();
   }
   mQ.offer(m);
 }
 public void addKnownInput(
     String hostName,
     int port,
     InputAttemptIdentifier srcAttemptIdentifier,
     int srcPhysicalIndex) {
   String identifier = InputHost.createIdentifier(hostName, port);
   InputHost host = knownSrcHosts.get(identifier);
   if (host == null) {
     host = new InputHost(hostName, port, inputContext.getApplicationId(), srcPhysicalIndex);
     assert identifier.equals(host.getIdentifier());
     InputHost old = knownSrcHosts.putIfAbsent(identifier, host);
     if (old != null) {
       host = old;
     }
   }
   if (LOG.isDebugEnabled()) {
     LOG.debug("Adding input: " + srcAttemptIdentifier + ", to host: " + host);
   }
   host.addKnownInput(srcAttemptIdentifier);
   lock.lock();
   try {
     boolean added = pendingHosts.offer(host);
     if (!added) {
       String errorMessage = "Unable to add host: " + host.getIdentifier() + " to pending queue";
       LOG.error(errorMessage);
       throw new TezUncheckedException(errorMessage);
     }
     wakeLoop.signal();
   } finally {
     lock.unlock();
   }
 }
 /** {@inheritDoc} */
 protected void doPublish(final ExtLogRecord record) {
   switch (state) {
     case 0:
       {
         if (stateUpdater.compareAndSet(this, 0, 1)) {
           thread.start();
         }
       }
     case 1:
       {
         break;
       }
     default:
       {
         return;
       }
   }
   final BlockingQueue<ExtLogRecord> recordQueue = this.recordQueue;
   // prepare record to move to another thread
   record.copyAll();
   if (overflowAction == OverflowAction.DISCARD) {
     recordQueue.offer(record);
   } else {
     try {
       recordQueue.put(record);
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
       return;
     }
   }
 }
Example #17
0
 public void schedule(IManagerCommand command) {
   if (myClosed) {
     command.cancel();
   } else {
     myCommandQueue.offer(command);
   }
 }
Example #18
0
  /**
   * Passes image to fully-qualified name.
   *
   * @param instanceId
   * @param fullInName
   * @param image
   */
  public void put(String instanceId, String outName, String fullInName, ItemWrapper item) {
    // show debugging information
    if (null != m_debugger) {
      String inName = fullInName;
      int index = fullInName.indexOf('.');
      if (index > 0) {
        inName = fullInName.substring(++index);
      }
      DebugInfo debugInfo = new DebugInfo(instanceId, outName + " to " + inName, item);
      m_debugger.addDebugInfo(debugInfo);
    }

    boolean success = false;
    BlockingQueue<ItemWrapper> queue = getQueue(fullInName);
    // TODO currently using an unlimited LinkedBlockingQueue, so will never block
    while (!success) {
      try {
        success = queue.offer(item, 100, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Put interrupted");
      }
      if (m_quit) {
        throw new TeardownException("Teardown");
      }
    }
  }
  /** {@inheritDoc} */
  @Override
  public void collect(final Span span) {

    final long start = System.currentTimeMillis();
    try {

      if (!defaultAnnotations.isEmpty()) {
        for (final BinaryAnnotation ba : defaultAnnotations) {
          span.addToBinary_annotations(ba);
        }
      }

      final boolean offer = spanQueue.offer(span, 5, TimeUnit.SECONDS);
      if (!offer) {
        LOGGER.error(
            "It took to long to offer Span to queue (more than 5 seconds). Span not submitted: "
                + span);
      } else {
        final long end = System.currentTimeMillis();
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Adding span to queue took " + (end - start) + "ms.");
        }
      }
    } catch (final InterruptedException e1) {
      LOGGER.error("Unable to submit span to queue: " + span, e1);
    }
  }
  public boolean consumeMessage(List<MessageExt> msgs, MessageQueue mq) {
    LOG.debug("Receiving {} messages {} from MQ {} !", new Object[] {msgs.size(), msgs, mq});

    if (msgs == null || msgs.isEmpty()) {
      return true;
    }

    BatchMessage batchMsgs = new BatchMessage(msgs, mq);

    cache.put(batchMsgs.getBatchId(), batchMsgs);

    batchQueue.offer(batchMsgs);

    try {
      boolean isDone = batchMsgs.waitFinish();
      if (!isDone) {
        cache.remove(batchMsgs.getBatchId());
        return false;
      }
    } catch (InterruptedException e) {
      cache.remove(batchMsgs.getBatchId());
      return false;
    }

    return batchMsgs.isSuccess();
  }
 public boolean putMessage(String message, long timeout) {
   BufferListener listener = listenerRef.get();
   if (listener != null) {
     try {
       if (queue.size() == 0) {
         return listener.onMessage(message);
       } else {
         ArrayList<String> messages = new ArrayList<String>(queue.size() + 1);
         queue.drainTo(messages);
         messages.add(message);
         return listener.onMessages(messages);
       }
     } catch (Throwable t) {
       return false;
     }
   } else {
     try {
       if (!inputSemaphore.tryAcquire(message.length(), timeout, TimeUnit.MILLISECONDS)) {
         return false;
       }
       queue.offer(message);
       return true;
     } catch (InterruptedException e) {
       return false;
     }
   }
 }
 protected void offerData(byte[] data, Executor executor) {
   if (dataQueue.offer(data)) {
     handleQueue(executor);
   } else {
     offerDataError();
   }
 }
 public void process(LoggingEvent loggingEvent) {
   // we ignore events for SLF4J
   if (!ORG_SLF4J_IMPL_JCL_LOGGER_ADAPTER.equals(
       loggingEvent.getLocationInformation().getClassName())) {
     queue.offer(loggingEvent);
   }
 }
 public void delete(K key) {
   store.remove(key);
   callCount.incrementAndGet();
   if (deleteLatch != null) {
     deleteLatch.countDown();
   }
   events.offer(STORE_EVENTS.DELETE);
 }
 private void queueMessage(VoiceConferenceEvent event) {
   try {
     messages.offer(event, 5, TimeUnit.SECONDS);
   } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Example #26
0
 public void abort(XMLStreamException fatal) {
   abort = true;
   if (fatal != null) {
     this.fatal = fatal;
   }
   queue.clear();
   queue.offer(CLOSED);
 }
 void stop() {
   stop = true;
   if (!crawlerResponseQueue.offer(CrawlerTask.createExitTask())) {
     logger.warning("Failed to add STOP sentinel to crawler response queue");
   }
   synchronized (requestLock) {
     if (activeRequest != null) activeRequest.abort();
   }
 }
 public Set<K> loadAllKeys() {
   if (loadAllLatch != null) {
     loadAllLatch.countDown();
   }
   callCount.incrementAndGet();
   events.offer(STORE_EVENTS.LOAD_ALL_KEYS);
   if (!loadAllKeys) return null;
   return store.keySet();
 }
 public void store(K key, V value) {
   store.put(key, value);
   callCount.incrementAndGet();
   storeCount.incrementAndGet();
   if (storeLatch != null) {
     storeLatch.countDown();
   }
   events.offer(STORE_EVENTS.STORE);
 }
Example #30
0
  private void submit(String name, long value, double sampleRate, String type, String tags) {
    StringBuilder sb = new StringBuilder(name);
    Formatter fmt = new Formatter(sb);

    sb.append(':').append(value).append('|').append(type);
    appendSampleRate(sampleRate, sb, fmt);
    appendTags(tags, sb);
    q.offer(sb.toString());
  }