There is nothing wrong with MINA and my program.
It is because of my Ant build file, that I have to specify fork="true" to
make it works!
<target name="run" depends="compile">
<java classname="Server" fork="true">
<classpath refid="runtime.path" />
</java>
</target>
Thank you for all your help!
On Tue, Oct 7, 2008 at 4:50 PM, Ashish <[EMAIL PROTECTED]> wrote:
> Hez,
>
> The Program does not terminated. Its working fine, as expected
> listening. try sending some UDP packets and check the logs.
> Edwin has already explained that the call is non-blocking, and the
> worker threads are working in the background as non-daemon threads.
> Since the call is non-blocking, the call returns and hence you see
> "DONE".
>
> I copied your code and ran the program. It is working. Are you getting
> the command prompt back, when you run the program?
>
> thanks
> ashish
>
>
> On Tue, Oct 7, 2008 at 11:55 AM, hezjing <[EMAIL PROTECTED]> wrote:
> > Hi Edwin
> >
> > Here is my program,
> >
> > public class Server {
> >
> > private final Logger logger = LoggerFactory.getLogger(Server.class);
> >
> > private Server(int port) throws IOException {
> > NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
> > 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));
> > logger.info("Quota Server is listening on port {}", port);
> > }
> >
> > public static void main(String[] args) {
> > try {
> > Server server = new Server(8121);
> > } catch (Exception e) {
> > e.printStackTrace();
> > System.exit(1);
> > }
> > System.out.println("Done");
> > }
> > }
> >
> > When run, the program prints the following output and the JVM is
> terminated.
> >
> > Did I miss anything here?
> >
> >
> > 14:23:04.296 [main] INFO com.openet.quotaserver.Server - Quota Server is
> > listening on port 8121
> > Done
> >>
> >
> >
> > On Tue, Oct 7, 2008 at 12:30 PM, Edwin Lee <[EMAIL PROTECTED]
> >wrote:
> >
> >> 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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]>
> >> 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 <[EMAIL PROTECTED]> 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/
> >>
> >>
> >
> >
> > --
> >
> > Hez
> >
>
>
>
> --
> thanks
> ashish
>
> Visit My Photo Galleries: http://www.pbase.com/ashishpaliwal
>
--
Hez