remotelock.java

来自「java版ace,java程序员值得一看」· Java 代码 · 共 544 行 · 第 1/2 页

JAVA
544
字号
   *@exception LockException remote access error occured
   */
  protected int makeRequest (int operationType,
			     int proxyType,
			     int requeuePosition,
			     TimeValue timeout)
    throws LockException, TimeoutException
  {
    request_.operationType (operationType);
    request_.proxyType (proxyType);
    request_.requeuePosition (requeuePosition);
    request_.useTimeout (timeout);

    sendRequest (request_);
    receiveReply (reply_);

    request_.arg (reply_.arg ());

    if (reply_.errno () == TokenReply.ETIME)
      throw new TimeoutException (timeout, "Remote Lock");

    return processErrno (reply_);
  }

  /**
   * Acquire ownership of the lock, blocking indefinitely if necessary.  
   * <P>
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException a remote error occured
   */
  public int acquire () throws LockException
  {
    return makeRequest (LockOperations.ACQUIRE, 0, 0);
  }

  /**
   * Acquire ownership of the lock by the given absolute time time-out.
   * A value of null for the timeout parameter results in a blocking
   * acquire.
   * A value of TimeValue.zero throws a TimeoutException if the
   * acquire would block.
   * <P>
   *@param timeout  absolute time by which the lock must be acquired
   *@return         appropriate Lock return value (AbstractLock.FAILURE, 
   *                AbstractLock.SUCCESS or AbstractLock.SLEEPHOOK)
   *@exception      LockException a remote error occured
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@see #tryAcquire
   */
  public int acquire (TimeValue timeout)
    throws LockException, TimeoutException
  {
    return makeRequest (LockOperations.ACQUIRE, 0, 0, timeout);
  }
    
  /**
   * Acquire a read lock, blocking indefinitely if necessary. 
   *
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException a remote error occured
   */
  public int acquireRead () throws LockException
  {
    return makeRequest (LockOperations.ACQUIRE,
			LockTypes.READ_LOCK_PROXY,
			0);
  }

  /**
   * Acquire a read lock by the given absolute time time-out.  
   *
   *@param timeout  absolute time by which the lock must be acquired
   *@return         appropriate lock return value (AbstractLock.FAILURE, 
   *                AbstractLock.SUCCESS or AbstractLock.SLEEPHOOK)
   *@exception      LockException a remote error occured
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@see #tryAcquireRead
   */
  public int acquireRead (TimeValue timeout) 
    throws LockException, TimeoutException
  {
    return makeRequest (LockOperations.ACQUIRE,
			LockTypes.READ_LOCK_PROXY,
			0,
			timeout);
  }

  /**
   * Acquire a write lock, blocking indefinitely if necessary. 
   *
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException a remote error occured
   */
  public int acquireWrite () 
    throws LockException
  {
    return makeRequest (LockOperations.ACQUIRE,
			LockTypes.WRITE_LOCK_PROXY,
			0);
  }

  /**
   * Acquire a write lock by the given absolute time time-out.  
   *
   *@param timeout  absolute time by which the lock must be acquired
   *@return         appropriate lock return value (AbstractLock.FAILURE, 
   *                AbstractLock.SUCCESS or AbstractLock.SLEEPHOOK)
   *@exception      LockException a remote error occured
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@see #tryAcquireWrite
   */
  public int acquireWrite (TimeValue timeout) 
    throws LockException, TimeoutException
  {
    return makeRequest (LockOperations.ACQUIRE,
			LockTypes.WRITE_LOCK_PROXY,
			0,
			timeout);
  }

  
  /**
   * Give up the lock to some number of waiting threads (if any), then 
   * reacquire, blocking indefinitely if necessary.
   * <P>
   * An optimized method that efficiently reacquires the token if no
   * other threads are waiting.  This is useful for situations where
   * you don't want to degrade the quality of service if there are
   * other threads waiting to get the token. 
   * <P>
   *@param     requeuePosition position in the waiters queue to insert
   *           this thread.  If this value is -1 and there are other
   *           threads waiting to obtain the token, this thread is queued
   *           at the end.  If this value is greater than -1, then it
   *           indicates how many entries to skip over before inserting
   *           our thread into the queue.  (For example, if it is 0,
   *           this thread is put at the front of the queue.)  If this 
   *           value is greater than the number of waiters, this thread is
   *           simply put at the end of the current waiters queue.
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException a remote error occured
   */
  public int renew (int requeuePosition) 
    throws LockException 
  {
    return makeRequest (LockOperations.RENEW,
			0,
			requeuePosition);
  }

  /**
   * Give up the lock to some waiting threads (if any), then reacquire
   * by the given absolute time time-out.
   * <P>
   * An optimized method that efficiently reacquires the token if no
   * other threads are waiting.  This is useful for situations where
   * you don't want to degrade the quality of service if there are
   * other threads waiting to get the token. 
   * <P>
   * A value of null for the timeout indicates a blocking renew.
   * <P>
   *@param     requeuePosition position in the waiters queue to insert
   *           this thread.  If this value is -1 and there are other
   *           threads waiting to obtain the token, this thread is queued
   *           at the end.  If this value is greater than -1, then it
   *           indicates how many entries to skip over before inserting
   *           our thread into the queue.  (For example, if it is 0,
   *           this thread is put at the front of the queue.)  If this 
   *           value is greater than the number of waiters, this thread is
   *           simply put at the end of the current waiters queue.
   * 
   *@param     timeout absolute time by which the lock must be reacquired
   *
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException a remote error occured
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   */
  public int renew (int requeuePosition, TimeValue timeout) 
    throws LockException, TimeoutException
  {
    return makeRequest (LockOperations.RENEW,
			0,
			requeuePosition,
			timeout);
  }

  /** 
   * Try to acquire the lock without blocking.  
   * <P>
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException a remote error occured
   */
  public int tryAcquire () throws LockException
  {
    return makeRequest (LockOperations.TRY_ACQUIRE, 0, 0);
  }

  /** 
   * Try to acquire a read lock without blocking.  
   * <P>
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException a remote error occured
   */
  public int tryAcquireRead () throws LockException
  {
    return makeRequest (LockOperations.TRY_ACQUIRE,
			LockTypes.READ_LOCK_PROXY,
			0);
  }

  /** 
   * Try to acquire a write lock without blocking.  
   * 
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException a remote error occured
   */
  public int tryAcquireWrite () throws LockException
  {
    return makeRequest (LockOperations.TRY_ACQUIRE,
			LockTypes.WRITE_LOCK_PROXY,
			0);
  }

  /** 
   * Release ownership of this lock.  
   * 
   *@return    appropriate AbstractLock return value 
   *           (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception LockException a remote error occured
   */
  public int release () throws LockException
  {
    return makeRequest (LockOperations.RELEASE, 0, 0);
  }

  /**
   * Closes the connection to the server (if it is still open).
   */
  protected void finalize () throws Throwable
  {
    close ();
  }

  /**
   * No-op implementation for the sleep hook (unused).
   */
  public void sleepHook () {}

  /** Status of whether this RemoteLock is connected to the server or not */
  protected boolean connected_ = false;

  /** Request object for transmissions to the server */
  protected TokenRequest request_;

  /** Reply object for receiving transmissions from the server */
  protected TokenReply reply_;

  /** Host name of the token service */
  protected String host_;

  /** Port number of the token service */
  protected int port_;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?