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);
}
.......
}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();
.......Sorry my mistake.
There was a bug in one of the Server classes.
Now it's working.
bye
Francesco