Thursday, November 20, 2008

GWT

A few important differences between GWT and Swing. First, the components that ship with GWT don’t follow the Model View Controller (MVC) pattern: No model object can be shared by multiple components to keep them in sync. Second, GWT doesn’t use layout managers to control the layout.

Monday, November 10, 2008

RESTful Web Services

- Goal: to reunite the programmable web (RSS, XML-RPC, SOAP) with the human web.
- HTTP is a document-based protocol

• The scoping information (“why should the server send this data instead of that data?”) is kept in the URI. This is the principle of addressability.
• The method information (“why should the server send this data instead of deleting it?”) is kept in the HTTP method. There are only a few HTTP methods, and everyone knows ahead of time what they do. This is the principle of the uniform interface
Statelessness means that every HTTP request happens in complete isolation.
• Connectedness

The difference between PUT and POST is this: the client uses PUT when it’s in charge of deciding which URI the new resource should have. The client uses POST when the server is in charge of deciding which URI the new resource should have.

Thursday, October 9, 2008

NIO server framework



- It follows one dispatcher, multiple worker threads model. The HandlerAdapter (worker thread) need to disable the channel's interest ops before the processing, to avoid selector fire more events and spawn other worker thread; it resumes channel's interest ops after the processing. However, selector is not thread-safe. So the approach is to only allow dispatcher thread updates channel's interest ops. Worker threads only queue the status change requests to dispatcher thread.

- HandlerAdapter present a Facade through which the InputHandler may interact with the framework. It encapsulates event state, channels, input and output queues. Client code registers an InputHandler for a channel, dispatcher wraps handler in an internal adapter class. The client InputHandler is decoupled from NIO and dispatcher implementation details. **Good practice **

- Client handler is passed a ChannelFacade interface through which it may interact with the channel and/or queues. **Elegant encapsulation, hide channel **

- When client code want to write data, i.e., send request, it acquires the handler, and then output byte data to the outputQueue of the channelFacade, modifies the interest ops to OP_WRITE, then next time selector will fire writable event and drain out the output queue to channel.

See: NIO Server Framework

Sunday, September 28, 2008

Java NIO [reading note]




- NIO is primarily block oriented, java.io uses streams
page-oriented nature of system I/O (disk, file)
stream I/O, OS provides non-blocking, readiness selection support

- Channels are conduits to OS I/O service, not stream. They do bulk data transfer to and from buffer, channel.write(buffer) ~= buffer.get(byteArray), channel.read(buffer)~= buffer.put(byteArray). Data is logically transferred in blocks, not char/byte at a time.

- Channels are gateways through which the native I/O services of the operating system can be accessed with a minimum of overhead, and buffers are the internal endpoints used by channels to send and receive data. Channel implementations often use native code. The channel interfaces allow you to gain access to low-level I/O services in a controlled and portable way.

- The new socket channels can operate in nonblocking mode and are selectable. These two capabilities enable multiplexed I/O.

- Readiness selection effectively prevents you from separating the code that checks for readiness from the code that processes the data, at least without significant complexity.

- A SelectionKey encapsulates the registration relationship between a specific channel and a specific selector. SelectionKey objects contain two bit sets (encoded as integers) interest set and ready set indicating which channel operations the registrant has an interest in and which operations the channel is ready to perform.

- Building Highly Scalable Servers with Java NIO has a good discussion on thread model:

  • M dispatchers/no workers
Several event-dispatch threads doing all the work (dispatching events, reading, processing requests, and writing).
  • 1 dispatcher/N workers
A single event-dispatch thread and multiple worker threads.
  • M dispatchers/N workers
Multiple event dispatch threads and multiple worker threads.

- More thread != More efficient. More thread means more context switching overhead. Actual concurrency is limited by the number of CPUs available.

See: Introducing Nonblocking Sockets