com.sun.grizzly
Interface Context

All Superinterfaces:
AttributeHolder, Copyable
All Known Implementing Classes:
NIOContext

public interface Context
extends AttributeHolder, Copyable

This interface is used to share information between the Grizzly Framework classes and ProtocolFilter implementation. Since Context is a pooled resource Controller#pollContext(java.nio.channels.SelectionKey) transactions using Context outside its ProtocolChain must invoke incrementRefCount() and Controller.returnContext(com.sun.grizzly.Context) to keep its pooling intact.

Author:
Jeanfrancois Arcand

Nested Class Summary
static class Context.AttributeScope
           
static class Context.KeyRegistrationState
          A SelectionKey's registration state.
static class Context.OpType
          The list of possible SelectionKey.OP_XXXX.
 
Field Summary
static String THROWABLE
          Constant 'throwable' String
 
Method Summary
 void cancel()
          Cancel a suspend()ed Context.
 int decrementRefCount()
          Decrements the reference count of this Context.
 void execute()
          Deprecated.  
 void execute(ContextTask contextTask)
          Execute this Context using the Controller's thread pool
 void execute(ContextTask contextTask, boolean runInSeparateThread)
          Execute this Context using either Controller's thread pool or current thread
 AsyncQueueReader getAsyncQueueReader()
          Return the
 AsyncQueueWritable getAsyncQueueWritable()
          Returns AsyncQueueWritable, assciated with the current Context.
 AttributeHolder getAttributeHolderByScope(Context.AttributeScope scope)
          Return AttributeHolder, which corresponds to the given Context.AttributeScope>
 Map<String,Object> getAttributes()
          Return a Map of attribute name/value pairs.
 Controller getController()
          Return the current Controller.
 Context.OpType getCurrentOpType()
          Get the current SelectionKey interest ops this instance is executing.
 IOEvent getIOEvent()
          Return the current IOEvent associated with this instance.
 Context.KeyRegistrationState getKeyRegistrationState()
          Return SelectionKey's next registration state, or null if not supported.
 Controller.Protocol getProtocol()
          Return the current Controller#Protocol this instance is executing.
 ProtocolChain getProtocolChain()
          Return ProtocolChain executed by this instance.
 SelectionKey getSelectionKey()
          Return the current SelectionKey or null if {@link SelectionKey aren't supported.
 SelectorHandler getSelectorHandler()
          Return the current SelectorHandler this instance is executing, or null if not supported.
 ExecutorService getThreadPool()
          Return the ExecutorService executing this instance.
 void incrementRefCount()
          Called by outer Threads that are not instances of WorkerThread to indicate that this Context should not be recycle() or offered back to its pool.
 boolean isSuspended()
          Return true if this Context has been suspended by invoking suspend.
 void recycle()
          Recycle this instance.
 Object removeAttribute(String key)
          Remove a key/value object.
 void resume()
          Resume a suspend()ed Context.
 void setAttributes(Map<String,Object> attributes)
          Set a Map of attribute name/value pairs.
 void setIOEvent(IOEvent<Context> ioEvent)
          Set an optional CallbackHandler.
 void setKeyRegistrationState(Context.KeyRegistrationState keyRegistrationState)
          Set the SelectionKey's next registration state
 void setProtocolChain(ProtocolChain protocolChain)
          Set the ProtocolChain used by this Context.
 void setThreadPool(ExecutorService threadPool)
          Set the ExecutorService that will execute this instance.
 void suspend()
          Suspend the execution of this Context.
 
Methods inherited from interface com.sun.grizzly.util.AttributeHolder
getAttribute, setAttribute
 
Methods inherited from interface com.sun.grizzly.util.Copyable
copyTo
 

Field Detail

THROWABLE

static final String THROWABLE
Constant 'throwable' String

See Also:
Constant Field Values
Method Detail

getIOEvent

IOEvent getIOEvent()
Return the current IOEvent associated with this instance.

Returns:
IOEvent the current IOEvent associated with this instance.

getCurrentOpType

Context.OpType getCurrentOpType()
Get the current SelectionKey interest ops this instance is executing.

Returns:
OpType the currentOpType.

getController

Controller getController()
Return the current Controller.

Returns:
- this Context's current Controller

execute

void execute()
Deprecated. 

Execute this Context using the Controller's thread pool


execute

void execute(ContextTask contextTask)
Execute this Context using the Controller's thread pool

Parameters:
contextTask - ContextTask, which will be executed by ExecutorService

execute

void execute(ContextTask contextTask,
             boolean runInSeparateThread)
Execute this Context using either Controller's thread pool or current thread

Parameters:
contextTask - ContextTask, which will be executed by ExecutorService
runInSeparateThread - if true - ContextTask will be executed in separate thread, false - in current thread.

recycle

void recycle()
Recycle this instance.


getSelectionKey

SelectionKey getSelectionKey()
Return the current SelectionKey or null if {@link SelectionKey aren't supported.

Returns:
- this Context's SelectionKey

getProtocol

Controller.Protocol getProtocol()
Return the current Controller#Protocol this instance is executing.

Returns:
the current Controller.Protocol this instance is executing.

cancel

void cancel()
Cancel a suspend()ed Context. Invoking this method will automatically clean the state of this Context and mark it as a candidate for being re-used by another Thread and connection. Important. When cancelled, all operations done on this object are not thread-safe and there is probability that another thread is already using this object. Never use this object once cancelled. When invoked this method will automatically close the underlying connection (represented by its SelectionKey. If the Context hasn't been suspended, calling that method has no effet.


getProtocolChain

ProtocolChain getProtocolChain()
Return ProtocolChain executed by this instance.

Returns:
ProtocolChain instance

setIOEvent

void setIOEvent(IOEvent<Context> ioEvent)
Set an optional CallbackHandler.

Parameters:
ioEvent - the IOEvent to set

getKeyRegistrationState

Context.KeyRegistrationState getKeyRegistrationState()
Return SelectionKey's next registration state, or null if not supported.

Returns:
this Context's SelectionKey registration state

getSelectorHandler

SelectorHandler getSelectorHandler()
Return the current SelectorHandler this instance is executing, or null if not supported.

Returns:
the current SelectorHandler this instance is executing.

removeAttribute

Object removeAttribute(String key)
Remove a key/value object.

Specified by:
removeAttribute in interface AttributeHolder
Parameters:
key - - name of an attribute
Returns:
attribute which has been removed

setAttributes

void setAttributes(Map<String,Object> attributes)
Set a Map of attribute name/value pairs. Old AttributeHolder values will not be available. Later changes of this Map will lead to changes to the current AttributeHolder.

Specified by:
setAttributes in interface AttributeHolder
Parameters:
attributes - - map of name/value pairs

getAttributes

Map<String,Object> getAttributes()
Return a Map of attribute name/value pairs. Updates, performed on the returned Map will be reflected in this AttributeHolder

Specified by:
getAttributes in interface AttributeHolder
Returns:
- Map of attribute name/value pairs

setKeyRegistrationState

void setKeyRegistrationState(Context.KeyRegistrationState keyRegistrationState)
Set the SelectionKey's next registration state

Parameters:
{@link - keyRegistrationState} - set this Context's SelectionKey registration state

getAsyncQueueWritable

AsyncQueueWritable getAsyncQueueWritable()
Returns AsyncQueueWritable, assciated with the current Context. This method is not threadsafe.

Returns:
AsyncQueueWritable

getAsyncQueueReader

AsyncQueueReader getAsyncQueueReader()
Return the

Returns:
the

getAttributeHolderByScope

AttributeHolder getAttributeHolderByScope(Context.AttributeScope scope)
Return AttributeHolder, which corresponds to the given Context.AttributeScope>

Parameters:
scope - - Context.AttributeScope>
Returns:
- AttributeHolder instance, which contains Context.AttributeScope> attributes

getThreadPool

ExecutorService getThreadPool()
Return the ExecutorService executing this instance.

Returns:
ExecutorService

setThreadPool

void setThreadPool(ExecutorService threadPool)
Set the ExecutorService that will execute this instance.

Parameters:
thread - pool the ExecutorService to set

setProtocolChain

void setProtocolChain(ProtocolChain protocolChain)
Set the ProtocolChain used by this Context.

Parameters:
protocolChain - instance of ProtocolChain to be used by the Context

suspend

void suspend()
Suspend the execution of this Context. Suspending the execution allow application to store the current instance, and re-use it later by not only the Thread used when called suspend, but also from any other Thread. A suspended Context will not be re-used by any other transaction and Thread. A suspended Context will keep its current state intact, meaning its SelectionKey, attributes, SelectorHandler, etc, will not change. Internally, The Context will not be recyled and will not be re-used by any Thread. When invoked this method will automatically set the setKeyRegistrationState(com.sun.grizzly.Context.KeyRegistrationState) to Context.KeyRegistrationState to KeyRegistrationState.NONE. Invoking this method many times as not effect once suspended.


isSuspended

boolean isSuspended()
Return true if this Context has been suspended by invoking suspend. When suspended, invoking recycle() will throw an IllegalStateException

Returns:
true if this Context has been suspended

resume

void resume()
Resume a suspend()ed Context. Resume will not call recycle(). So after the caller is finished using Context caller must call Controller.returnContext(com.sun.grizzly.Context) to mark it as a candidate for being re-used by another Thread and connection. Important. When resumed, all operations done on this object are not thread-safe and there is probability that another thread is already using this object. Never use this object once resumed. When invoked this method will automatically set the setKeyRegistrationState(com.sun.grizzly.Context.KeyRegistrationState) to Context.KeyRegistrationState to KeyRegistrationState.REGISTER and automatically re-enable read and write operations. If the Context hasn't been suspended, calling that method has no effet.


incrementRefCount

void incrementRefCount()
Called by outer Threads that are not instances of WorkerThread to indicate that this Context should not be recycle() or offered back to its pool. When a outer Thread is done with Context it must call Controller#returnContext(com.sun.grizzly.Context) to ensure that {@link Context} will be properly recycled.


decrementRefCount

int decrementRefCount()
Decrements the reference count of this Context. Threads wanting to release Context should not call this method but instead use Controller.returnContext(com.sun.grizzly.Context)

Returns:
return decremented reference count


Copyright © 2012 Oracle Corporation. All Rights Reserved.