コード例 #1
0
    public Reservation reserveNext() {
      int i = 0;
      int start;

      synchronized (m_resourcePools) {
        start = ++m_next;
      }

      while (true) {
        final Reservation reservation =
            m_resourcePools[(start + i) % m_resourcePools.length].reserveNext();

        if (!reservation.isSentinel() || i == m_resourcePools.length - 1) {
          return reservation;
        }

        ++i;
      }
    }
コード例 #2
0
 public void shutdown() {
   // We rely on our ResponseSender's single call to send to ensure we don't
   // free the reservation multiple times.
   m_reservation.free();
 }
コード例 #3
0
    public void interruptibleRun() {
      try {
        // Did we do some work on the last pass?
        boolean idle = false;

        while (true) {
          final Reservation reservation = m_sockets.reserveNext();
          boolean holdReservation = false;

          try {
            if (reservation.isSentinel()) {
              if (idle) {
                Thread.sleep(m_delay);
              }

              idle = true;
            } else {
              final SocketWrapper socketWrapper = (SocketWrapper) reservation.getResource();

              // We don't need to synchronise access to the SocketWrapper
              // stream; access is protected through the socket set and only we
              // hold the reservation.
              final InputStream inputStream = socketWrapper.getInputStream();

              if (inputStream.available() > 0) {

                idle = false;

                final ObjectInputStream objectStream = new ObjectInputStream(inputStream);

                final Message message = (Message) objectStream.readObject();

                if (message instanceof CloseCommunicationMessage) {
                  reservation.close();
                  continue;
                }

                if (message instanceof MessageRequiringResponse) {

                  final MessageRequiringResponse messageRequiringResponse =
                      (MessageRequiringResponse) message;

                  messageRequiringResponse.setResponder(
                      new SenderWithReservation(
                          new StreamSender(socketWrapper.getOutputStream()), reservation));

                  m_messageQueue.queue(message);

                  // Whatever handles the MessageExpectingResponse takes
                  // responsibility for the reservation.
                  holdReservation = true;
                } else {
                  m_messageQueue.queue(message);
                }
              }
            }
          } catch (IOException e) {
            reservation.close();
            UncheckedInterruptedException.ioException(e);
            m_messageQueue.queue(e);
          } catch (ClassNotFoundException e) {
            reservation.close();
            m_messageQueue.queue(e);
          } catch (InterruptedException e) {
            reservation.close();
            throw new UncheckedInterruptedException(e);
          } finally {
            if (!holdReservation) {
              reservation.free();
            }
          }
        }
      } catch (ThreadSafeQueue.ShutdownException e) {
        // We've been shutdown, exit this thread.
      } finally {
        // Ensure we're shutdown.
        shutdown();
      }
    }