I've been given the task of completing a project at work written in Java where the actions of web users needs to cause a socket to connect to a server and exchange messages in a proprietary protocol. There is a server running at each hotel, and depending on the hotel the web user has a reservation for, it will connect to the right one. If 30 web users want to use one specific hotel, they all have to share one socket connection.
This is a complex problem and after a couple days of reading I've found a solution that will work well. It will involve the servlets (Struts actions) to make a timed synchronous request to a JMS message queue specific to the hotel. There will be a client message driven bean processing one request from the queue at a time and communicating through the socket.
When programming sockets in VB6 you have a winsock control. You tell it to connect and the Connect() method does not block. You get an event when the connection is established, or an error event. When data arrives, you get an event. When you send data, the SendData() method does not block and you get a SendComplete() event when it's done. People laugh at VB but I really love the winsock control. After spending a couple days reading about socket programming in Java I think VB's winsock control is much nicer to work with. Even using Java's nio asynchrnous sockets you still have to have a main loop polling for events, then process them.
Last night I decided to create JSock, my attempt at making Java socket programming like using winsock in VB. You'll create a JSock instance, set a few properties and tell it to connect. When it connects you'll get a callback. When data arrives you'll get a callback. Etc etc... To accomplish this the JSock class has to create a new thread containing the actual nio socket, and the main loop polling for events. I stayed up until 3am last night writing most of the code though it's still far from being finished. Hopefully another day or two on it and it'll be polished. I'm doing this on my own time, even though it's for work, because I want to release it as open source so other developers can use it and help make it better. If others help make it better than IJW can benefit from it.
Working from his home office in Toronto,
Ryan de Laplante can be found developing software in
Java by day, and obsessing with technology by night.
Ryan has been designing and writing software for
IJW since 1998 and is very passionate about his work.





