public void run() {
    generateDefaultMessageImage();
    while (!stopped) {
      try {
        if (queue.size() == 0) {
          display(DEFAULT_FILE_NAME);
          LOG.info("Waiting for updates");
          updates.take();
          continue;
        }

        TwitterBoardNotification notification = queue.take();
        while (queue.size() >= MAX_MESSAGES_IN_QUEUE) {
          LOG.info("Queue size is to big ({}). Skipping message {} ", queue.size(), notification);
          notification = queue.take();
        }

        long drawFinishTime = 0l;
        boolean redraw = true;
        boolean completed = false;
        while (!completed) {
          if (redraw) {
            LOG.info("Going to display {}", notification);
            int width = generateMessageImage(notification);
            LOG.info(
                "Width of this message {} and repeat count is {}",
                width,
                configuration.getRepeatCount());
            display(CUSTOM_FILE_NAME);
            drawFinishTime =
                System.currentTimeMillis()
                    + width
                        * configuration.getScrollSpeed()
                        * (Math.max(MIN_REPEAT_COUNT, configuration.getRepeatCount()));
            redraw = false;
          }
          long timeout = drawFinishTime - System.currentTimeMillis();
          if (timeout < 0) {
            completed = true;
            continue;
          }
          LOG.info("Will sleep for {} ms", timeout);
          TwitterBoardUpdate update = updates.poll(timeout, TimeUnit.MILLISECONDS);
          if (update != null) {
            LOG.info("Update detected: {}", update);
            if (update.isNewNotification()) {
              completed = configuration.getQueueSize() == 0;
            } else if (update.isNewConfiguration()) {
              LOG.info("Restarting message due to configuration update");
              redraw = update.isRedraw();
            }
          } else {
            completed = true;
          }
        }
      } catch (Exception e) {
        LOG.error("Failed to display message: {}", e.getMessage(), e);
      }
    }
  }
 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;
     }
   }
 }
  public void status() {
    logger.info("DyscoQueue:" + dyscosQueue.size());
    logger.info("totalRetrievedItems:" + totalRetrievedItems);

    if (dyscosQueue.size() > 500) {
      dyscosQueue.clear();
    }
  }
 private void checkOverflowSize() {
   if (memoryQueue.size() > overflowSize) {
     throw new JobLogException(
         "Memory Log size is "
             + memoryQueue.size()
             + " , please check the JobLogger is available");
   }
 }
 @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);
 }
 @Override
 public void run() {
   while (!shutdown) {
     LogEvent event;
     try {
       event = queue.take();
       if (event == SHUTDOWN) {
         shutdown = true;
         continue;
       }
     } catch (final InterruptedException ex) {
       break; // LOG4J2-830
     }
     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 LogEvent event = queue.take();
       if (event instanceof Log4jLogEvent) {
         final Log4jLogEvent logEvent = (Log4jLogEvent) event;
         logEvent.setEndOfBatch(queue.isEmpty());
         callAppenders(logEvent);
         count++;
       } else {
         ignored++;
         LOGGER.trace("Ignoring event of class {}", event.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);
 }
 @Override
 public void stop() {
   super.stop();
   LOGGER.trace("AsyncAppender stopping. Queue still has {} events.", queue.size());
   thread.shutdown();
   try {
     thread.join();
   } catch (final InterruptedException ex) {
     LOGGER.warn("Interrupted while stopping AsyncAppender {}", getName());
   }
   LOGGER.trace("AsyncAppender stopped. Queue has {} events.", queue.size());
 }
Beispiel #8
0
 /** remainingCapacity() always returns Integer.MAX_VALUE */
 public void testRemainingCapacity() {
   BlockingQueue q = populatedQueue(SIZE);
   for (int i = 0; i < SIZE; ++i) {
     assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
     assertEquals(SIZE - i, q.size());
     assertTrue(q.remove() instanceof PDelay);
   }
   for (int i = 0; i < SIZE; ++i) {
     assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
     assertEquals(i, q.size());
     assertTrue(q.add(new PDelay(i)));
   }
 }
  private MPacket searchQueue() {
    if (eventQueue.size() == 0) return null;

    MPacket tempQueue[] =
        Arrays.copyOf(eventQueue.toArray(), eventQueue.toArray().length, MPacket[].class);

    for (int i = 0; i < eventQueue.size(); i++) {
      if (tempQueue[i].sequenceNumber == clientSequenceNumber) {
        eventQueue.remove(tempQueue[i]);
        return tempQueue[i];
      }
    }

    return null;
  }
 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);
       }
     }
   }
 }
