You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by kwtan <ka...@yahoo.com> on 2007/09/03 16:17:12 UTC

Re: how to configure maximum no. of client connections

Hi,
I can insert the class into the filter chain:
cfg.getFilterChain().addLast( "connection limit", new ConnLimitFilter() );
But how do I adjust the maximum allowed? 
Thanks

Kelvin


Michael Bauroth wrote:
> 
> 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 {@link 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);
>     }
> }
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-configure-maximum-no.-of-client-connections-tf2696920s16868.html#a12462417
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: how to configure maximum no. of client connections

Posted by Mark <el...@gmail.com>.
setMaxConnections(int)

On 9/3/07, kwtan <ka...@yahoo.com> wrote:
>
>
> Hi,
> I can insert the class into the filter chain:
> cfg.getFilterChain().addLast( "connection limit", new ConnLimitFilter() );
> But how do I adjust the maximum allowed?
> Thanks
>
> Kelvin
>
>
> Michael Bauroth wrote:
> >
> > 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 {@link 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);
> >     }
> > }
> >
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/how-to-configure-maximum-no.-of-client-connections-tf2696920s16868.html#a12462417
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>


-- 
..Cheers
Mark