Package org.jboss.remoting3.spi
Class AbstractHandleableCloseable<T extends HandleableCloseable<T>>
- java.lang.Object
-
- org.jboss.remoting3.spi.AbstractHandleableCloseable<T>
-
- Type Parameters:
T
- the type of the closeable resource
- All Implemented Interfaces:
Closeable
,AutoCloseable
,HandleableCloseable<T>
public abstract class AbstractHandleableCloseable<T extends HandleableCloseable<T>> extends Object implements HandleableCloseable<T>
A basic implementation of a closeable resource. Use as a convenient base class for your closeable resources. Ensures that theclose()
method is idempotent; implements the registry of close handlers.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.remoting3.HandleableCloseable
HandleableCloseable.Key
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractHandleableCloseable(Executor executor)
Basic constructor.protected
AbstractHandleableCloseable(Executor executor, boolean autoClose)
Basic constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HandleableCloseable.Key
addCloseHandler(CloseHandler<? super T> handler)
Add a handler that will be called upon close.void
awaitClosed()
Wait for a resource close to complete.void
awaitClosedUninterruptibly()
Wait for a resource close to complete.protected void
checkOpen()
Check if open, throwing an exception if it is not.void
close()
Close this resource.protected void
closeAction()
Called exactly once when theclose()
method is invoked; the actual close operation should take place here.void
closeAsync()
Asynchronously close this resource.protected void
closeComplete()
Call when close is complete.protected void
closeFailed(IOException cause)
Call if an async close has failed.protected void
finalize()
Finalize this closeable instance.protected Executor
getExecutor()
Get the executor to use for handler invocation.boolean
isOpen()
Read the status of this resource.
-
-
-
Constructor Detail
-
AbstractHandleableCloseable
protected AbstractHandleableCloseable(Executor executor)
Basic constructor.- Parameters:
executor
- the executor used to execute the close notification handlers
-
AbstractHandleableCloseable
protected AbstractHandleableCloseable(Executor executor, boolean autoClose)
Basic constructor.- Parameters:
executor
- the executor used to execute the close notification handlersautoClose
-true
if this instance should automatically close on finalize
-
-
Method Detail
-
isOpen
public boolean isOpen()
Read the status of this resource. This is just a snapshot in time; there is no guarantee that the resource will remain open for any amount of time, even if this method returnstrue
.- Specified by:
isOpen
in interfaceHandleableCloseable<T extends HandleableCloseable<T>>
- Returns:
true
if the resource is still open
-
closeAction
protected void closeAction() throws IOException
Called exactly once when theclose()
method is invoked; the actual close operation should take place here. This method must callcloseComplete()
, directly or indirectly, for the close operation to finish (it may happen in another thread but it must happen). This method should not expect thecloseComplete()
call to be made from another thread from the same thread pool that may causeclose()
. As close will block, this can result in situations where all threads in the pool are blocked onclose()
method calls, which means thecloseComplete()
will never be run.- Throws:
RemotingException
- if the close failedIOException
-
close
public void close() throws IOException
Close this resource. Call any registered close handlers. Calling this method more than once will not have any additional effect.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceHandleableCloseable<T extends HandleableCloseable<T>>
- Throws:
IOException
- if the close failed
-
closeComplete
protected void closeComplete()
Call when close is complete.
-
closeFailed
protected void closeFailed(IOException cause)
Call if an async close has failed.- Parameters:
cause
- the failure cause
-
awaitClosed
public void awaitClosed() throws InterruptedException
Wait for a resource close to complete.- Specified by:
awaitClosed
in interfaceHandleableCloseable<T extends HandleableCloseable<T>>
- Throws:
InterruptedException
- if the operation is interrupted
-
awaitClosedUninterruptibly
public void awaitClosedUninterruptibly()
Wait for a resource close to complete.- Specified by:
awaitClosedUninterruptibly
in interfaceHandleableCloseable<T extends HandleableCloseable<T>>
-
closeAsync
public void closeAsync()
Asynchronously close this resource. Returns immediately.- Specified by:
closeAsync
in interfaceHandleableCloseable<T extends HandleableCloseable<T>>
-
addCloseHandler
public HandleableCloseable.Key addCloseHandler(CloseHandler<? super T> handler)
Add a handler that will be called upon close. If the resource is already closed, the handler will be called immediately.- Specified by:
addCloseHandler
in interfaceHandleableCloseable<T extends HandleableCloseable<T>>
- Parameters:
handler
- the close handler- Returns:
- a key which may be used to later remove this handler
-
getExecutor
protected Executor getExecutor()
Get the executor to use for handler invocation.- Returns:
- the executor
-
finalize
protected void finalize() throws Throwable
Finalize this closeable instance. If the instance hasn't been closed, it is closed and a warning is logged.
-
checkOpen
protected void checkOpen() throws NotOpenException
Check if open, throwing an exception if it is not.- Throws:
NotOpenException
- if not open
-
-