/**
   * After deserialization hook. Puts all vehicles on separate threads so everything will work as it
   * should
   *
   * @param in Fired on readobject
   * @throws IOException
   * @throws ClassNotFoundException
   */
  private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {

    }

    for (int i = 0; i < getVehicles().size(); i++) {
      Thread t = new Thread(getVehicles().get(i));
      t.start();
    }

    for (int i = 0; i < getIntersections().size(); i++) {
      Thread t = new Thread(getIntersections().get(i));
      t.start();
    }

    Passenger p = passengers.poll();
    while (p != null) {
      Thread t = new Thread(p);
      t.start();

      p = passengers.poll();
    }
  }
  /**
   * Subscribes listener to the list of objects notified when World Changes
   *
   * @param listener IChangesListener to be added to subscribents list
   */
  public void addWorldChangesListener(IChangesListener listener) {
    if (listeners == null) listeners = new LinkedBlockingQueue<IChangesListener>();

    if (!listeners.contains(listener)) {
      listeners.add(listener);
    }
  }
 /** {@inheritDoc} */
 public ScheduledTask getNextTask(boolean wait) throws InterruptedException {
   ScheduledTask task = queue.poll();
   if ((task != null) || (!wait)) {
     return task;
   }
   return queue.take();
 }
示例#4
0
 @Override
 public Boolean call() {
   synchronized (isStarted) {
     isStarted.set(true);
     isStarted.notify();
   }
   try {
     while (true) {
       flushPage = null;
       try {
         flushPage = flushQ.take();
         if (flushPage == POISON_PILL || terminateFlag.get()) {
           return true;
         }
       } catch (InterruptedException e) {
         if (flushPage == null) {
           continue;
         }
       }
       flushPage.flush();
       emptyQ.offer(flushPage);
     }
   } catch (Exception e) {
     if (LOGGER.isLoggable(Level.INFO)) {
       LOGGER.info("-------------------------------------------------------------------------");
       LOGGER.info("LogFlusher is terminating abnormally. System is in unusalbe state.");
       LOGGER.info("-------------------------------------------------------------------------");
     }
     e.printStackTrace();
     throw e;
   }
 }
  @Override
  public IQ query(Component component, IQ packet, long timeout) throws ComponentException {
    final LinkedBlockingQueue<IQ> answer = new LinkedBlockingQueue<>(8);
    XMPPServer.getInstance()
        .getIQRouter()
        .addIQResultListener(
            packet.getID(),
            new IQResultListener() {
              @Override
              public void receivedAnswer(IQ packet) {
                answer.offer(packet);
              }

              @Override
              public void answerTimeout(String packetId) {
                Log.warn(
                    "An answer to a previously sent IQ stanza was never received. Packet id: "
                        + packetId);
              }
            });
    sendPacket(component, packet);
    IQ reply = null;
    try {
      reply = answer.poll(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      // Ignore
    }
    return reply;
  }
 /** Shutdown the processor. */
 public void shutdown() {
   LOG.info("Shutting down");
   finished = true;
   queuedRequests.clear();
   queuedRequests.add(Request.requestOfDeath);
   nextProcessor.shutdown();
 }
  @Override
  public void tempFileAnalysis(
      LinkedBlockingQueue<String> urlQ, LinkedBlockingQueue<DataModle> dmQ) {
    logger.info("now useful URL queue size:" + urlQ.size());

    DataModle dm = null;
    String url = "";
    try {
      url = urlQ.take();
    } catch (InterruptedException e2) {
      logger.warn("url queue is empty,can not take a dm");
      return;
    }

    try {
      dm = elementFilter(url);
    } catch (IOException e1) {
      logger.error("can not generate a dm");
      return;
    }

    try {
      dmQ.put(dm);
    } catch (InterruptedException e) {
      logger.warn("unfinishDmQ is full,can not put a dm");
    }
  }
示例#8
0
  public static void main(String[] args) {
    long time = System.currentTimeMillis();
    queue = new LinkedBlockingQueue<>();
    File file = new File("text.txt");

    try {
      reader = new BufferedReader(new FileReader(file));
      String line = "";
      line = reader.readLine();
      firstLetter = line.charAt(0) + "";
      queue.add(new Task(line));
      while ((line = reader.readLine()) != null) {
        queue.add(new Task(line));
      }

      Worker w = new Worker();
      w.start();

      try {
        w.join();
      } catch (InterruptedException e1) {
        e1.printStackTrace();
      }

      System.out.println("Number of first letters in file: " + letterCounter);
      System.out.println(System.currentTimeMillis() - time + " [ms]");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#9
0
  @Override
  public ErisMessage receive(long timeout)
      throws org.eris.messaging.TransportException, org.eris.messaging.ReceiverException,
          org.eris.messaging.TimeoutException {
    checkClosed();
    if (_creditMode == CreditMode.EXPLICT && _capacity == 0) {
      throw new org.eris.messaging.ReceiverException(
          "Since CreditMode is EXPLICIT, you need to explicity set the capacity before calling receive");
    }
    issuePreReceiveCredit();
    ErisMessage msg = null;
    try {
      if (timeout == 0) {
        msg = _queue.take();
      } else {
        msg = _queue.poll(timeout, TimeUnit.MILLISECONDS);
      }
    } catch (InterruptedException e) {
      checkClosed();
    }

    if (msg != null) {
      issuePostReceiveCredit();
      return msg;
    } else {
      checkClosed();
      if (timeout == 0) {
        throw new org.eris.messaging.ReceiverException("Receive operation was interrupted");
      } else {
        throw new org.eris.messaging.TimeoutException(
            "Timeout waiting for message to be available");
      }
    }
  }
示例#10
0
  @Test
  public void testReceiveMessage() {

    ESFEventMessage msg1 = new ESFEventMessage();
    ESFEventMessage msg2 = new ESFEventMessage();

    messageQueue.add(msg1);
    messageQueue.add(msg2);

    messageReceiver.setMessageQueue(messageQueue);
    messageProcessor.setMessageQueue(messageQueue);

    ESFEventMessage msg3 = new ESFEventMessage();
    ESFFetchMissedEventsResponse msg4 = new ESFFetchMissedEventsResponse();
    ESFSubscriptionResponse msg5 = new ESFSubscriptionResponse();
    ESFUnsubscriptionResponse msg6 = new ESFUnsubscriptionResponse();

    messageQueue.add(msg3);
    // messageQueue.add(msg4);
    // messageQueue.add(msg5);
    // messageQueue.add(msg6);

    messageReceiver.receivedMessage(msg3, comm);
    messageReceiver.receivedMessage(msg4, comm);
    messageReceiver.receivedMessage(msg5, comm);
    messageReceiver.receivedMessage(msg6, comm);
  }
 @Test
 public void testAddNew() throws Exception {
   RpcStub handler = makeHandler();
   MultiplexPoolHandler han = new MultiplexPoolHandler(Collections.singleton(handler), 2, 3);
   han.setWaitTimeout(1);
   travelPool(han);
   ArrayList<RpcStub> list = new ArrayList<RpcStub>();
   list.add(makeHandler());
   list.add(makeHandler());
   list.add(makeHandler());
   han.addNew(list);
   travelPool(han);
   ArrayList<RpcStub> backupHandlers = FieldUtil.getValue(han, "backupHandlers");
   assertEquals(4, backupHandlers.size());
   LinkedBlockingQueue<RpcStub> readyQueue = FieldUtil.getValue(han, "readyQueue");
   assertEquals(4, readyQueue.size());
   RpcStub h1 = han.take();
   RpcStub h2 = han.take();
   RpcStub h3 = han.take();
   RpcStub h4 = han.take();
   travelPool(han);
   han.offer(h1);
   han.offer(h2);
   han.offer(h3);
   han.offer(h4);
   travelPool(han);
   ArrayList<RpcStub> list2 = new ArrayList<RpcStub>();
   list2.add(makeHandler());
   han.addNew(list2);
   assertEquals(5, backupHandlers.size());
   assertEquals(10, readyQueue.size());
   han.destroy();
 }
  @Override
  public void run() {
    Thread videoDecoder = new Thread(new VideoDecoder(this));
    videoDecoder.start();
    long lastFrameWrite = System.nanoTime() - NANOSECONDS_PER_FRAME;
    long timeNow = 0;

    try {
      while ((!videoDecoded || !imgBuffer.isEmpty()) && frame.isVisible()) {
        timeNow = System.nanoTime();
        if (timeNow - lastFrameWrite >= NANOSECONDS_PER_FRAME) {
          lastFrameWrite = timeNow;
          BufferedImage img = imgBuffer.take();
          g.drawImage(img, 0, 0, frame.getWidth(), frame.getHeight(), null);

          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          ImageIO.write(img, "jpg", bos);
          proxy.receiveImage(ByteBuffer.wrap(bos.toByteArray()));
        }
      }
    } catch (IOException | InterruptedException e) {
      // other user has disconnected, stop sending video
    }

    imgBuffer.clear();
    frame.setVisible(false);
    try {
      proxy.setFrameVisible(true, false);
    } catch (IOException e) {
      System.err.println("The other user has disconnected, stopping video.");
    }
  }
示例#13
0
  /**
   * The put request will be buffered by its corresponding buffer queue. And the put request will be
   * retried before dropping the request. Return false if the queue is already full.
   *
   * @return true if the request can be accepted by its corresponding buffer queue.
   */
  public boolean put(final TableName tableName, final Put put, int maxAttempts) {
    if (maxAttempts <= 0) {
      return false;
    }

    try {
      HTable.validatePut(put, maxKeyValueSize);
      // Allow mocking to get at the connection, but don't expose the connection to users.
      ClusterConnection conn = (ClusterConnection) getConnection();
      // AsyncProcess in the FlushWorker should take care of refreshing the location cache
      // as necessary. We shouldn't have to do that here.
      HRegionLocation loc = conn.getRegionLocation(tableName, put.getRow(), false);
      if (loc != null) {
        // Add the put pair into its corresponding queue.
        LinkedBlockingQueue<PutStatus> queue = getQueue(loc);

        // Generate a MultiPutStatus object and offer it into the queue
        PutStatus s = new PutStatus(loc.getRegionInfo(), put, maxAttempts);

        return queue.offer(s);
      }
    } catch (IOException e) {
      LOG.debug("Cannot process the put " + put, e);
    }
    return false;
  }
  protected void checkExceptionHandling(
      final LinkedBlockingQueue<Throwable> unexpectedExceptions,
      ExecutorTaskAgent agent,
      Future<?> future,
      Class<? extends Exception> expectedKlass,
      boolean isExpected)
      throws InterruptedException {

    try {
      future.get();
    } catch (ExecutionException ce) {
      assertTrue(expectedKlass.isInstance(ce.getCause()));
      // ok
    }
    agent.awaitPendingTasks();

    if (expectedKlass == null || isExpected) {
      assertTrue(unexpectedExceptions.size() == 0);
      return;
    } else {
      assertTrue(unexpectedExceptions.size() == 1);
      Throwable removed = unexpectedExceptions.remove();
      assertTrue(expectedKlass.isInstance(removed));
    }
  }
示例#15
0
 /** Get an event from the queue. */
 synchronized AppletEvent getNextEvent() throws InterruptedException {
   while (queue == null || queue.isEmpty()) {
     wait();
   }
   int eventId = queue.take();
   return new AppletEvent(this, eventId, null);
 }
示例#16
0
  private boolean schedule() {
    try {
      /* hash com os workers em processamento. Indexado pela tarefa*/
      workingOn = new Hashtable();
      currentStatus = Phase.MAP;
      while (!currentStatus.equals(Phase.ERROR)) {

        Worker worker = (Worker) workerQueue.take();
        Task task = (Task) taskQueue.take();
        TaskStatus op = task.getStatus();

        /* termina escalonamento se encontra flag de fim*/
        if (op.value() == TaskStatus._END) {
          break;
        }
        workingOn.put(task, worker);
        worker.execute(channel, task);
      }

      if (currentStatus.equals(Phase.ERROR)) {
        return false;
      }

      return true;
    } catch (Exception e) {
      exception = LogError.getStackTrace(e);
      reporter.report(0, "MasterServant::schedule - Escalonamento interrompido. \n" + exception);
      return false;
    }
  }
  @Override
  public Iterable<Message> getNewMessages(String remoteNodeId) throws RemoteException {
    logger.debug("Requesting the new messages from node [" + remoteNodeId + "]");

    LinkedBlockingQueue<Message> newMessages = new LinkedBlockingQueue<Message>();

    for (MessageContainer messageContainer : broadcastedMessagesQueue) {
      if (!synchronizationTime.containsKey(remoteNodeId)
          || synchronizationTime.get(remoteNodeId) < messageContainer.getTimeStamp()) {
        newMessages.add(messageContainer.getMessage());
        logger.debug(
            "Message [" + messageContainer.getMessage() + "] added to the new messages to send");
      }
    }

    // set the synchronization time of the node
    long timeStamp = new DateTime().getMillis();
    if (synchronizationTime.containsKey(remoteNodeId)) {
      synchronizationTime.replace(remoteNodeId, timeStamp);
      logger.debug("Synchronization time replaced by new one");
    } else {
      synchronizationTime.put(remoteNodeId, timeStamp);
      logger.debug("Synchronization time setted by first time");
    }
    logger.debug("Node [" + remoteNodeId + "] synchronized at [" + timeStamp + "]");

    return newMessages;
  }
示例#18
0
  @Test
  public void testBackpressure() throws Exception {
    createAndRegisterStats();
    MockStatsSource.delay = 20;

    /*
     * Generate a bunch of requests, should get backpressure on some of them
     */
    for (int ii = 0; ii < 30; ii++) {
      m_mvoltdb
          .getStatsAgent()
          .performOpsAction(m_mockConnection, 32, OpsSelector.STATISTICS, subselect("DR", 0));
    }

    boolean hadBackpressure = false;
    for (int ii = 0; ii < 30; ii++) {
      ClientResponseImpl response = responses.take();

      if (response.getStatus() == ClientResponse.GRACEFUL_FAILURE) {
        assertTrue("Too many pending stat requests".equals(response.getStatusString()));
        hadBackpressure = true;
      }
    }
    assertTrue(hadBackpressure);

    /*
     * Now having recieved all responses, it should be possible to collect the stats
     */
    m_mvoltdb
        .getStatsAgent()
        .performOpsAction(m_mockConnection, 32, OpsSelector.STATISTICS, subselect("DR", 0));
    ClientResponseImpl response = responses.take();
    verifyResults(response);
  }
  public static void event(Call call, String event, Object data) {
    if (call == null) {
      Log.i(TAG, "Non-call EVENT: %s, %s", event, data);
      return;
    }
    synchronized (mCallEventRecords) {
      if (!mCallEventRecordMap.containsKey(call)) {
        // First remove the oldest entry if no new ones exist.
        if (mCallEventRecords.remainingCapacity() == 0) {
          CallEventRecord record = mCallEventRecords.poll();
          if (record != null) {
            mCallEventRecordMap.remove(record.getCall());
          }
        }

        // Now add a new entry
        CallEventRecord newRecord = new CallEventRecord(call);
        mCallEventRecords.add(newRecord);
        mCallEventRecordMap.put(call, newRecord);
      }

      CallEventRecord record = mCallEventRecordMap.get(call);
      record.addEvent(event, data);
    }
  }
  @Test
  @Transactional
  @Rollback
  public void verify() throws Exception {
    final LinkedBlockingQueue<HttpRequest> requests = new LinkedBlockingQueue<HttpRequest>();

    server.register(
        "/*",
        new HttpRequestHandler() {
          @Override
          public void handle(HttpRequest request, HttpResponse response, HttpContext context)
              throws HttpException, IOException {
            requests.add(request);

            response.setEntity(
                new StringEntity(
                    getQueryParamValue(request.getRequestLine().getUri(), "hub.challenge")));
          }
        });

    subscriber.verify(SubscriptionMode.SUBSCRIBE);

    Assert.assertEquals(1, requests.size());

    HttpRequest actualRequest = requests.poll();
    String requestUri = actualRequest.getRequestLine().getUri();
    Assert.assertEquals("subscribe", getQueryParamValue(requestUri, "hub.mode"));
    Assert.assertEquals(
        subscriber.getTopic().toString(),
        URLDecoder.decode(getQueryParamValue(requestUri, "hub.topic"), "UTF-8"));
    Assert.assertNotNull(getQueryParamValue(requestUri, "hub.challenge"));
    Assert.assertEquals("123", getQueryParamValue(requestUri, "hub.lease_seconds"));
    Assert.assertEquals(
        subscriber.getVerifyToken(), getQueryParamValue(requestUri, "hub.verify_token"));
  }
示例#21
0
  public void run() {
    while (true) {
      try {
        if (locker.isEmpty()) {
          logger.debug(
              "Waiting(locked) for requests or data items, Number of total listeners: "
                  + listeners.size());
          locker.put(locker.take());
          logger.debug(
              "Lock released, trying to find interest listeners (total listeners:"
                  + listeners.size()
                  + ")");
        }
      } catch (InterruptedException e) {
        logger.error(e.getMessage(), e);
      }

      for (Entry<DistributionRequest, DataEnumerator> item : candidateListeners.entrySet()) {
        boolean success = flushStreamElement(item.getValue(), item.getKey());
        if (success == false) removeListener(item.getKey());
        else {
          if (!item.getValue().hasMoreElements()) {
            removeListenerFromCandidates(item.getKey());
            // As we are limiting the number of elements returned by the JDBC driver
            // we consume the eventual remaining items.
            consume(null, item.getKey().getVSensorConfig());
          }
        }
      }
    }
  }
  public void send(Message message) {
    try {
      // force keyframe? ditch all the other messages on the queue
      // since this message will overwrite everything
      if (message.isBlockMessage() && ((BlockMessage) message).getForceKeyFrame()) {
        blockDataQ.clear();
        notifyQueueListener(0);
      }
      // stick message on queue
      blockDataQ.put(message);

      // if the queue is backed up, purge it of duplicate blocks
      int qSizeInBlocks = getQueueSizeInBlocks();
      if (qSizeInBlocks > ScreenShareInfo.MAX_QUEUE_SIZE_FOR_PAUSE) {
        if (ScreenShareInfo.getPurgeBackedUpQueue()) {
          purgeBlockDataQ();
        }
        notifyQueueListener(qSizeInBlocks);
      }

      // if we're being slow, notify also
      if (ScreenShareInfo.adjustPauseFromQueueSize && perfStats.isSlow()) {
        notifyPerformanceListener(true);
      }

    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  @Override
  public void postFrame(double timePerFrame) {
    super.postFrame(timePerFrame);

    while (!nodesToAddPostFrame.isEmpty()) {
      getGraphics3DAdapter().getRenderer().getZUpNode().attachChild(nodesToAddPostFrame.poll());
    }
  }
示例#24
0
 public void runMatcher() {
   System.out.println("Matcher => runMatcher() metodu...");
   if (ObjectiveQ == null) System.out.println("incomingQ null...");
   if (ObjectiveQ.isEmpty()) {
     System.out.println("incomingQ bos...");
   }
   Objective incomingObjective = (Objective) ObjectiveQ.poll();
   Match(incomingObjective);
 }
 @Override
 public void put(V value) {
   if (value == null) {
     throw new NullPointerException();
   }
   while (!queue.offer(value)) {
     queue.poll();
   }
 }
  /**
   * Called in case of emergency after deserialization. Deserialization doesn't save listeners
   * because they are transient because listeners are mostly windows which cannot be serialized.
   * After Deserialization we have to find instances of classes implementing IChangesListener and
   * reconstruct listeners list once again to get everything working smooth.
   */
  public synchronized void recoverChangesListeners() {
    if (listeners == null || listeners.size() < 2) {
      listeners = new java.util.concurrent.LinkedBlockingQueue<IChangesListener>();

      listeners.add(VehicleInspector.getInstance());
      listeners.add(BuildingInspector.getInstance());
      listeners.add(DrawPanel.getInstance());
    }
  }
 public synchronized void debug() {
   GnuBackgammon.out.println("===> EVENTI IN CODA: " + queue.size());
   GnuBackgammon.out.print("   ===> ");
   Iterator<Evt> itr = queue.iterator();
   while (itr.hasNext()) {
     Evt element = itr.next();
     GnuBackgammon.out.print("  " + element.e);
   }
   GnuBackgammon.out.println(" ");
 }
 /**
  * Returns the next available packet. The method call will block (not return) until a packet is
  * available or the <tt>timeout</tt> has elapased. If the timeout elapses without a result (or the
  * thread is interrupted), <tt>null</tt> will be returned.
  *
  * @param timeout the amount of time in milliseconds to wait for the next packet.
  * @throws IllegalStateException if this collector is canceled
  * @return the next available packet.
  */
 public Packet nextResult(long timeout) {
   if (canceled && resultQueue.isEmpty())
     throw new IllegalStateException("Canceled packet collector");
   try {
     return resultQueue.poll(timeout, TimeUnit.MILLISECONDS);
   } catch (InterruptedException ie) {
     // Ignore
     return null;
   }
 }
  public void replace(FilterBypass fb, int offset, int length, String str, AttributeSet a)
      throws BadLocationException {

    /* Queue a copy of the event and then modify the text */
    if (length > 0) {
      eventHistory.add(new TextRemoveEvent(offset, length));
    }
    eventHistory.add(new TextInsertEvent(offset, str));
    super.replace(fb, offset, length, str, a);
  }
示例#30
0
  @Override
  public void run() {
    while (!stop) {
      // 业务逻辑
      try {
        // 这里做流量控制
        TpsTool.limitTPS();
        // 取包发送
        APackage pack = this.doSubmit();
        if (pack == null) {
          pack = buffer.poll();
        }
        if (pack == null) {
          // 确认是否需要发送链路检测包
          long curTime = System.currentTimeMillis();
          if (curTime > (lastActiveTime + timeout)) {
            if (channel.isLogin()) {
              // 链路已经空闲超过60s,且连接还未断开,需要发送UnbindMessage包
              log.info("链路空闲超过" + timeout + "ms,发送UnbindMessage包断开连接");
              channel.unbind();
            }
          }
        }
        if (pack == null) {
          continue;
        }
        // 设置发送时间
        lastActiveTime = System.currentTimeMillis();
        try {
          boolean flag = sendPacket(pack);
          if (!flag) {
            buffer.offer(pack);
          }
        } catch (IOException ex) {
          // 发送失败重发
          log.error(null, ex);
          if (pack.getTryTimes() < retryTimes) {
            buffer.offer(pack);
          } else {
            // 记录丢失的包到文件中
            discard.info(
                "丢失包,重试多次失败"
                    + pack.getHead().getCommandIdString()
                    + ",字节码:"
                    + Hex.rhex(pack.getBytes()));
          }
        }

      } catch (Exception ex) {
        log.error(null, ex);
        // 发生异常,强制关闭连接
        channel.close();
      }
    }
  }