Beispiel #11
0
 /** Finalise communicate with socket server. */
 public void endClient() {
   Xlog.v(TAG, "enter endClient");
   mSendThread.interrupt();
   Xlog.v(TAG, "Queue remaining:" + mCommandQueue.size());
   closeClient();
   mCallBack = null;
 }
 public File getOneFile(BlockingQueue<File> queque) {
   System.out.println(" write thread id is " + this.getId() + " size " + queque.size());
   File f = null;
   File renamef = null;
   try {
     System.out.println(queque.size());
     f = queque.poll(1, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   if (f != null) {
     renamef = renameFile(f);
   }
   return renamef;
 }
  @Test
  public void testNotificationExecutor() throws Exception {
    ListeningExecutorService executor = SingletonHolder.getDefaultNotificationExecutor();
    ThreadPoolExecutor tpExecutor =
        (ThreadPoolExecutor)
            setAccessible(executor.getClass().getDeclaredField("delegate")).get(executor);
    BlockingQueue<Runnable> queue = tpExecutor.getQueue();

    for (int idx = 0; idx < 100; idx++) {
      final int idx2 = idx;
      logger.info("Adding {}\t{}\t{}", idx, queue.size(), tpExecutor.getActiveCount());
      executor.execute(
          new Runnable() {

            @Override
            public void run() {
              logger.info("in  {}", idx2);
              try {
                Thread.sleep(1000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              logger.info("out {}", idx2);
            }
          });
    }
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
  }
Beispiel #14
0
  private boolean init(int poolsize) {
    try {
      Class.forName("com.mysql.jdbc.Driver");
      url =
          "jdbc:mysql://"
              + _host
              + ":"
              + _port
              + "/"
              + _dbname
              + "?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&interactiveClient=true&"
              + "&user="******"&password="******"MysqlClient init pool success");
    } catch (Exception ex) {
      _logger.error(ex.getMessage(), ex);
      return false;
    }
    return true;
  }
 /**
  * Get number of requests waiting for execution.
  *
  * @return number of pending requests.
  */
 public int getQueueSize() {
   BlockingQueue<PrioritizedRequest> queue;
   synchronized (this) {
     queue = _queue;
   }
   return queue.size();
 }
  private void processMessages() {
    List<NetData.NetMessage> messages = Lists.newArrayListWithExpectedSize(queuedMessages.size());
    queuedMessages.drainTo(messages);

    for (NetData.NetMessage message : messages) {
      if (message.hasTime()) {
        time.updateTimeFromServer(message.getTime());
      }
      processBlockRegistrations(message);
      processReceivedChunks(message);
      processInvalidatedChunks(message);
      processBlockChanges(message);
      processBiomeChanges(message);
      processRemoveEntities(message);
      for (NetData.CreateEntityMessage createEntity : message.getCreateEntityList()) {
        createEntityMessage(createEntity);
      }
      for (NetData.UpdateEntityMessage updateEntity : message.getUpdateEntityList()) {
        updateEntity(updateEntity);
      }
      for (NetData.EventMessage event : message.getEventList()) {
        try {
          processEvent(event);
        } catch (RuntimeException e) {
          logger.error("Error processing server event", e);
        }
      }
    }
  }
 void checkEmpty(BlockingQueue q) {
   try {
     assertTrue(q.isEmpty());
     assertEquals(0, q.size());
     assertNull(q.peek());
     assertNull(q.poll());
     assertNull(q.poll(0, MILLISECONDS));
     assertEquals("[]", q.toString());
     assertTrue(Arrays.equals(q.toArray(), new Object[0]));
     assertFalse(q.iterator().hasNext());
     try {
       q.element();
       shouldThrow();
     } catch (NoSuchElementException success) {
     }
     try {
       q.iterator().next();
       shouldThrow();
     } catch (NoSuchElementException success) {
     }
     try {
       q.remove();
       shouldThrow();
     } catch (NoSuchElementException success) {
     }
   } catch (InterruptedException ie) {
     threadUnexpectedException(ie);
   }
 }
 private void registerWrite() throws CancelledKeyException {
   int size = oqueue.size();
   for (int i = 0; i < size; i++) {
     WebSocketImpl conn = oqueue.remove();
     conn.key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
   }
 }
 public synchronized int size() {
   int size = 0;
   for (BlockingQueue<Service> entry : pool.values()) {
     size += entry.size();
   }
   return size;
 }
  /** Issue #230 */
  @Test
  public void badRsmValueReturnsValidError() throws Exception {

    String after = "/user/[email protected]/posts/example.com|94948";
    Element rsm =
        new BaseElement(new QName("set", new Namespace("", "http://jabber.org/protocol/rsm")));
    rsm.addElement("after").setText(after);

    String stanza =
        "<iq type=\"get\" to=\"channels.buddycloud.org\" "
            + "from=\"[email protected]/resource\" id=\"id:1\">"
            + "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">"
            + "<user-items xmlns=\"http://buddycloud.org/v1\" parent-only=\"true\" "
            + "since=\"2000-01-01T00:00:00.000Z\"/>"
            + "<set xmlns=\"http://jabber.org/protocol/rsm\">"
            + "<max>10</max>"
            + "<after>/user/[email protected]/posts/example.com|94948</after>"
            + "</set>"
            + "</pubsub>"
            + "</iq>";
    IQ request = this.toIq(stanza);

    userItemsGet.process(element, jid, request, rsm);
    Assert.assertEquals(1, queue.size());

    IQ response = (IQ) queue.poll();
    PacketError errorPacket = response.getError();
    Assert.assertEquals("Could not parse the 'after' id: " + after, errorPacket.getText());
    Assert.assertEquals(PacketError.Condition.bad_request, errorPacket.getCondition());
    Assert.assertEquals(PacketError.Type.modify, errorPacket.getType());
  }
 public void expectNoEvents(ClientEvent.Type type) {
   switch (type) {
     case CLIENT_CACHE_ENTRY_CREATED:
       assertEquals(0, createdEvents.size());
       break;
     case CLIENT_CACHE_ENTRY_MODIFIED:
       assertEquals(0, modifiedEvents.size());
       break;
     case CLIENT_CACHE_ENTRY_REMOVED:
       assertEquals(0, removedEvents.size());
       break;
     case CLIENT_CACHE_ENTRY_EXPIRED:
       assertEquals(0, expiredEvents.size());
       break;
   }
 }
Beispiel #22
0
 protected BlockingQueue<Job> restoreQueue() {
   BlockingQueue<Job> q = new LinkedBlockingQueue<Job>();
   try {
     File f = mostCurrentFile();
     if (f != null) {
       ObjectInputStream in =
           new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
       try {
         q = (BlockingQueue<Job>) in.readObject();
         while (true) {
           Record r = (Record) in.readObject();
           if (r.add) {
             q.add(r.job);
           } else {
             q.poll();
           }
         }
       } finally {
         in.close();
       }
     }
   } catch (ClassNotFoundException cnfe) {
     LOG.error("Could not restore job queue from journal", cnfe);
   } catch (IOException ioe) {
     LOG.error("Could not restore job queue from journal", ioe);
   }
   try {
     deleteMarkedFiles();
   } catch (IOException ioe) {
     LOG.error("Error deleting stale log files", ioe);
   }
   System.out.println("Restored queue with " + q.size() + " jobs");
   System.out.println(q);
   return q;
 }
  @Test
  public void testBasicHandling() throws IOException, InterruptedException {
    final List<String> messages = new ArrayList<>();
    messages.add("1 syslog 20 this is message 1234\n");
    messages.add("2 syslog 22 this is message 456789\n");
    messages.add("3 syslog 21 this is message ABCDE\n");

    run(messages);
    Assert.assertEquals(messages.size(), events.size());

    boolean found1 = false;
    boolean found2 = false;
    boolean found3 = false;

    TestEvent event;
    while ((event = events.poll()) != null) {
      Map<String, String> metadata = event.metadata;
      Assert.assertTrue(metadata.containsKey(RELPMetadata.TXNR_KEY));

      final String txnr = metadata.get(RELPMetadata.TXNR_KEY);
      if (txnr.equals("1")) {
        found1 = true;
      } else if (txnr.equals("2")) {
        found2 = true;
      } else if (txnr.equals("3")) {
        found3 = true;
      }
    }

    Assert.assertTrue(found1);
    Assert.assertTrue(found2);
    Assert.assertTrue(found3);
  }
    private String buildMessage(Failure failure) {
      StringBuilder sb = new StringBuilder();
      sb.append("Failure #").append(failureList.size()).append(" ");
      if (failure.workerAddress != null) {
        sb.append(' ');
        sb.append(failure.workerAddress);
        sb.append(' ');
      } else if (failure.agentAddress != null) {
        sb.append(' ');
        sb.append(failure.agentAddress);
        sb.append(' ');
      }
      sb.append(failure.type);

      if (failure.cause != null) {
        String[] lines = failure.cause.split("\n");
        if (lines.length > 0) {
          sb.append("[");
          sb.append(lines[0]);
          sb.append("]");
        }
      }

      return sb.toString();
    }
Beispiel #25
0
  public static boolean tryWaitZkEventsCleaned(ZkClient zkclient) throws Exception {
    java.lang.reflect.Field field = getField(zkclient.getClass(), "_eventThread");
    field.setAccessible(true);
    Object eventThread = field.get(zkclient);
    // System.out.println("field: " + eventThread);

    java.lang.reflect.Field field2 = getField(eventThread.getClass(), "_events");
    field2.setAccessible(true);
    BlockingQueue queue = (BlockingQueue) field2.get(eventThread);
    // System.out.println("field2: " + queue + ", " + queue.size());

    if (queue == null) {
      LOG.error("fail to get event-queue from zkclient. skip waiting");
      return false;
    }

    for (int i = 0; i < 20; i++) {
      if (queue.size() == 0) {
        return true;
      }
      Thread.sleep(100);
      System.out.println("pending zk-events in queue: " + queue);
    }
    return false;
  }
  @Override
  protected long runQueuePass() throws InterruptedException {
    resetCounters();
    Future<?>[] futures = new Future[NUM_WORKERS];
    for (int i = 0; i < NUM_WORKERS; i++) {
      futures[i] = EXECUTOR.submit(queueWorkers[i]);
    }

    long start = System.currentTimeMillis();

    for (long i = 0; i < ITERATIONS; i++) {
      blockingQueue.put(Long.valueOf(i));
    }

    while (blockingQueue.size() > 0) {
      // spin while queue drains
    }

    for (int i = 0; i < NUM_WORKERS; i++) {
      queueWorkers[i].halt();
      futures[i].cancel(true);
    }

    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);

    assertEquals(ITERATIONS, sumCounters());

    return opsPerSecond;
  }
Beispiel #27
0
  /** 将数据提交到运营商 */
  private void startSubmitData() {
    while (isContinue) {
      List<SmQueue> tempList = new LinkedList<SmQueue>();

      try {
        // 每晚23:58分暂停操作
        if (ConstantUtils.isPause_23_58()) {
          Thread.sleep(10 * 60 * 1000);
        }

        if (queue.size() >= (smgFlowLimit / 2) || isDrainTo()) {
          int num = queue.drainTo(tempList, (smgFlowLimit / 2));
          lastDrainToTime = System.currentTimeMillis();

          if (num > 0) {
            submitDataThreadPool.execute(new SubmitChildThread(tempList, channel));
          }
        } else {
          Thread.sleep(1000);
        }
      } catch (InterruptedException e1) {

      }
    }
  }
  private JobPo get(String taskTrackerNodeGroup) {
    BlockingQueue<JobPo> jobPos = JOB_MAP.get(taskTrackerNodeGroup);
    if (jobPos == null) {
      jobPos =
          new PriorityBlockingQueue<JobPo>(
              step,
              new Comparator<JobPo>() {
                @Override
                public int compare(JobPo left, JobPo right) {
                  int compare = left.getTriggerTime().compareTo(right.getTriggerTime());
                  if (compare != 0) {
                    return compare;
                  }
                  compare = left.getPriority().compareTo(left.getPriority());
                  if (compare != 0) {
                    return compare;
                  }
                  return left.getGmtCreated().compareTo(right.getGmtCreated());
                }
              });
      BlockingQueue<JobPo> oldJobPos = JOB_MAP.putIfAbsent(taskTrackerNodeGroup, jobPos);
      if (oldJobPos != null) {
        jobPos = oldJobPos;
      }
    }

    if (jobPos.size() / step < factor) {
      // 触发加载的请求
      if (!LOAD_SIGNAL.contains(taskTrackerNodeGroup)) {
        LOAD_SIGNAL.add(taskTrackerNodeGroup);
      }
    }
    return jobPos.poll();
  }
Beispiel #29
0
  @Override
  public void observe(Event event) {
    if (event instanceof MoveSelectedEvent) {
      Move theMove = ((MoveSelectedEvent) event).getMove();
      if (theQueue.size() < 2) {
        theQueue.add(theMove);
      }
    } else if (event instanceof ServerNewGameStateEvent) {
      stateFromServer = ((ServerNewGameStateEvent) event).getState();
    } else if (event instanceof ServerCompletedMatchEvent) {
      theGUI.updateGameState(stateFromServer);

      List<Role> theRoles = getStateMachine().getRoles();
      List<Integer> theGoals = ((ServerCompletedMatchEvent) event).getGoals();

      StringBuilder finalMessage = new StringBuilder();
      finalMessage.append("Goals: ");
      for (int i = 0; i < theRoles.size(); i++) {
        finalMessage.append(theRoles.get(i));
        finalMessage.append(" = ");
        finalMessage.append(theGoals.get(i));
        if (i < theRoles.size() - 1) {
          finalMessage.append(", ");
        }
      }

      theGUI.showFinalMessage(finalMessage.toString());
    }
  }
Beispiel #30
0
  private void _eventloop() {
    for (; run | (awateTermination & (queue.size() > 0)); ) {
      try {

        final Touple touple = queue.take();
        if (touple == null) continue;
        Object result = null;
        Exception error = null;
        try {
          result = touple.callable.call();
          if (touple.onSuccess != null)
            try {
              touple.onSuccess.accept(result);
            } catch (Exception ex) {
              error = ex;
              if (touple.onError != null) shallow(touple.onError, error);
            }

        } catch (Exception ex) {
          error = ex;
          if (touple.onError != null) shallow(touple.onError, error);

        } finally {

          if (touple.onComplete != null) {
            if (result != null) shallow(touple.onComplete, result);
            else shallow(touple.onComplete, error);
          }
        }

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }