mina-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edwin Lee <edwin11_1...@yahoo.com.sg>
Subject Re: Why program exits after NioDatagramAcceptor's bind()?
Date Tue, 07 Oct 2008 04:30:43 GMT
Hi hez,

hmm does your JVM exit? it shouldn't, right? because the listener worker
thread(s) that are working in the background are non-daemon threads...

anyway, what i'll suggest is to use passive wait with a flag to allow it to be
stopped later, so that you can clean up resources (unbind and dispose). (After
calling dispose, the JVM should exit provided you have no other acceptors or
other threads still running).

i.e.

Boolean running = Boolean.TRUE;

....

acceptor.bind(new InetSocketAddress(port));

synchronized (running) {
    while (running.booleanValue()) {
        running.wait();
    }
}

acceptor.unbind();
acceptor.dispose();

then you can have another method to stop the server:

public void stop() {
    synchronized (running) {
        running = Boolean.FALSE;
        running.notify();
    }
}



HTH,
Edwin



--- hezjing <hezjing@gmail.com> wrote:

> Hi Edwin
> Yes, I'm expecting the program to wait for an incoming connection.
> 
> I have already specified the IoHandlerAdapter to the NioDatagramAcceptor,
> 
>         acceptor.setHandler(new IoHandlerAdapter());
> 
> 
> Do you mean I have to write a logic after the bind() to keep the program
> running?
> 
>         acceptor.bind(new InetSocketAddress(port));
>         // wait for the connection here ...
>         while (true);
> 
> 
> On Sat, Oct 4, 2008 at 2:46 AM, Edwin Lee <edwin11_1979@yahoo.com.sg> wrote:
> 
> > Hi Hez,
> >
> > 1. Are you expecting the line
> >
> > acceptor.bind(new InetSocketAddress(port));
> >
> > to block until an incoming connection is accepted? (i.e. similar to
> > ServerSocket.accept())
> >
> > 2. or are you saying that, after the line is executed, the programme
> > terminates
> > completely, and no longer listens on that port? (Check netstat)
> >
> > if (1), well actually, this non-blocking is actually a feature of MINA (and
> > Java NIO). After the bind, the execution continues into the next line, and
> > "event" related to this bound port will be handled by a Handler object
> > (similar
> > to event listeners and message-driven beans.) What you can do is to
> > implement a
> > subclass of org.apache.mina.core.service.IoHandlerAdapter, e.g.
> >
> > static class TestHandler extends IoHandlerAdapter {
> >
> >    @Override
> >    public void messageReceived(IoSession session, Object message) {
> >
> >        System.out.println("Message Received");
> >    }
> >
> >    @Override
> >    public void sessionOpened(IoSession session) {
> >
> >        System.out.println("Incoming Connection Accepted");
> >    }
> > }
> >
> > then, in your code segment,
> >
> > acceptor.setHandler(new TestHandler());
> >
> > before binding the acceptor to the port.
> >
> >
> >
> > HTH,
> > Edwin
> >
> >
> >
> > --- hezjing <hezjing@gmail.com> wrote:
> >
> > > Hi
> > > I have the following UDP server code,
> > >
> > >         NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
> > >         // temporary set to IoHandlerAdapter ...
> > >         acceptor.setHandler(new IoHandlerAdapter());
> > >
> > >         DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
> > >         chain.addLast("logger", new LoggingFilter());
> > >
> > >         DatagramSessionConfig sessionConfig =
> > acceptor.getSessionConfig();
> > >         sessionConfig.setReuseAddress(true);
> > >
> > >         acceptor.bind(new InetSocketAddress(port));
> > >
> > >
> > > The problem now is the code is executed successfully and terminated after
> > > the bind().
> > > I think the program should stay and listen to the specified port after
> > the
> > > bind()?
> >
> >
> >
> >       Try cool new emoticons, skins, plus more space for friends.
> > Download Yahoo! Messenger Singapore now!
> > http://sg.messenger.yahoo.com
> >
> >
> 
> 
> -- 
> 
> Hez
> 



      Get your preferred Email name!
Now you can @ymail.com and @rocketmail.com
http://mail.promotions.yahoo.com/newdomains/sg/


Mime
View raw message