Hi Cam,

If you are using a SocketConnector, you need to set workerTimeout to
smaller value such as 3 seconds: connector.setWorkerTimeout(3);

If you are using trunk, you have to call connector.dispose() explicitly.

HTH,
Trustin

On Nov 21, 2007 1:38 AM, Cam Bazz <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I am having trouble closing connections. I made a custom protocol codec
> as described in the tutorial.
> I will pass tuple messages and receive and send responses. However the
> program will only quit if I call System.exit(0);
>
> it wont disconnect from mina server.  client.disconnect() will only run
> if waited 1000 ms.
>
> Best regards,
> -C.B.
>
> Consider the code below:
>
>     public static final int CONNECT_TIMEOUT = 100;
>
>     private String host;
>     private int port;
>     private SocketConnector connector;
>     private IoSession session;
>
>     public Client(String host, int port) {
>         this.host = host;
>         this.port = port;
>         connector = new SocketConnector();
>         connector.getFilterChain().addLast("codec", new
> ProtocolCodecFilter(new TupleCodecFactory(true)));
>     }
>
>     public void connect()
>     {
>         ConnectFuture connectFuture = connector.connect(new
> InetSocketAddress(host,port), this);
>         connectFuture.join(CONNECT_TIMEOUT);
>
>         try {
>             session = connectFuture.getSession();
>         } catch (RuntimeIOException e) {
>             // TODO: log error
>         }
>     }
>
>     public void disconnect() {
>         if (session != null) {
>             session.close().join(CONNECT_TIMEOUT);
>             session = null;
>         }
>     }
>
>     public void sendRequest(Tuple tuple) {
>         if (session == null) {
>             // TODO: log error
>         } else {
>             session.write(tuple);
>         }
>     }
>
>     public void messageReceived(IoSession session, Object message)
> throws Exception {
>     }
>
>     public static void main(String[] args) {
>
>        Client client = new Client("localhost", 63795);
>         client.connect();
>
>         for(int i=0; i<2; i++) {
>             client.sendRequest(new Tuple(10, 10));
>         }
>
>         //try { Thread.sleep(1000); } catch (InterruptedException e) {}
>
>         client.disconnect();
>
>         // TODO: why wont it quit after here?
>     }
>



-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Reply via email to