views:

266

answers:

2

To implement a production level java socket server, what are the potential gotchas that I have to be aware of?

For now I have got a basic code structure using the ServerSocket and Socket classes of core java.net package. The server waits for a client and spawns a new thread for it.
I was wondering whether this is the way to go or are there other issues that I have to consider before launching the code out in the wild.

A few things that I have thought of with my limited knowledge and experience is:

  1. Client disconnection detection and releasing client resources.
  2. Connection pooling, is this necessary or should I stick to the existing design.
  3. Server monitoring, logging and error recovery etc.
  4. Go with ready made framework like Apache Mina..??

What does your experience have to say?...

+1  A: 

I think it depends on your use cases. But some thoughts:

Ready-made frameworks are a good way to go. As you've discovered, there are a lot of things to think about here. See below. Some possibilities (which implement very different strategies) are Jini (discovery-based recovery-oriented-computing framework) or queuing systems (which offer a distinct client/server decoupling and transactionality - JMS and/or AMQP-based)

Pooling is a major issue. It will prevent your server grinding to a halt in the face of too many clients. Can you use the Java ExecutorService ?

How can you monitor this server ? Can you use JMX and JConsole ? If you can, I would suggest this is a good first-step approach.

What happens if your server shuts down ? Will your clients have the ability to automatically reconnect ? Retry their outstanding requests ? Is it a good idea to retry a request (i.e. is it idempotent) ?

What are you transporting between the client and server ? Are they serialised Java classes ? If so, have you ensured that a simple recompile of a server doesn't require a recompile of the clients (via serialVersionUids). If you're transporting Strings and converting to byte arrays (a common scenario) do the client and server use the same character encoding ? If you're confused about encodings then read this and (possibly) enforce the same client/server encoding via the confusingly named file.encoding property.

As ever with distributed computing, consider Peter Deutsch's Fallacies of Distributed Computing.

Brian Agnew
JMX and JConsole are new concepts for me, maybe I will have to go into detail regarding those, any leads where to start?ExecutorService have heard of it but never used it before, what would be the pros of using it?Server shut down and client reconnection? I have never thought of that, what should I do in that case?Yes I will be be transporting strings and am serving Flash clients so I have no idea about encoding.
Kevin Boyd
Ah :-) So lots to think about! JMX/Jconsole will expose your program internals (e.g. thread pools) for monitoring. ExecutorService will allow thread pooling, so you can hand off work from the sockets to that pool and limit the number of concurrent threads. Think about having your clients reconnect after a disconnect is determined. And for encodings see the above link.
Brian Agnew
Summarizing, what ready-made framework would you suggest that cover most of the discussed topics above?
Kevin Boyd
Jini handles service discovery and managing service leases (lifetimes) nicely. Queues (implementing JMS or AMQP or similar) allow clients and services to be decoupled via queues, and permit transactional invocations.
Brian Agnew
At the moment I am using core java J2SE classes while JMS is J2EE how do these two integrate? Are there any connectors for this?
Kevin Boyd
+1  A: 

I'd be inclined to simply use a existing library to do all that for you. Perhaps the 2 main ones are Apache MINA and JBoss Netty, the latter is (reportedly) quicker and not prone to causing out of memory errors. A MINA send is asynchronous onto a queue which is flushed by another thread, it's therefore quite possible to get OoM even if you're careful. They have a JIRA about this IIRC.

Alternatively use HornetQ which is built on top of Netty but also provides a JMS facade in the client api hence you can run an inprocess messaging server which you can interact as per JMS. Quite convenient really.

Matt
Out of Memory Errors? is there a way out of them? how do I restart the server then? Will have a look at Netty.
Kevin Boyd
there may have been some progress on this since I last looked, see http://mina.apache.org/traffic-throttling.html though no implemented solution as far as I can see. Once you're OoM then that's that, you need to restart the jvm.
Matt
Can A Flash client get access to a JMS service?
Kevin Boyd
that's probably a question all of its own, a quick google says yes but I don't know flash at all --> http://opensource.adobe.com/wiki/display/blazeds/Features
Matt