Beispiel #1
0
  /**
   * This method looks up a MailboxAddress that has been registered with the system, and implicitly
   * opens the connection to the remote Mailbox. It also opens the reply address for the remote
   * mailbox, so it may send responses back to this Isolate.
   *
   * @param name the name of the Mailbox to lookup.
   * @param replyMailbox the address of a local mailbox that the remote isolate can send replies to.
   * @return an open address to the remote mailbox named <code>name</code>.
   * @throws NoSuchMailboxException if there is no mailbox named <code>name</code>.
   */
  public static MailboxAddress lookupMailbox(String name, Mailbox replyMailbox)
      throws NoSuchMailboxException {
    Mailbox box = VM.lookupMailbox(name);

    if (box == null) {
      throw new NoSuchMailboxException(name);
    }

    MailboxAddress replyAddress = new MailboxAddress(replyMailbox);
    MailboxAddress startingAddress = new MailboxAddress(box);
    MailboxAddress finalAddress = box.callHandleOpen(startingAddress, replyAddress);

    if (finalAddress == null) {
      // something fell apart in callHandleOpen
      throw new NoSuchMailboxException(name);
    }

    // record the address of the remote mailbox with this isolate:
    finalAddress.recordAddress(Isolate.currentIsolate(), replyAddress);

    // also, tell the isolate containing the remote mailbox about the
    // reply address that it will use to send replies back to this isolate.
    replyAddress.recordAddress(box.getOwner(), finalAddress);

    return finalAddress;
  }
Beispiel #2
0
  /**
   * Sends a close message to the corresponding Mailbox, explaining that no more messages will be
   * sent via this MailboxAddress. Also closes otherAddress if it's open, to eagerly break
   * connections between isolates. Redundant closes on a Mailbox are ignored.
   *
   * <p>Do NOT call while synchronized on "this". Can deadlock on otherAddress.close().
   *
   * @todo Rethink how to close otherAddress only semi-agressivly. Reaper thread, or close dead
   *     Mailbox addresses before attampting a hibernate.
   */
  public void close() {
    try {
      synchronized (this) {
        if (state != OPEN) {
          return;
        }
        Assert.that(mailbox != null, "mailbox is null");
        Assert.that(otherAddress != null, "otherAddress is null");
        Assert.that(owner != null, "owner is null");

        send(new AddressClosedEnvelope());
        closeLocalState();
      }

      if (otherAddress.isOpen()) {
        otherAddress.close();
      }
    } catch (AddressClosedException ex) {
      // send() will have closeLocalState() for us...
    }
  }