SlideShare una empresa de Scribd logo
1 de 59
Descargar para leer sin conexión
Advanced Java™ NIO Technology-Based
Applications Using the Grizzly Framework
JeanFrancois Arcand,
Oleksiy Stashok,
Sun Microsystems
TS-4883
2008 JavaOneSM
Conference | java.sun.com/javaone | 2
Learn how easy it can be to write scalable
client-server applications with the Grizzly
Framework.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Project Grizzly
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
What is Project Grizzly
● Open Source Project on java.net,
https://grizzly.dev.java.net
● Open Sourced under CDDL/LGPL license.
● Very open community policy.
● All project communications are done on Grizzly mailing list. No
internal, off mailing list conversations.
● Project meetings open to anyone (public conference call).
● Project decisions are made by project member votes.
● Sun and non Sun commiters.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
What is Project Grizzly
● Uses Java NIO primitives and hides the complexity
programming with Java NIO.
● Easy-to-use high performance APIs for TCP, UDP and
SSL communications.
● Brings non-blocking sockets to the protocol processing
layer.
● Utilizes high performance buffers and buffer
management.
● Choice of several different high performance thread
pools.
● Ship with an HTTP module which is really easy to
extend.
● Use the Framework, but STILL have a way to control
the IO layer.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 1 – Server, how easy it is
The Grizzly Framework bundles default implementation
for TCP, UDP and TLS/SSL transports.
Every supported transport SelectorHandler should be
added to a Controller
By default, you can use TCPSelectorHandler,
UDPSelectorHandler and TLSSelectorHandler.
You can also create your own by implementing the
SelectorHandler interface.
In Grizzly, EVERYTHING is customizable. If the default
isn't doing what you need, customize it!
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Grizzly's Monster face
SelectorHandler
SelectorHandler ConnectorHandler
ConnectorHandler
SelectorHandler
SelectorHandler
CallbackHandler
CallbackHandler
ProtocolChain
ProtocolChain
InstanceHandler
InstanceHandler Controller
Controller
Pipeline
Pipeline
SelectionKeyHandler
SelectionKeyHandler
Context
Context ProtocolChain
ProtocolChain
ProtocolFilter
ProtocolFilter
ProtocolFilter 2
ProtocolFilter 2
ProtocolParser
ProtocolParser
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example: Simple log server over TCP
Controller controller = new Controller();
controller.setProtocolChainInstanceHandler(new
DefaultProtocolChainInstanceHandler() {
ProtocolChain protocolChain;
// Stateless ProtocolChain
public ProtocolChain poll() {
if(protocolChain == null) {
protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new ReadFilter());
protocolChain.addFilter(new LogFilter());
}
return protocolChain;
});
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example: Supporting TCP and UDP
Controller controller = new Controller();
controller.addSelectorHandler(new TCPSelectorHandler());
controller.addSelectorHandler(new UDPSelectorHandler());
controller.setProtocolChainInstanceHandler(new
DefaultProtocolChainInstanceHandler() {
ProtocolChain protocolChain;
// Stateless ProtocolChain
public ProtocolChain poll() {
if(protocolChain == null) {
protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new ReadFilter());
protocolChain.addFilter(new LogFilter());
}
return protocolChain;
});
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
SelectorHandler
SelectorHandler UDPSelectorHandler
UDPSelectorHandler
TCPSelectorHandler
TCPSelectorHandler
Controller
Controller
Pipeline
Pipeline
Context
Context ProtocolChain
ProtocolChain
ReadFilter
ReadFilter
LogFilter
LogFilter
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example: Why not TLS?
Controller controller = new Controller();
controller.addSelectorHandler(new TLSSelectorHandler());
controller.setProtocolChainInstanceHandler(new
DefaultProtocolChainInstanceHandler() {
ProtocolChain protocolChain;
// Stateless ProtocolChain
public ProtocolChain poll() {
if(protocolChain == null) {
protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new TLSReadFilter();
protocolChain.addFilter(new LogFilter());
}
return protocolChain;
});
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
SelectorHandler
SelectorHandler
TLSSelectorHandler
TLSSelectorHandler
Controller
Controller
Pipeline
Pipeline
Context
Context ProtocolChain
ProtocolChain
TLSReadFilter
TLSReadFilter
LogFilter
LogFilter
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Controller
The main entry point when working with Grizzly.
A Controller is composed of
• SelectorHandler
• ConnectorHandler
• SelectionKeyHandler
• ProtocolChainInstanceHandler
• ProtocolChain
• Pipeline
By default, all these interfaces have ready to use
implementations.
All of these components are customizable using the
Grizzly Framework
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
SelectorHandler (optional)
A SelectorHandler handles all
java.nio.channels.Selector operations.
The logic for processing of SelectionKey
interest(OP_READ, OP_WRITE, etc) is usually defined
using an instance of SelectorHandler
Ex: TCPSelectorHandler, UDPSelectorHandler
public void preSelect(Context ctx)
throws IOException;
public Set<SelectionKey> select(Context ctx)
throws IOException;
public void postSelect(Context ctx)
throws IOException;
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
SelectionKeyHandler (optional)
A SelectionKeyHandler used to handle the life-cycle of
a SelectionKey. This object is responsible of canceling,
registering or closing (timing out) SelectionKey(s).
You can have one SelectionKey per SelectorHandler,
or globally shared amongst them.
public void register(Iterator<SelectionKey> it, int
selectionKeyOps);
public void expire(Iterator<SelectionKey> it);
public void cancel(SelectionKey key);
public void close(SelectionKey key);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ProtocolFilter
A ProtocolFilter encapsulates a unit of processing work
to be performed, whose purpose is to examine and/or
modify the state of a transaction that is represented by
a Context.
Most Grizzly based application only have to write an
implementation of one ProtocolFilter.
Individual ProtocolFilters can be assembled into a
ProtocolChain.
The only classes you might have to write!
public boolean execute(Context ctx) throws
IOException;
public boolean postExecute(Context ctx) throws
IOException;
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ProtocolParser
A specialized ProtocolFilter that knows how to parse
bytes into protocol unit of data.
Simply protocol implementation in Grizzly. Split
protocol parsing state into steps:
• start processing a buffer
• enumerate the message in the buffer
• and end processing the buffer.
The buffer can contain 0 or more complete messages,
and it's up to the protocol parser to make sense of that.
public T getNextMessage();
public boolean hasNextMessage();
public void startBuffer(ByteBuffer bb);
public boolean releaseBuffer();
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ProtocolChain (optional)
A ProtocolChain implements the “Chain of
Responsibility” pattern (for more info take a look at the
classic “Gang of Four” design patterns book).
The ProtocolChain API models a computation as a
series of “protocol filters”, that can be combined into a
“protocol chain”.
public boolean addFilter(ProtocolFilter pf);
public boolean removeFilter(ProtocolFilter pf);
public void execute(Context context) throws Exception;
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ProtocolChainInstanceHandler (optional)
A ProtocolChainInstanceHandler is where one or
several ProtocolChains are created and cached.
A ProtocolChainInstanceHandler decides if a stateless
or stateful ProtocolChain needs to be created:
• Stateless: One ProtocolChain instance shared amongst
Threads.
• Statefull: One ProtocolChain instance per Thread
public ProtocolChain poll();
public boolean offer(ProtocolChain pc);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Pipeline (optional)
An interface is used as a wrapper around any kind of
thread pool.
The best performing implementation is the default
configured Pipeline.
Can have one Pipeline per SelectorHandler or a shared
one amongst them.
public void execute(E task) throws
PipelineFullException;
public E waitForIoTask() ;
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 2 – Client: synchronous mode
Controller ctrl = new Controller();
ctrl.addSelectorHandler(new TCPSelectorHandler(true));
startGrizzly(ctrl);
ConnectorHandler connection =
ctrl.acquireConnectorHandler(Protocol.TCP);
connection.connect(new InetSocketAddress(host, port));
connection.write(outputByteBuffer, true);
connection.read(inputByteBuffer, true);
connection.close();
ctrl.releaseConnectorHandler(connection);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Grizzly's Client Monster face
ConnectorHandler
ConnectorHandler
CallbackHandler
CallbackHandler
Controller
Controller
Context
Context ProtocolChain
ProtocolChain
ProtocolFilter
ProtocolFilter
ProtocolFilter 2
ProtocolFilter 2
ProtocolParser
ProtocolParser
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 2 – Client: asynchronous mode
Controller ctrl = new Controller();
ctrl.addSelectorHandler(new TCPSelectorHandler(true));
startGrizzly(ctrl);
ConnectorHandler connection =
ctrl.acquireConnectorHandler(Protocol.TCP);
connection.connect(new InetSocketAddress(host, port),
new CustomCallbackHandler(connection));
connection.write(giantByteBuffer, false);
// continue, while ByteBuffer will be writen
asynchronously
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 2 – Client: asynchronous mode
(cont.)
public class CustomCallbackHandler implements
CallbackHandler<Context> {
private ConnectorHandler connection;
CustomCallbackHandler(ConnectorHandler connection){
this.connection = connection;
}
public void onWrite(IOEvent<Context> ioEvent) {
connection.write(giantByteBuffer, false);
if (!giantByteBuffer.hasRemaining()) {
notifyAsyncWriteCompleted();
}
}
// Skip other CallbackHandler methods implementation...
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ConnectorHandler
Client side connection unit, which implements basic I/O
functionality: connect, read, write, close.
Provides possibility of working in both synchronous and
asynchronous modes (e.g. blocking or non blocking
read/write).
Asynchronous operation execution could be controlled
by CallbackHandler with user custom code, or
automatically by framework, using asynchronous read
and write queues.
Grizzly Framework contains default TCP,TLS UDP
ConnectorHandler implementations.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
CallbackHandler
Handles client side asynchronous I/O operations:
connect, read, write. Corresponding CallbackHandler
method will be called, when NIO channel is ready to
perform the operation.
Asynchronous events could be either processed inside
CallbackHandler or propagated to a ProtocolChain.
When processing asynchronous event inside
CallbackHandler, be careful with SelectionKey interests
registration.
public void onConnect(IOEvent<E> ioEvent);
public void onRead(IOEvent<E> ioEvent);
public void onWrite(IOEvent<E> ioEvent);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Client and server side logic unification
There are use cases, where both client and server
sides should process requests similar way, for example
CORBA or SIP.
CallbackHandler are able to redirect a request
processing to a ProtocolChain, so high level protocol
logic, which is implemented in ProtocolChain filters will
be accessible from client side API too.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ConnectorHandler
ConnectorHandler
CallbackHandler
CallbackHandler
Controller
Controller
Context
Context ProtocolChain
ProtocolChain
ReadFilter
ReadFilter
LogFilter
LogFilter
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 3 – Client and server side logic
unification
public class CallbackHandlerToProtocolChain implements
CallbackHandler<Context> {
public void onRead(IOEvent<Context> context) {
Context context = ioEvent.attachment();
context.getProtocolChain().execute(context);
}
// Skip other CallbackHandler methods implementation...
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Asynchronous write queue
Provides user the easy way for sending data
asynchronously and be notified, when operation will be
either successfully completed or interrupted due to I/O
error.
Asynchronous write queue is thread safe. Data from
different buffers will never be intermixed.
Asynchronous write queue processors provide easy
and pluggable way to compress, encrypt output data
just before it will be sent over NIO channel.
Grizzly Framework has default asynchronous write
queue implementation for UDP and TCP protocols.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Asynchronous read queue
Provides user the easy way for receiving data
asynchronously and be notified, when operation will be
either successfully completed or interrupted due to I/O
error.
Asynchronous read queue is thread safe. Next buffer
will start to fill up only after current one will be
completed.
Asynchronous read queue processors provide easy
and pluggable way to decompress, decrypt input data
just after it was read from NIO channel.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Asynchronous read queue (cont.)
Asynchronous read conditions provide user possibility
to set custom “read complete” condition. By default
asynchronous read will consider completed, when input
ByteBuffer will become full.
Grizzly Framework has default asynchronous read
queue implementation for UDP and TCP protocols.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 6 – Asynchronous queue: write
class AsyncQueueEchoFilter implements ProtocolFilter {
public boolean execute(Context ctx)
throws IOException {
ByteBuffer buffer = getByteBuffer(ctx);
// we pass null for callback notificator and
// data processor. Last true parameter means buffer
// will be cloned before added to the queue
ctx.getAsyncQueueWritable().writeToAsyncQueue(
buffer, null, null, true);
}
public boolean postExecute(Context ctx)
throws IOException {
return true;
}
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 6 – Asynchronous queue: read
ConnectorHandler connection =
ctrl.acquireConnectorHandler(Protocol.TCP);
connection.connect(new InetSocketAddress(host, port));
// request notification, when byte buffer will be full
AsyncReadCallbackHandler notificationHandler =
new CustomAsyncReadCallbackHandler();
connection.readFromAsyncQueue(bigByteBuffer,
notifiicationHandler);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example 6 – Asynchronous queue: read
(cont.)
class CustomAsyncReadCallbackHandler implements
AsyncReadCallbackHandler {
public void onReadCompleted(SelectionKey key,
SocketAddress srcAddress, ByteBuffer buffer) {
processReadData(buffer);
}
public void onIOException(IOException ioException,
...
}
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Synchronous Read and Write
There is some situations where registering a Channel on
more than one Selector improve performance:
• Instead of registering a SelectionKey using the Selector used by
the acceptor thread (the Selector than handled the OP_ACCEPT),
get a Selector from a pool and try to read/write more bytes using it.
readSelector = SelectorFactory.getSelector();
tmpKey = socketChannel.register
(readSelector,SelectionKey.OP_READ)
tmpKey.interestOps(tmpKey.interestOps() |
SelectionKey.OP_READ);
int code = readSelector.select(readTimeout);
tmpKey.interestOps(tmpKey.interestOps() &
(~SelectionKey.OP_READ));
while (count > 0){
count = socketChannel.read(byteBuffer);
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Synchronous Read and Write (Con't)
Grizzly offers two utility classes for supporting
synchronous read and write:
• com.sun.grizzly.util.InputReader
{
InputReader syncReader = new InputReader();
// Block at most 5 seconds.
syncReader.read(bb,5,TimeUnits.SECONDS);
}
• com.sun.grizzly.util.OutputWriter
{
// Block at most 5 seconds.
OutputWriter.write(bb,5,TimeUnits.SECONDS);
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ThreadAttachment
Between OP_READ and OP_WRITE, some protocol
needs to keep some states (remaining bytes inside a byte
buffer, attributes, etc.).
In Grizzly, the default is to associate one Byte Buffer per
Thread. That means a byte buffer cannot be cached as its
Thread can always be re-used for another transaction.
To persist the byte buffer amongs transactions, a Thread
Attachment can be used:
{
WorkerThread wt =
(WorkerThread)Thread.currentThread();
ThreadAttachment = wt.detach();
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
ThreadAttachment (Con't)
What happens is internally, all attributes associated with
the WorkerThread are 'detached', and new instance
recreated
• Warning: A new ByteBuffer will be created!!
The ThreadAttachment can be attached to a SelectionKey,
and next time an OP_READ/OP_WRITE happens, you are
guarantee the value will be re-used.
{
ta.setAttribute(keepAliveCount,10);
ctx.getSelectionKey().attach(ta);
...
TheadAttachment ta = (ThreadAttachment)
ctx.getSelectionKey().attachment();
int keepAliveCount = (int)ta.getAttibute();
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
HTTP modules
The Grizzly Framework also have an HTTP framework that
can be used to build Web Server
• This is what GlassFish™ v1|2|3 build on top of.
• More specialized modules are also available like Comet (Async
HTTP).
Simple interface to allow customization of the HTTP
Protocol
• GrizzlyRequest: A utility class to manipulate the HTTP protocol
request.
• GrizzlyResponse: A utility class to manipulate the HTTP protocol
response.
• GrizzlyAdapter: A utility class to manipulate the HTTP
request/response object.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
HTTP protocol module (Con't)
public class FileAdapter extends GrizzlyAdapter{
public void service(GrizzlyRequest req,
GrizzlyResponse res){
String fileUri = req.getRequestURI();
FileChannel file = ....;
fileChannel.transferTo(...);
res.flush();
res.close();
}
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example: Jersey (jersey.dev.java.net).
Jersey is the open source JAX-RS (Java Specification
Request (JSR) 311) Reference Implementation for building
RESTful Web services.
By default, Jersey ship with support for Grizzly HTTP
WebServer:
• Implement the GrizzlyAdapter interface, use the GrizzlyRequest
and Response object to handle the http protocol.
In less than 20 minutes, Jersey was running with Grizzly!
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example: Jersey (jersey.dev.java.net).
public void service(GrizzlyRequest request,
GrizzlyResponse response) {
try {
requestAdaptor = new GrizzlyRequestAdaptor(
application.getMessageBodyContext(),request);
responseAdaptor =
new GrizzlyResponseAdaptor(response,
application.getMessageBodyContext(),
requestAdaptor);
application.handleRequest(requestAdaptor,
responseAdaptor);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Example: Jersey (Con't).
protected void commitStatusAndHeaders() throws IOException {
response.setStatus(this.getStatus());
for (Map.Entry<String, List<Object>> e :
this.getHttpHeaders().entrySet()) {
String key = e.getKey();
for (Object value: e.getValue()) {
response.addHeader(key,value.toString());
}
}
String contentType = response.getHeader("Content-Type");
if (contentType != null) {
response.setContentType(contentType);
}
}
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
SelectorHandler
SelectorHandler UDPSelectorHandler
UDPSelectorHandler
TCPSelectorHandler
TCPSelectorHandler
GlassFish v3
GlassFish v3
Controller
Controller
Pipeline
Pipeline
Context
Context ProtocolChain
ProtocolChain
ReadFilter
ReadFilter
V3HttpParser
V3HttpParser
Servlet Container
Servlet Container JRuby Container
JRuby Container Grails Container
Grails Container
GrizzlyAdapter
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
GlassFish v3 Demo
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
NIO.2: Asynchronous I/O (JSR-203)
NIO.2 is:
• An API for asynchronous (as opposed to polled, non-blocking) I/O
operations on both sockets and files.
• The completion of the socket-channel functionality defined in JSR-
51, including the addition of support for binding, option
configuration, and multicast datagrams.
Starting in Grizzly 1.8.0, Asynchronous I/O is supported if
you are running JDK version 7.
You can easily mix non blocking with asynchronous I/O.
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
NIO.2: Asynchronous I/O (JDK version
7.0)
Grizzly supports NIO.2. Switching an application from
using NIO.1 to NIO.2 is quite simple
Bytes are delivered the same way, independently of NIO.1
or NIO.2:
// Instead of TCPSelectorHandler
controller.addIOHandler(new TCPAIOHandler());
//Instead of using ReadFilter
AIOReadFilter readFilter = new AIOReadFilter();
protocolChain.addFilter(readFilter);
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Agenda
What is the Grizzly Framework
The Server Components
The Client Components
Asynchronous Read and Write Queue
Synchronous Read and Write using Temporary Selectors
Persisting your data using ThreadAttachment
HTTP Modules
NIO.2 Support
Summary
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Summary
Project Grizzly is a simple NIO framework.
• With less than 50 lines, you can create power client or server side
components.
• Support NIO.1 and NIO.2.
• You have control of the framework: everything can be customized!
HTTP module makes it easy to build WebServer extension
• Jersey, blog-city.com, Netbeans™ software, Ning, Red Hat REST
Impl, RESTlet, GlassFish v1|2|3, Sailfin Load Balancer, JXTA, and
many many more!
Healthy community: get a response in less than 2 hours
:-)!
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
For More Information
Grizzly:
• http://grizzly.dev.java.net
Jeanfrancois's blog:
• http://weblogs.java.net/blog/jfarcand/
Alexey's blog:
• http://blogs.sun.com/oleksiys/
Project Grizzly mailing lists,
• dev@grizzly.dev.java.net
• users@dev.grizzly.java.net
2008 JavaOneSM
Conference | java.sun.com/javaone | <number>
Jeanfrancois Arcand
Oleksiy Stashok,
Sun Microsystems
TS-4883

Más contenido relacionado

Similar a NIO-Grizly.pdf

Jdk Tools For Performance Diagnostics
Jdk Tools For Performance DiagnosticsJdk Tools For Performance Diagnostics
Jdk Tools For Performance DiagnosticsDror Bereznitsky
 
Massaging the Pony: Message Queues and You
Massaging the Pony: Message Queues and YouMassaging the Pony: Message Queues and You
Massaging the Pony: Message Queues and YouShawn Rider
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaMax Alexejev
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCFAKHRUN NISHA
 
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise IntegratorTroubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise IntegratorWSO2
 
Shopzilla On Concurrency
Shopzilla On ConcurrencyShopzilla On Concurrency
Shopzilla On ConcurrencyWill Gage
 
LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1Hajime Tazaki
 
Introduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android ApplicationIntroduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android ApplicationKelwin Yang
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVMSylvain Wallez
 
Taming Deployment With Smart Frog
Taming Deployment With Smart FrogTaming Deployment With Smart Frog
Taming Deployment With Smart FrogSteve Loughran
 
Opencensus with prometheus and kubernetes
Opencensus with prometheus and kubernetesOpencensus with prometheus and kubernetes
Opencensus with prometheus and kubernetesJinwoong Kim
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)Shaharyar khan
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & ProfilingIsuru Perera
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; JitAnkit Somani
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; JitAnkit Somani
 
Tomcat, Undertow, Jetty, Nginx Unit: pros and cons
Tomcat, Undertow, Jetty, Nginx Unit: pros and consTomcat, Undertow, Jetty, Nginx Unit: pros and cons
Tomcat, Undertow, Jetty, Nginx Unit: pros and consGeraldo Netto
 

Similar a NIO-Grizly.pdf (20)

Java se7 features
Java se7 featuresJava se7 features
Java se7 features
 
Grizzly 20080925 V2
Grizzly 20080925 V2Grizzly 20080925 V2
Grizzly 20080925 V2
 
Maf3 - Part 1
Maf3 - Part 1Maf3 - Part 1
Maf3 - Part 1
 
Jdk Tools For Performance Diagnostics
Jdk Tools For Performance DiagnosticsJdk Tools For Performance Diagnostics
Jdk Tools For Performance Diagnostics
 
Massaging the Pony: Message Queues and You
Massaging the Pony: Message Queues and YouMassaging the Pony: Message Queues and You
Massaging the Pony: Message Queues and You
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and Scala
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise IntegratorTroubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
 
Shopzilla On Concurrency
Shopzilla On ConcurrencyShopzilla On Concurrency
Shopzilla On Concurrency
 
Mina2
Mina2Mina2
Mina2
 
LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1
 
Introduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android ApplicationIntroduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android Application
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 
Taming Deployment With Smart Frog
Taming Deployment With Smart FrogTaming Deployment With Smart Frog
Taming Deployment With Smart Frog
 
Opencensus with prometheus and kubernetes
Opencensus with prometheus and kubernetesOpencensus with prometheus and kubernetes
Opencensus with prometheus and kubernetes
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & Profiling
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; Jit
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; Jit
 
Tomcat, Undertow, Jetty, Nginx Unit: pros and cons
Tomcat, Undertow, Jetty, Nginx Unit: pros and consTomcat, Undertow, Jetty, Nginx Unit: pros and cons
Tomcat, Undertow, Jetty, Nginx Unit: pros and cons
 

Último

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Último (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

NIO-Grizly.pdf

  • 1. Advanced Java™ NIO Technology-Based Applications Using the Grizzly Framework JeanFrancois Arcand, Oleksiy Stashok, Sun Microsystems TS-4883
  • 2. 2008 JavaOneSM Conference | java.sun.com/javaone | 2 Learn how easy it can be to write scalable client-server applications with the Grizzly Framework.
  • 3. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Project Grizzly The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 4. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> What is Project Grizzly ● Open Source Project on java.net, https://grizzly.dev.java.net ● Open Sourced under CDDL/LGPL license. ● Very open community policy. ● All project communications are done on Grizzly mailing list. No internal, off mailing list conversations. ● Project meetings open to anyone (public conference call). ● Project decisions are made by project member votes. ● Sun and non Sun commiters.
  • 5. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> What is Project Grizzly ● Uses Java NIO primitives and hides the complexity programming with Java NIO. ● Easy-to-use high performance APIs for TCP, UDP and SSL communications. ● Brings non-blocking sockets to the protocol processing layer. ● Utilizes high performance buffers and buffer management. ● Choice of several different high performance thread pools. ● Ship with an HTTP module which is really easy to extend. ● Use the Framework, but STILL have a way to control the IO layer.
  • 6. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 7. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 1 – Server, how easy it is The Grizzly Framework bundles default implementation for TCP, UDP and TLS/SSL transports. Every supported transport SelectorHandler should be added to a Controller By default, you can use TCPSelectorHandler, UDPSelectorHandler and TLSSelectorHandler. You can also create your own by implementing the SelectorHandler interface. In Grizzly, EVERYTHING is customizable. If the default isn't doing what you need, customize it!
  • 8. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Grizzly's Monster face SelectorHandler SelectorHandler ConnectorHandler ConnectorHandler SelectorHandler SelectorHandler CallbackHandler CallbackHandler ProtocolChain ProtocolChain InstanceHandler InstanceHandler Controller Controller Pipeline Pipeline SelectionKeyHandler SelectionKeyHandler Context Context ProtocolChain ProtocolChain ProtocolFilter ProtocolFilter ProtocolFilter 2 ProtocolFilter 2 ProtocolParser ProtocolParser
  • 9. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example: Simple log server over TCP Controller controller = new Controller(); controller.setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler() { ProtocolChain protocolChain; // Stateless ProtocolChain public ProtocolChain poll() { if(protocolChain == null) { protocolChain = new DefaultProtocolChain(); protocolChain.addFilter(new ReadFilter()); protocolChain.addFilter(new LogFilter()); } return protocolChain; });
  • 10. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example: Supporting TCP and UDP Controller controller = new Controller(); controller.addSelectorHandler(new TCPSelectorHandler()); controller.addSelectorHandler(new UDPSelectorHandler()); controller.setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler() { ProtocolChain protocolChain; // Stateless ProtocolChain public ProtocolChain poll() { if(protocolChain == null) { protocolChain = new DefaultProtocolChain(); protocolChain.addFilter(new ReadFilter()); protocolChain.addFilter(new LogFilter()); } return protocolChain; });
  • 11. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> SelectorHandler SelectorHandler UDPSelectorHandler UDPSelectorHandler TCPSelectorHandler TCPSelectorHandler Controller Controller Pipeline Pipeline Context Context ProtocolChain ProtocolChain ReadFilter ReadFilter LogFilter LogFilter
  • 12. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example: Why not TLS? Controller controller = new Controller(); controller.addSelectorHandler(new TLSSelectorHandler()); controller.setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler() { ProtocolChain protocolChain; // Stateless ProtocolChain public ProtocolChain poll() { if(protocolChain == null) { protocolChain = new DefaultProtocolChain(); protocolChain.addFilter(new TLSReadFilter(); protocolChain.addFilter(new LogFilter()); } return protocolChain; });
  • 13. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> SelectorHandler SelectorHandler TLSSelectorHandler TLSSelectorHandler Controller Controller Pipeline Pipeline Context Context ProtocolChain ProtocolChain TLSReadFilter TLSReadFilter LogFilter LogFilter
  • 14. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Controller The main entry point when working with Grizzly. A Controller is composed of • SelectorHandler • ConnectorHandler • SelectionKeyHandler • ProtocolChainInstanceHandler • ProtocolChain • Pipeline By default, all these interfaces have ready to use implementations. All of these components are customizable using the Grizzly Framework
  • 15. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> SelectorHandler (optional) A SelectorHandler handles all java.nio.channels.Selector operations. The logic for processing of SelectionKey interest(OP_READ, OP_WRITE, etc) is usually defined using an instance of SelectorHandler Ex: TCPSelectorHandler, UDPSelectorHandler public void preSelect(Context ctx) throws IOException; public Set<SelectionKey> select(Context ctx) throws IOException; public void postSelect(Context ctx) throws IOException;
  • 16. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> SelectionKeyHandler (optional) A SelectionKeyHandler used to handle the life-cycle of a SelectionKey. This object is responsible of canceling, registering or closing (timing out) SelectionKey(s). You can have one SelectionKey per SelectorHandler, or globally shared amongst them. public void register(Iterator<SelectionKey> it, int selectionKeyOps); public void expire(Iterator<SelectionKey> it); public void cancel(SelectionKey key); public void close(SelectionKey key);
  • 17. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ProtocolFilter A ProtocolFilter encapsulates a unit of processing work to be performed, whose purpose is to examine and/or modify the state of a transaction that is represented by a Context. Most Grizzly based application only have to write an implementation of one ProtocolFilter. Individual ProtocolFilters can be assembled into a ProtocolChain. The only classes you might have to write! public boolean execute(Context ctx) throws IOException; public boolean postExecute(Context ctx) throws IOException;
  • 18. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ProtocolParser A specialized ProtocolFilter that knows how to parse bytes into protocol unit of data. Simply protocol implementation in Grizzly. Split protocol parsing state into steps: • start processing a buffer • enumerate the message in the buffer • and end processing the buffer. The buffer can contain 0 or more complete messages, and it's up to the protocol parser to make sense of that. public T getNextMessage(); public boolean hasNextMessage(); public void startBuffer(ByteBuffer bb); public boolean releaseBuffer();
  • 19. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ProtocolChain (optional) A ProtocolChain implements the “Chain of Responsibility” pattern (for more info take a look at the classic “Gang of Four” design patterns book). The ProtocolChain API models a computation as a series of “protocol filters”, that can be combined into a “protocol chain”. public boolean addFilter(ProtocolFilter pf); public boolean removeFilter(ProtocolFilter pf); public void execute(Context context) throws Exception;
  • 20. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ProtocolChainInstanceHandler (optional) A ProtocolChainInstanceHandler is where one or several ProtocolChains are created and cached. A ProtocolChainInstanceHandler decides if a stateless or stateful ProtocolChain needs to be created: • Stateless: One ProtocolChain instance shared amongst Threads. • Statefull: One ProtocolChain instance per Thread public ProtocolChain poll(); public boolean offer(ProtocolChain pc);
  • 21. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Pipeline (optional) An interface is used as a wrapper around any kind of thread pool. The best performing implementation is the default configured Pipeline. Can have one Pipeline per SelectorHandler or a shared one amongst them. public void execute(E task) throws PipelineFullException; public E waitForIoTask() ;
  • 22. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 23. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 2 – Client: synchronous mode Controller ctrl = new Controller(); ctrl.addSelectorHandler(new TCPSelectorHandler(true)); startGrizzly(ctrl); ConnectorHandler connection = ctrl.acquireConnectorHandler(Protocol.TCP); connection.connect(new InetSocketAddress(host, port)); connection.write(outputByteBuffer, true); connection.read(inputByteBuffer, true); connection.close(); ctrl.releaseConnectorHandler(connection);
  • 24. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Grizzly's Client Monster face ConnectorHandler ConnectorHandler CallbackHandler CallbackHandler Controller Controller Context Context ProtocolChain ProtocolChain ProtocolFilter ProtocolFilter ProtocolFilter 2 ProtocolFilter 2 ProtocolParser ProtocolParser
  • 25. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 2 – Client: asynchronous mode Controller ctrl = new Controller(); ctrl.addSelectorHandler(new TCPSelectorHandler(true)); startGrizzly(ctrl); ConnectorHandler connection = ctrl.acquireConnectorHandler(Protocol.TCP); connection.connect(new InetSocketAddress(host, port), new CustomCallbackHandler(connection)); connection.write(giantByteBuffer, false); // continue, while ByteBuffer will be writen asynchronously
  • 26. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 2 – Client: asynchronous mode (cont.) public class CustomCallbackHandler implements CallbackHandler<Context> { private ConnectorHandler connection; CustomCallbackHandler(ConnectorHandler connection){ this.connection = connection; } public void onWrite(IOEvent<Context> ioEvent) { connection.write(giantByteBuffer, false); if (!giantByteBuffer.hasRemaining()) { notifyAsyncWriteCompleted(); } } // Skip other CallbackHandler methods implementation... }
  • 27. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ConnectorHandler Client side connection unit, which implements basic I/O functionality: connect, read, write, close. Provides possibility of working in both synchronous and asynchronous modes (e.g. blocking or non blocking read/write). Asynchronous operation execution could be controlled by CallbackHandler with user custom code, or automatically by framework, using asynchronous read and write queues. Grizzly Framework contains default TCP,TLS UDP ConnectorHandler implementations.
  • 28. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> CallbackHandler Handles client side asynchronous I/O operations: connect, read, write. Corresponding CallbackHandler method will be called, when NIO channel is ready to perform the operation. Asynchronous events could be either processed inside CallbackHandler or propagated to a ProtocolChain. When processing asynchronous event inside CallbackHandler, be careful with SelectionKey interests registration. public void onConnect(IOEvent<E> ioEvent); public void onRead(IOEvent<E> ioEvent); public void onWrite(IOEvent<E> ioEvent);
  • 29. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Client and server side logic unification There are use cases, where both client and server sides should process requests similar way, for example CORBA or SIP. CallbackHandler are able to redirect a request processing to a ProtocolChain, so high level protocol logic, which is implemented in ProtocolChain filters will be accessible from client side API too.
  • 30. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ConnectorHandler ConnectorHandler CallbackHandler CallbackHandler Controller Controller Context Context ProtocolChain ProtocolChain ReadFilter ReadFilter LogFilter LogFilter
  • 31. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 3 – Client and server side logic unification public class CallbackHandlerToProtocolChain implements CallbackHandler<Context> { public void onRead(IOEvent<Context> context) { Context context = ioEvent.attachment(); context.getProtocolChain().execute(context); } // Skip other CallbackHandler methods implementation... }
  • 32. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 33. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Asynchronous write queue Provides user the easy way for sending data asynchronously and be notified, when operation will be either successfully completed or interrupted due to I/O error. Asynchronous write queue is thread safe. Data from different buffers will never be intermixed. Asynchronous write queue processors provide easy and pluggable way to compress, encrypt output data just before it will be sent over NIO channel. Grizzly Framework has default asynchronous write queue implementation for UDP and TCP protocols.
  • 34. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Asynchronous read queue Provides user the easy way for receiving data asynchronously and be notified, when operation will be either successfully completed or interrupted due to I/O error. Asynchronous read queue is thread safe. Next buffer will start to fill up only after current one will be completed. Asynchronous read queue processors provide easy and pluggable way to decompress, decrypt input data just after it was read from NIO channel.
  • 35. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Asynchronous read queue (cont.) Asynchronous read conditions provide user possibility to set custom “read complete” condition. By default asynchronous read will consider completed, when input ByteBuffer will become full. Grizzly Framework has default asynchronous read queue implementation for UDP and TCP protocols.
  • 36. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 6 – Asynchronous queue: write class AsyncQueueEchoFilter implements ProtocolFilter { public boolean execute(Context ctx) throws IOException { ByteBuffer buffer = getByteBuffer(ctx); // we pass null for callback notificator and // data processor. Last true parameter means buffer // will be cloned before added to the queue ctx.getAsyncQueueWritable().writeToAsyncQueue( buffer, null, null, true); } public boolean postExecute(Context ctx) throws IOException { return true; } }
  • 37. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 6 – Asynchronous queue: read ConnectorHandler connection = ctrl.acquireConnectorHandler(Protocol.TCP); connection.connect(new InetSocketAddress(host, port)); // request notification, when byte buffer will be full AsyncReadCallbackHandler notificationHandler = new CustomAsyncReadCallbackHandler(); connection.readFromAsyncQueue(bigByteBuffer, notifiicationHandler);
  • 38. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example 6 – Asynchronous queue: read (cont.) class CustomAsyncReadCallbackHandler implements AsyncReadCallbackHandler { public void onReadCompleted(SelectionKey key, SocketAddress srcAddress, ByteBuffer buffer) { processReadData(buffer); } public void onIOException(IOException ioException, ... } }
  • 39. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 40. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Synchronous Read and Write There is some situations where registering a Channel on more than one Selector improve performance: • Instead of registering a SelectionKey using the Selector used by the acceptor thread (the Selector than handled the OP_ACCEPT), get a Selector from a pool and try to read/write more bytes using it. readSelector = SelectorFactory.getSelector(); tmpKey = socketChannel.register (readSelector,SelectionKey.OP_READ) tmpKey.interestOps(tmpKey.interestOps() | SelectionKey.OP_READ); int code = readSelector.select(readTimeout); tmpKey.interestOps(tmpKey.interestOps() & (~SelectionKey.OP_READ)); while (count > 0){ count = socketChannel.read(byteBuffer); }
  • 41. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Synchronous Read and Write (Con't) Grizzly offers two utility classes for supporting synchronous read and write: • com.sun.grizzly.util.InputReader { InputReader syncReader = new InputReader(); // Block at most 5 seconds. syncReader.read(bb,5,TimeUnits.SECONDS); } • com.sun.grizzly.util.OutputWriter { // Block at most 5 seconds. OutputWriter.write(bb,5,TimeUnits.SECONDS); }
  • 42. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 43. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ThreadAttachment Between OP_READ and OP_WRITE, some protocol needs to keep some states (remaining bytes inside a byte buffer, attributes, etc.). In Grizzly, the default is to associate one Byte Buffer per Thread. That means a byte buffer cannot be cached as its Thread can always be re-used for another transaction. To persist the byte buffer amongs transactions, a Thread Attachment can be used: { WorkerThread wt = (WorkerThread)Thread.currentThread(); ThreadAttachment = wt.detach(); }
  • 44. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> ThreadAttachment (Con't) What happens is internally, all attributes associated with the WorkerThread are 'detached', and new instance recreated • Warning: A new ByteBuffer will be created!! The ThreadAttachment can be attached to a SelectionKey, and next time an OP_READ/OP_WRITE happens, you are guarantee the value will be re-used. { ta.setAttribute(keepAliveCount,10); ctx.getSelectionKey().attach(ta); ... TheadAttachment ta = (ThreadAttachment) ctx.getSelectionKey().attachment(); int keepAliveCount = (int)ta.getAttibute(); }
  • 45. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 46. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> HTTP modules The Grizzly Framework also have an HTTP framework that can be used to build Web Server • This is what GlassFish™ v1|2|3 build on top of. • More specialized modules are also available like Comet (Async HTTP). Simple interface to allow customization of the HTTP Protocol • GrizzlyRequest: A utility class to manipulate the HTTP protocol request. • GrizzlyResponse: A utility class to manipulate the HTTP protocol response. • GrizzlyAdapter: A utility class to manipulate the HTTP request/response object.
  • 47. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> HTTP protocol module (Con't) public class FileAdapter extends GrizzlyAdapter{ public void service(GrizzlyRequest req, GrizzlyResponse res){ String fileUri = req.getRequestURI(); FileChannel file = ....; fileChannel.transferTo(...); res.flush(); res.close(); } }
  • 48. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example: Jersey (jersey.dev.java.net). Jersey is the open source JAX-RS (Java Specification Request (JSR) 311) Reference Implementation for building RESTful Web services. By default, Jersey ship with support for Grizzly HTTP WebServer: • Implement the GrizzlyAdapter interface, use the GrizzlyRequest and Response object to handle the http protocol. In less than 20 minutes, Jersey was running with Grizzly!
  • 49. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example: Jersey (jersey.dev.java.net). public void service(GrizzlyRequest request, GrizzlyResponse response) { try { requestAdaptor = new GrizzlyRequestAdaptor( application.getMessageBodyContext(),request); responseAdaptor = new GrizzlyResponseAdaptor(response, application.getMessageBodyContext(), requestAdaptor); application.handleRequest(requestAdaptor, responseAdaptor);
  • 50. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Example: Jersey (Con't). protected void commitStatusAndHeaders() throws IOException { response.setStatus(this.getStatus()); for (Map.Entry<String, List<Object>> e : this.getHttpHeaders().entrySet()) { String key = e.getKey(); for (Object value: e.getValue()) { response.addHeader(key,value.toString()); } } String contentType = response.getHeader("Content-Type"); if (contentType != null) { response.setContentType(contentType); } }
  • 51. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> SelectorHandler SelectorHandler UDPSelectorHandler UDPSelectorHandler TCPSelectorHandler TCPSelectorHandler GlassFish v3 GlassFish v3 Controller Controller Pipeline Pipeline Context Context ProtocolChain ProtocolChain ReadFilter ReadFilter V3HttpParser V3HttpParser Servlet Container Servlet Container JRuby Container JRuby Container Grails Container Grails Container GrizzlyAdapter
  • 52. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> GlassFish v3 Demo
  • 53. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 54. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> NIO.2: Asynchronous I/O (JSR-203) NIO.2 is: • An API for asynchronous (as opposed to polled, non-blocking) I/O operations on both sockets and files. • The completion of the socket-channel functionality defined in JSR- 51, including the addition of support for binding, option configuration, and multicast datagrams. Starting in Grizzly 1.8.0, Asynchronous I/O is supported if you are running JDK version 7. You can easily mix non blocking with asynchronous I/O.
  • 55. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> NIO.2: Asynchronous I/O (JDK version 7.0) Grizzly supports NIO.2. Switching an application from using NIO.1 to NIO.2 is quite simple Bytes are delivered the same way, independently of NIO.1 or NIO.2: // Instead of TCPSelectorHandler controller.addIOHandler(new TCPAIOHandler()); //Instead of using ReadFilter AIOReadFilter readFilter = new AIOReadFilter(); protocolChain.addFilter(readFilter);
  • 56. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Agenda What is the Grizzly Framework The Server Components The Client Components Asynchronous Read and Write Queue Synchronous Read and Write using Temporary Selectors Persisting your data using ThreadAttachment HTTP Modules NIO.2 Support Summary
  • 57. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Summary Project Grizzly is a simple NIO framework. • With less than 50 lines, you can create power client or server side components. • Support NIO.1 and NIO.2. • You have control of the framework: everything can be customized! HTTP module makes it easy to build WebServer extension • Jersey, blog-city.com, Netbeans™ software, Ning, Red Hat REST Impl, RESTlet, GlassFish v1|2|3, Sailfin Load Balancer, JXTA, and many many more! Healthy community: get a response in less than 2 hours :-)!
  • 58. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> For More Information Grizzly: • http://grizzly.dev.java.net Jeanfrancois's blog: • http://weblogs.java.net/blog/jfarcand/ Alexey's blog: • http://blogs.sun.com/oleksiys/ Project Grizzly mailing lists, • dev@grizzly.dev.java.net • users@dev.grizzly.java.net
  • 59. 2008 JavaOneSM Conference | java.sun.com/javaone | <number> Jeanfrancois Arcand Oleksiy Stashok, Sun Microsystems TS-4883