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?
    }

Reply via email to