Hi,
please see the attached filter I wrote for my own purposes. You can add
it to your filter chain and adjust the maximum allowed connections over
the method 'setMaxConnection( int pMaxConnections )'
Regards
Michael
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.util.SessionLog;
/**
* A [EMAIL PROTECTED] IoFilter} which limits the maximum simultaneous permitted
* opened connections.
*/
public class ConnectionLimitFilter extends IoFilterAdapter {
/**
* Defines the maximum simultaneous permitted opened connections.
* A value less then zero turns the filtering off.
* A value equal to zero blocks every new connection.
*/
private int mMaxConnections = -1;
private int mOpenConnections = 0;
/**
* Set the maximum of simultaneous permitted opened connections.
*
* NOTE: Values less then zero turn the filter off. A value of 0
* blocks all new incoming connections.
*
* @param the maximum of simultaneous permitted opened connections
*/
public synchronized void setMaxConnections( int pMaxConnections )
{
mMaxConnections = pMaxConnections;
}
public void sessionCreated( NextFilter pFilter, IoSession pSession )
{
mOpenConnections++;
if( !blockConnection() )
{
pFilter.sessionCreated( pSession );
}
else
{
closeSession( pSession );
}
}
public void sessionClosed( NextFilter pFilter, IoSession pSession )
throws Exception
{
mOpenConnections--;
pFilter.sessionClosed( pSession );
}
/**
* Closes the specified session.
*
* NOTE: Override this method to specify your own handling for closing
* discarded sessions (e.g. advanced logging, personalized messages to
* the remote client).
*
* @param the session to be closed
*/
protected void closeSession( IoSession pSession )
{
pSession.write( "Session maximum exceeded. Please try again
later." );
pSession.close();
}
/**
* Returns if the maximum of simultaneous permitted opened connections
* is reached and the new connection sould be blocked.
*
* NOTE: Override this method to achieve a more sophisticated blocking
* behaviour, e.g. depending on applications time response.
*
* @return <tt>true</tt> if maximum reached, otherwise <tt>false</tt>
*/
protected boolean blockConnection()
{
return (mMaxConnections >= 0 && mOpenConnections > mMaxConnections);
}
}