dev@grizzly.java.net

Re: refactring SelectionKeyHandler related code

From: charlie hunt <charlie.hunt_at_sun.com>
Date: Tue, 28 Aug 2007 10:02:32 -0500

Hi Alexy,

I like the idea, +1.

I have one general comment that's outside the realm of the specific changes.

I think we have had some discussion about this in the past. But, I
don't recall the specifics of the conclusion or resolution. My comment
is whether and/or what we should be doing with the catching of
exceptions such as this example:

    try {
        channel.close();
    } catch (IOException ex){
    }


Should we be logging such an example at a FINE level, for instance?

I suppose I should go back and look at meeting minutes to see what we
did decide, and if we have addressed my question ;-)

charlie ...

Oleksiy Stashok wrote:
> Hello.
>
> Here is update I propose in order to move SelectionKeyHandler from
> Controller and associate it with SelectorHandler.
> For a while old methods are marked deprecated, but once we will have a
> chance - we need to remove them.
>
> If you have any comments - welcome :)
>
> WBR,
> Alexey.
> ------------------------------------------------------------------------
>
> Index: UDPSelectorHandler.java
> ===================================================================
> --- UDPSelectorHandler.java (revision 432)
> +++ UDPSelectorHandler.java (working copy)
> @@ -31,6 +31,7 @@
> import java.net.SocketAddress;
> import java.net.SocketException;
> import java.nio.channels.DatagramChannel;
> +import java.nio.channels.SelectableChannel;
> import java.nio.channels.SelectionKey;
> import java.nio.channels.Selector;
> import java.util.Iterator;
> @@ -249,6 +250,14 @@
> throw new IllegalStateException(NOT_SUPPORTED);
> }
>
> + public void closeChannel(SelectableChannel channel) {
> + try{
> + channel.close();
> + } catch (IOException ex){
> + ; // LOG ME
> + }
> + }
> +
> //--------------- ConnectorInstanceHandler -----------------------------
> @Override
> protected Callable<ConnectorHandler> getConnectorInstanceHandlerDelegate() {
> Index: DefaultSelectionKeyHandler.java
> ===================================================================
> --- DefaultSelectionKeyHandler.java (revision 434)
> +++ DefaultSelectionKeyHandler.java (working copy)
> @@ -66,11 +66,26 @@
> */
> protected long timeout = 30 * 1000L;
>
> + /**
> + * Associated <code>SelectorHandler</code>
> + */
> + private SelectorHandler selectorHandler;
>
> public DefaultSelectionKeyHandler() {
> }
>
> + public DefaultSelectionKeyHandler(SelectorHandler selectorHandler) {
> + this.selectorHandler = selectorHandler;
> + }
> +
> /**
> + * Set associated <code>SelectorHandler</code>
> + */
> + public void setSelectorHandler(SelectorHandler selectorHandler) {
> + this.selectorHandler = selectorHandler;
> + }
> +
> + /**
> * {_at_inheritDoc}
> */
> public void process(SelectionKey key) {
> @@ -206,7 +221,11 @@
> return;
> }
>
> - closeChannel(key.channel());
> + if (selectorHandler != null) {
> + selectorHandler.closeChannel(key.channel());
> + } else {
> + closeChannel(key.channel());
> + }
>
> key.attach(null);
> key.cancel();
> @@ -238,7 +257,6 @@
> this.timeout = timeout;
> }
>
> -
> protected void closeChannel(SelectableChannel channel) {
> if (channel instanceof SocketChannel) {
> Socket socket = ((SocketChannel) channel).socket();
> @@ -267,5 +285,5 @@
> } catch (IOException ex){
> ; // LOG ME
> }
> - }
> + }
> }
> Index: SelectorHandler.java
> ===================================================================
> --- SelectorHandler.java (revision 432)
> +++ SelectorHandler.java (working copy)
> @@ -228,4 +228,9 @@
> * Return the <code>ProtocolChainInstanceHandler</code>
> */
> public ProtocolChainInstanceHandler getProtocolChainInstanceHandler();
> +
> + /**
> + * Closes <code>SelectableChannel</code>
> + */
> + public void closeChannel(SelectableChannel channel);
> }
> Index: TCPSelectorHandler.java
> ===================================================================
> --- TCPSelectorHandler.java (revision 443)
> +++ TCPSelectorHandler.java (working copy)
> @@ -804,7 +804,37 @@
> return instanceHandler;
> }
>
> -
> + /**
> + * {_at_inheritDoc}
> + */
> + public void closeChannel(SelectableChannel channel) {
> + Socket socket = ((SocketChannel) channel).socket();
> +
> + try {
> + if (!socket.isInputShutdown()) socket.shutdownInput();
> + } catch (IOException ex){
> + ;
> + }
> +
> + try {
> + if (!socket.isOutputShutdown()) socket.shutdownOutput();
> + } catch (IOException ex){
> + ;
> + }
> +
> + try{
> + socket.close();
> + } catch (IOException ex){
> + ;
> + }
> +
> + try{
> + channel.close();
> + } catch (IOException ex){
> + ; // LOG ME
> + }
> + }
> +
> //--------------- ConnectorInstanceHandler -----------------------------
> /**
> * Return <Callable>factory<Callable> object, which knows how
>
> ------------------------------------------------------------------------
>
> Index: main/java/com/sun/grizzly/http/SelectorThread.java
> ===================================================================
> --- main/java/com/sun/grizzly/http/SelectorThread.java (revision 432)
> +++ main/java/com/sun/grizzly/http/SelectorThread.java (working copy)
> @@ -47,7 +47,6 @@
> import java.net.Socket;
> import java.net.SocketException;
> import java.util.Iterator;
> -import java.util.Set;
> import java.util.logging.Logger;
> import java.util.logging.Level;
> import java.util.concurrent.ConcurrentLinkedQueue;
> @@ -617,7 +616,7 @@
> controller.setSelectorHandler(selectorHandler);
>
> DefaultSelectionKeyHandler keyHandler =
> - new DefaultSelectionKeyHandler(){
> + new DefaultSelectionKeyHandler(selectorHandler){
> public void expire(Iterator<SelectionKey> keys){
> super.expire(keys);
> if (isMonitoringEnabled()) {
> @@ -634,7 +633,7 @@
> };
> keyHandler.setLogger(logger);
> keyHandler.setTimeout(keepAliveTimeoutInSeconds * 1000);
> - controller.setSelectionKeyHandler(keyHandler);
> + selectorHandler.setSelectionKeyHandler(keyHandler);
>
> final DefaultProtocolChain protocolChain = new DefaultProtocolChain(){
> public void execute(Context ctx) throws Exception {
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>

-- 
Charlie Hunt
Java Performance Engineer
630.285.7708 x47708 (Internal)
<http://java.sun.com/docs/performance/>