Mytec
Grizzly 2.0 StreamReader/StreamWriter API
When you work with Grizzly 2.0, you'll most probably come to situation, when you need to read or write some data on network :))
Core Grizzly 2.0 I/O API is based on 3 main entities:
- Connection, which represents any kind of Transport connection. As example for TCP NIO Transport, one Connection represents one NIO SocketChannel.
- StreamReader represents Connection's input stream, using which it's possible to read data from Connection.
- StreamWriter represents Connection's output stream to write data to Connection.
So to read data from Connection, we need to read data from Connection's StreamReader: connection.getStreamReader().readXXX(); The similar step, but with StreamWriter, we need to make in order to write some data to Connection: connection.getStreamWriter().writeXXX(...);
StreamReader and StreamWriter API has a set of readXXX(), writeXXX() methods to work with Java primitives and arrays of primitives. For example to write float value to Connection we call corresponding StreamWriter write method: connection.getStreamWriter().writeFloat(0.3f);
How Streams could work in non-blocking?
In Grizzly 2.0 we can work with StreamReader and StreamWriter either in blocking or non-blocking mode. The mode could be checked and set using following methods: isBlocking(), setBlocking(boolean).
When Streams operate in blocking mode - all their methods, which may work asynchronously (return "Future") will work in blocking mode, and returned Future will always have ready result.
As for non-blocking mode, ok let's start from...
StreamReader.
In order to use StreamReader in non-blocking mode - we may want to check if StreamReader has enough data to be read: streamReader.availableDataSize(); So once StreamReader has enough data - we can safely read it without blocking.
It is also possible to ask StreamReader to notify us once it will have enough data, or provide any other custom Condition, which StreamReader should check each time new data come - and notify us, once this Condition is met. For example:
Future<Integer> future = streamReader.notifyAvailable(10, completionHandler);
StreamReader returns Future object, which will mark as done, once StreamReader will have available 10 bytes for reading. At the same time we pass the CompletionHandler, which will be notified, once StreamReader will have 10 bytes available. So it's possible to have poll and push-like notifications with StreamReader.
We can ask StreamReader to get notification, when more complex conditions are met.
Future<Integer> future = streamReader.notifyCondition(customCondition, completionHandler);
This way we implemented SSL handshake mechanism, so SSLStreamReader notifies us, when handshake status becomes NEED_WRAP.
StreamWriter.
Non-blocking mode for StreamWriter means, that stream flushing will be done non-blocking. For example:
Future<Integer> future = streamWriter.flush();
where flush() operation returns Future<Integer>, which could be used to check if bytes were flushed and how may bytes were written on Connection. It is also possible to pass CompletionHandler to flush() operation, which will be notified by StreamWriter, once bytes will be flushed.
Posted at 02:34PM Apr 22, 2009 by oleksiys in Grizzly | Comments[4]
Grizzly 2.0 M1 Release
Finally we're ready to make first milestone release of Grizzly 2.0
The M1 release has 4 modules:
- Framework: Grizzly 2.0 core
- RCM: Resource Consumption Filter implementation, based on Grizzly 2.0 API
- HTTP: Lightweight Web container, based on Grizzly 2.0 API
- Servlet: Lightweight Servlet container, based on Grizzly 2.0 API.
There are 2 modules, which provide examples of basic features, provided by Grizzly 2.0:
- Framework-samples: "echo" client/server application, SSL example, Connection lifecycle control using Grizzly 2.0 API, custom I/O Strategy example.
- HTTP-samples: simple Grizzly lightweight webserver using Grizzly core API (WebFilter) and higher level GrizzlyWebServer API. Both HTTP and HTTPS scenarios.
Following new features are included in this release:
- New I/O API, based on Grizzly StreamReader and StreamWriter.
- Extensible memory management API, which makes possible to implement either simple or complex memory management logic and use it with Grizzly.
- Extensible Strategy API for processing Connection events. Grizzly bundles 4 built-in strategies:
- SameThreadStrategy, which processes all I/O events in the same thread they were got (selector thread), so no additional worker threads are used. This strategy could be very useful and optimal for processing small amount of clients, and lets server have great response time
- LeaderFollowerStrategy. Performs I/O processing in the same thread (selector thread), and delegates selector polling logic to another thread.
- WorkerThreadStrategy. Performs all the I/O events in separate worker thread.
- SimpleDynamicStrategy. Dynamically applies one of the above 3 strategies, depending on number of SelectionKeys selected last time.
- WebFilter. Grizzly lightweight webcontainer implemented as Filter.
- GrizzlyWebServer. High level API to work with Grizzly lightweight webcontainer.
Later I will write separate blogs describing each feature....
Stay tuned...
Posted at 10:49PM Apr 21, 2009 by oleksiys in Grizzly | Comments[1]