1 Reply Latest reply on Mar 4, 2005 5:46 AM by fmarchioni fmarchioni

    Troubles converting a Java i/o server to a ServiceMBean

    fmarchioni fmarchioni Newbie

      Hi all,
      I have written a chat server with java.nio classes and I'd like to launch
      it with JBoss as a service.

      So I created a wrapper on it with a ServiceMBean which simply instantiates the chat server.

      public class ChatService extends ServiceMBeanSupport implements ChatServiceMBean
      {
      
      
       // The lifecycle
       protected void startService() throws Exception
       {
       new NbChatServer(9999);
      
       }
       .......
       }



      The NbChatServer is a standard java.nio Server which listens for incoming messages:

      public NbChatServer( int port ) {
      
       this.port = port;
      
       new Thread( this ).start();
      
       }
      
       public void run() {
       try {
      
       ServerSocketChannel ssc = ServerSocketChannel.open();
      
       // Set it to non-blocking, so we can use select
       ssc.configureBlocking( false );
      
       // Get the Socket connected to this channel, and bind it
       // to the listening port
       ServerSocket ss = ssc.socket();
       InetSocketAddress isa = new InetSocketAddress( port );
       ss.bind( isa );
      
       // Create a new Selector for selecting
       Selector selector = Selector.open();
      
       // Register the ServerSocketChannel, so we can
       // listen for incoming connections
       ssc.register( selector, SelectionKey.OP_ACCEPT );
      
       System.out.println( "Server started on port "+port );
      
       while (true) {
       // See if we've had any activity -- either
       // an incoming connection, or incoming data on an
       // existing connection
      
      
       int num = selector.select();
       // EXECUTION HANGS HERE!!!
      
       // If we don't have any activity, loop around and wait
       // again
       if (num == 0) {
       continue;
       }
      
      
       Set keys = selector.selectedKeys();
       Iterator it = keys.iterator();
      
       .......


      As you can see from the comments executions hangs after

      int num = selector.select();

      but launched as a plain Java applications it doesn't stops there........
      why is it such when it's launched from a ServiceMBean ?
      Anyway I can see that the port stays busy as if the server is up but it doesn't receive any messages.......

      is it forbidden to use java.nio api from a ServiceMBean ?

      Hope I can get help........

      Thanks & regards
      Francesco