-1

I am trying to write an event driven HTTP web server. Because I will be using only one thread, the events have to queued up and handled asynchronously (I am also using Java NIO). However, I am stuck with the initial step only. I have opened a ServerSocketChannel. I am not sure how to get a new SocketChannel connection when a request comes in. Is there an operating system queue that I can access through Java? (I am not sure as Java is OS independent) I do not want to use any blocking calls.

If I am proceeding in the wrong direction, any help would be appreciated.

thanks.

user207421
  • 305,947
  • 44
  • 307
  • 483
user1489580
  • 45
  • 2
  • 5
  • 2
    You might want to give a look at Apache Mina: http://mina.apache.org/ it's a project that simplifies socket handling and they also have server and client examples in their page. – pabrantes Feb 02 '13 at 02:35
  • Why down vote, its a good high level design question. – Siddharth Feb 02 '13 at 04:16
  • If you're using NIO you aren't using ServerSockets and Sockets at all. No real evidence of prior research. Not a real question. @Siddharth As it is presently stated, it is not a 'good high level design question' at all, it is a self-contradiction. – user207421 Feb 04 '13 at 02:53
  • When someone asks "am I proceeding in the wrong direction", you generally tell him what you have done to solve your problem in a similar way. Using thread pools is a good idea since creation of threads when multiple clients connect simultaneously puts pressure on the server to create address space for each thread. I wonder why you would refuse this fact. RMI does not use thread pools since the usage does not demand 1 thread per client. Also you should be aware that RMI is slow. So its not a comparison here at all. I hate to argue with moderators. Scary to do so on SO. I rest my case. – Siddharth Feb 04 '13 at 09:17
  • @EJP: Well My apologizes for using wrong terminology and wordings. But, I DID do my prior research work. As a matter of fact I am using ServerSocketChannel of Java NIO which could be set to non-blocking IOs. My question here is pretty straightforward. How do i access the OS event queue from Java. – user1489580 Feb 04 '13 at 17:40
  • @Siddarth An accept() loop can only process one accept() at a time, so there is no such thing as 'multiple clients connecting at the same time' from the thread creation point of view. You don't know why RMI doesn't use thread pools any more than I do, but there is certainly nothing in its 'usage' that prevents it. I am aware that RMI is *considered* slow but that is mostly due to Serialization and has nothing specifically to do with thread pooling. As a matter of fact RMI does accomplish some thread pooling indirectly, via connection pooling at the client end. – user207421 Feb 04 '13 at 22:13

1 Answers1

0

You need to:

  • create a Selector
  • put the ServerSocketChannel into non-blocking mode
  • register the SSC with the Selector using OP_ACCEPT
  • write a select() loop, which you will find in the NIO tutorial

In the select() loop you will find keys for which isAcceptable() returns true: that means you need to call ServerSocketChannel.accept() to accept a connection. That returns a SocketChannel, which you must then put into non-blocking mode and register with OP_READ.

In turn that will cause keys for which isReadable() returns true: that means you should read the associated SocketChannel.

You will find examples of all this in the NIO Tutorial. It gets much more complicated than this ;-)

user207421
  • 305,947
  • 44
  • 307
  • 483