net.sf.xenqtt.message
Class AbstractBlockingCommand<T>

java.lang.Object
  extended by net.sf.xenqtt.message.AbstractBlockingCommand<T>
All Implemented Interfaces:
BlockingCommand<T>

public abstract class AbstractBlockingCommand<T>
extends Object
implements BlockingCommand<T>

Implementation of BlockingCommand that provides a simple extension point for command implementations.


Constructor Summary
AbstractBlockingCommand()
           
 
Method Summary
 T await()
          Waits indefinitely for the command to complete.
 T await(long timeout, TimeUnit unit)
          Waits for the specified amount of time for the command to complete
 void cancel()
          Cancels the command causes BlockingCommand.await() or BlockingCommand.await(long, TimeUnit) to throw an MqttCommandCancelledException.This should only be called by the same thread that calls BlockingCommand.execute().
 void complete()
          Called when the command is complete.
protected abstract  void doExecute()
          Extensions implement this method to execute the command
 void execute()
           Executes the command.
 void setFailureCause(Throwable cause)
          Sets the cause if the command fails.
 void setResult(T result)
          Sets the result of this command.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBlockingCommand

public AbstractBlockingCommand()
Method Detail

await

public final T await()
              throws MqttInterruptedException
Description copied from interface: BlockingCommand
Waits indefinitely for the command to complete.

Specified by:
await in interface BlockingCommand<T>
Returns:
The result of the command, if any.
Throws:
MqttInterruptedException - Thrown when the SyncMqttClient implementation is used and the calling thread is interrupted.
See Also:
BlockingCommand.await()

await

public final T await(long timeout,
                     TimeUnit unit)
              throws MqttInterruptedException,
                     MqttTimeoutException
Description copied from interface: BlockingCommand
Waits for the specified amount of time for the command to complete

Specified by:
await in interface BlockingCommand<T>
Parameters:
timeout - The maximum time to wait. If this is Long.MAX_VALUE is used then the unit arg is ignored and this is the same as invoking BlockingCommand.await().
unit - The time unit of the timeout argument
Returns:
The result of the command, if any.
Throws:
MqttInterruptedException - Thrown when the SyncMqttClient implementation is used and the calling thread is interrupted.
MqttTimeoutException - Thrown when the SyncMqttClient implementation is used and this method has blocked for approximately the configured timeout.
See Also:
BlockingCommand.await(long, java.util.concurrent.TimeUnit)

execute

public final void execute()
Description copied from interface: BlockingCommand

Executes the command. This is called by the thread responsible for processing the command, not the thread creating or waiting on the command. If an exception occurs BlockingCommand.setFailureCause(Throwable) is called with the thrown exception and BlockingCommand.complete() is invoked to complete the command. If the command returns a result use BlockingCommand.setResult(Object) to set it.

Specified by:
execute in interface BlockingCommand<T>
See Also:
BlockingCommand.execute()

setResult

public void setResult(T result)
Description copied from interface: BlockingCommand
Sets the result of this command. This is the value returned by BlockingCommand.await() and BlockingCommand.await(long, TimeUnit). This must be set before BlockingCommand.complete() is called. The last value set before BlockingCommand.complete() is called is returned from BlockingCommand.await() or BlockingCommand.await(long, TimeUnit). This should only be called by the same thread that calls BlockingCommand.complete().

Specified by:
setResult in interface BlockingCommand<T>
See Also:
BlockingCommand.setResult(java.lang.Object)

setFailureCause

public void setFailureCause(Throwable cause)
Description copied from interface: BlockingCommand
Sets the cause if the command fails. Null to clear any existing cause. If this is non-null then it is wrapped and re-thrown by BlockingCommand.await() or BlockingCommand.await(long, TimeUnit) after BlockingCommand.complete() is called. This should only be called by the same thread that calls BlockingCommand.execute().

Specified by:
setFailureCause in interface BlockingCommand<T>
See Also:
BlockingCommand.setFailureCause(java.lang.Throwable)

complete

public void complete()
Description copied from interface: BlockingCommand
Called when the command is complete. Causes BlockingCommand.await() or BlockingCommand.await(long, TimeUnit) to return.This should only be called by the same thread that calls BlockingCommand.execute().

Specified by:
complete in interface BlockingCommand<T>
See Also:
BlockingCommand.complete()

cancel

public void cancel()
Description copied from interface: BlockingCommand
Cancels the command causes BlockingCommand.await() or BlockingCommand.await(long, TimeUnit) to throw an MqttCommandCancelledException.This should only be called by the same thread that calls BlockingCommand.execute().

Specified by:
cancel in interface BlockingCommand<T>
See Also:
BlockingCommand.cancel()

doExecute

protected abstract void doExecute()
                           throws Throwable
Extensions implement this method to execute the command

Throws:
Exception - Any exception thrown by the command. This will be set as the failure cause using setFailureCause(Throwable).
Throwable


Copyright © 2013. All Rights Reserved.