Package org.apache.mina.core.service
Class AbstractIoService
- java.lang.Object
-
- org.apache.mina.core.service.AbstractIoService
-
- All Implemented Interfaces:
IoService
- Direct Known Subclasses:
AbstractIoAcceptor
,AbstractIoConnector
public abstract class AbstractIoService extends Object implements IoService
Base implementation ofIoService
s. An instance of IoService contains an Executor which will handle the incoming events.- Author:
- Apache MINA Project
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractIoService.ServiceOperationFuture
AIoFuture
dedicated class for
-
Field Summary
Fields Modifier and Type Field Description protected Object
disposalLock
A lock object which must be acquired when related resources are destroyed.protected static Logger
LOGGER
protected IoSessionConfig
sessionConfig
The defaultIoSessionConfig
which will be used to configure new sessions.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractIoService(IoSessionConfig sessionConfig, Executor executor)
Constructor forAbstractIoService
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addListener(IoServiceListener listener)
Adds anIoServiceListener
that listens any events related with this service.Set<WriteFuture>
broadcast(Object message)
Writes the specifiedmessage
to all theIoSession
s managed by this service.void
dispose()
Releases any resources allocated by this service.void
dispose(boolean awaitTermination)
Releases any resources allocated by this service.protected abstract void
dispose0()
Implement this method to release any acquired resources.protected void
executeWorker(Runnable worker)
protected void
executeWorker(Runnable worker, String suffix)
protected void
finishSessionInitialization0(IoSession session, IoFuture future)
Implement this method to perform additional tasks required for session initialization.long
getActivationTime()
DefaultIoFilterChainBuilder
getFilterChain()
IoFilterChainBuilder
getFilterChainBuilder()
IoHandler
getHandler()
IoServiceListenerSupport
getListeners()
int
getManagedSessionCount()
Map<Long,IoSession>
getManagedSessions()
int
getScheduledWriteBytes()
int
getScheduledWriteMessages()
IoSessionDataStructureFactory
getSessionDataStructureFactory()
IoServiceStatistics
getStatistics()
protected void
initSession(IoSession session, IoFuture future, IoSessionInitializer sessionInitializer)
boolean
isActive()
boolean
isDisposed()
boolean
isDisposing()
void
removeListener(IoServiceListener listener)
Removed an existingIoServiceListener
that listens any events related with this service.void
setFilterChainBuilder(IoFilterChainBuilder builder)
Sets theIoFilterChainBuilder
which will build theIoFilterChain
of allIoSession
s which is created by this service.void
setHandler(IoHandler handler)
Sets the handler which will handle all connections managed by this service.void
setSessionDataStructureFactory(IoSessionDataStructureFactory sessionDataStructureFactory)
Sets theIoSessionDataStructureFactory
that provides related data structures for a new session created by this service.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.mina.core.service.IoService
getSessionConfig, getTransportMetadata
-
-
-
-
Field Detail
-
LOGGER
protected static final Logger LOGGER
-
sessionConfig
protected final IoSessionConfig sessionConfig
The defaultIoSessionConfig
which will be used to configure new sessions.
-
disposalLock
protected final Object disposalLock
A lock object which must be acquired when related resources are destroyed.
-
-
Constructor Detail
-
AbstractIoService
protected AbstractIoService(IoSessionConfig sessionConfig, Executor executor)
Constructor forAbstractIoService
. You need to provide a default session configuration and anExecutor
for handling I/O events. If a nullExecutor
is provided, a default one will be created usingExecutors.newCachedThreadPool()
.
-
-
Method Detail
-
getFilterChainBuilder
public final IoFilterChainBuilder getFilterChainBuilder()
- Specified by:
getFilterChainBuilder
in interfaceIoService
- Returns:
- the
IoFilterChainBuilder
which will build theIoFilterChain
of allIoSession
s which is created by this service. The default value is an emptyDefaultIoFilterChainBuilder
.
-
setFilterChainBuilder
public final void setFilterChainBuilder(IoFilterChainBuilder builder)
Sets theIoFilterChainBuilder
which will build theIoFilterChain
of allIoSession
s which is created by this service. If you specifynull
this property will be set to an emptyDefaultIoFilterChainBuilder
.- Specified by:
setFilterChainBuilder
in interfaceIoService
- Parameters:
builder
- The filter chain builder to use
-
getFilterChain
public final DefaultIoFilterChainBuilder getFilterChain()
A shortcut for( ( DefaultIoFilterChainBuilder )
IoService.getFilterChainBuilder()
)
. Please note that the returned object is not a realIoFilterChain
but aDefaultIoFilterChainBuilder
. Modifying the returned builder won't affect the existingIoSession
s at all, becauseIoFilterChainBuilder
s affect only newly createdIoSession
s.- Specified by:
getFilterChain
in interfaceIoService
- Returns:
- The filter chain in use
-
addListener
public final void addListener(IoServiceListener listener)
Adds anIoServiceListener
that listens any events related with this service.- Specified by:
addListener
in interfaceIoService
- Parameters:
listener
- The listener to add
-
removeListener
public final void removeListener(IoServiceListener listener)
Removed an existingIoServiceListener
that listens any events related with this service.- Specified by:
removeListener
in interfaceIoService
- Parameters:
listener
- The listener to use
-
isActive
public final boolean isActive()
-
isDisposing
public final boolean isDisposing()
- Specified by:
isDisposing
in interfaceIoService
- Returns:
true
if and if onlyIoService.dispose()
method has been called. Please note that this method will returntrue
even after all the related resources are released.
-
isDisposed
public final boolean isDisposed()
- Specified by:
isDisposed
in interfaceIoService
- Returns:
true
if and if only all resources of this processor have been disposed.
-
dispose
public final void dispose()
Releases any resources allocated by this service. Please note that this method might block as long as there are any sessions managed by this service.
-
dispose
public final void dispose(boolean awaitTermination)
Releases any resources allocated by this service. Please note that this method might block as long as there are any sessions managed by this service. Warning : calling this method from a IoFutureListener withawaitTermination
= true will probably lead to a deadlock.
-
dispose0
protected abstract void dispose0() throws Exception
Implement this method to release any acquired resources. This method is invoked only once bydispose()
.- Throws:
Exception
- If the dispose failed
-
getManagedSessions
public final Map<Long,IoSession> getManagedSessions()
- Specified by:
getManagedSessions
in interfaceIoService
- Returns:
- the map of all sessions which are currently managed by this
service. The key of map is the
ID
of the session. An empty collection if there's no session.
-
getManagedSessionCount
public final int getManagedSessionCount()
- Specified by:
getManagedSessionCount
in interfaceIoService
- Returns:
- the number of all sessions which are currently managed by this service.
-
getHandler
public final IoHandler getHandler()
- Specified by:
getHandler
in interfaceIoService
- Returns:
- the handler which will handle all connections managed by this service.
-
setHandler
public final void setHandler(IoHandler handler)
Sets the handler which will handle all connections managed by this service.- Specified by:
setHandler
in interfaceIoService
- Parameters:
handler
- The IoHandler to use
-
getSessionDataStructureFactory
public final IoSessionDataStructureFactory getSessionDataStructureFactory()
- Specified by:
getSessionDataStructureFactory
in interfaceIoService
- Returns:
- the
IoSessionDataStructureFactory
that provides related data structures for a new session created by this service.
-
setSessionDataStructureFactory
public final void setSessionDataStructureFactory(IoSessionDataStructureFactory sessionDataStructureFactory)
Sets theIoSessionDataStructureFactory
that provides related data structures for a new session created by this service.- Specified by:
setSessionDataStructureFactory
in interfaceIoService
- Parameters:
sessionDataStructureFactory
- The factory to use
-
getStatistics
public IoServiceStatistics getStatistics()
- Specified by:
getStatistics
in interfaceIoService
- Returns:
- The statistics object for this service.
-
getActivationTime
public final long getActivationTime()
- Specified by:
getActivationTime
in interfaceIoService
- Returns:
- the time when this service was activated. It returns the last time when this service was activated if the service is not active now.
-
broadcast
public final Set<WriteFuture> broadcast(Object message)
Writes the specifiedmessage
to all theIoSession
s managed by this service. This method is a convenience shortcut forIoUtil.broadcast(Object, Collection)
.
-
getListeners
public final IoServiceListenerSupport getListeners()
- Returns:
- The
IoServiceListenerSupport
attached to this service
-
executeWorker
protected final void executeWorker(Runnable worker)
-
initSession
protected final void initSession(IoSession session, IoFuture future, IoSessionInitializer sessionInitializer)
-
finishSessionInitialization0
protected void finishSessionInitialization0(IoSession session, IoFuture future)
Implement this method to perform additional tasks required for session initialization. Do not call this method directly;initSession(IoSession, IoFuture, IoSessionInitializer)
will call this method instead.- Parameters:
session
- The session to initializefuture
- The Future to use
-
getScheduledWriteBytes
public int getScheduledWriteBytes()
- Specified by:
getScheduledWriteBytes
in interfaceIoService
- Returns:
- The number of bytes scheduled to be written
-
getScheduledWriteMessages
public int getScheduledWriteMessages()
- Specified by:
getScheduledWriteMessages
in interfaceIoService
- Returns:
- The number of messages scheduled to be written
-
-