Simple Grizzly 2.0

From time to time I see mails from people, who are looking forward to use Grizzly 2.0, but don't know where to start from. Currently we don't have much documentation on that and mainly I have to point people to unit tests code to give them some idea about Grizzly 2.0

Here I'll try to show the very simple scenario, how Grizzly 2.0 could be used. I will not show FilterChains, CallbackHandlers... nothing. I will show how Grizzly 2.0 could be used instead of plain Sockets. So, even if you have just basic understanding how Java Sockets work - you'll understand how Grizzly 2.0 could be used instead.

Why use Grizzly 2.0 instead of Sockets?

  1. Grizzly 2.0 provides very simple API, based on Future, CompletionHandler API, which lets you work asynchronously (in non-blocking manner) with connections.
  2. Grizzly 2.0 uses asynchronous read and write queues, which means you don't have to care about simultaneous reads and writes from different threads. 
  3. Understand better Grizzly 2.0 API, which will help you to write more complex Grizzly based systems.

 

Client, based on Grizzly 2.0:

TCPNIOTransport transport = TransportFactory.getInstance().createTCPTransport();
// Enable standalone mode
transport.setProcessorSelector(new NullProcessorSelector());
// Start transport
transport.start();
// Connect to a server ConnectFuture connectFuture = transport.connect(host, port);
Connection connection = connectFuture.get();
// Initialize data, which will be sent
MemoryManager memoryManager = transport.getMemeoryManager();
Buffer buffer = MemoryUtils.wrap(memoryManager, "Hello World!", Charset.forName("UTF-8"));
// Send data (async write queue will be used underneath)
Future writeFuture = connection.write(buffer);
// We can wait until write will be completed
writeFuture.get(timeout, TimeUnit.SECONDS);
// Initialize receive buffer
Buffer recieveBuffer = memoryManager.allocate(SIZE);
//Read response
Future readFuture = connection.read(receiveBuffer);
/\*
\* Tip. If we need to read whole buffer (readFully), use following:
\* Future readFuture = connection.read(receiveBuffer, null, null, MinBufferSizeCondition(receiveBuffer.remaining());
\*/

readFuture.get(timeout, TimeUnit.SECONDS);

......................
connection.close();

 

Socket-like server, based on Grizzly:

TCPNIOTransport transport = TransportFactory.getInstance().createTCPTransport();
// Enable standalone mode
transport.setProcessorSelector(new NullProcessorSelector());
// Bind transport to listen on specific port
transport.bind(PORT);
// Start transport
transport.start();
while(!transport.isStopped()) {
     // Accept new client connection
     AcceptFuture acceptFuture = transport.accept();
     Connection connection = acceptFuture.get();
     // Process accepted connection (may be in separate thread)
     processAcceptedConnection(connection);
}

From the samples above we can see, that Grizzly 2.0 provides API similar to Java Socket, but it lets us work with connections asynchronously! May be Grizzly need more initialization lines than Sockets

  1.  TCPNIOTransport transport = TransportFactory.getInstance().createTCPTransport();
  2.  transport.setProcessorSelector(new NullProcessorSelector();  


The "1" just initializes the TCP transport.
The "2" configures transports, so it will be used in standalone mode. FilterChains, CallbackHandlers and other possible Processors will not be invoked.

And again, if you want working example, how Grizzly could be used in standalone mode (Socket-like manner), please take a look here

Comments:

[Trackback] We got attacked by a community boost in January, which resulted in tons of contributions and improvements to the monster. This is for sure our biggest community release...Here comes 1.9.5!

Posted by Jean-Francois Arcand's Blog on February 03, 2009 at 09:10 AM CET #

Can notificationObject as Instance of Future in Grizzly 2.0?

Grizzly 2.0 framework( grizzly-framework) API is based on java.util.concurrent.Future. In release 2.0.0-M3, only one class ( com.sun.grizzly.utils.conditions.DefaultConditionListener) is referring java.util.concurrent.Callable. Here is that method of notifyListenerObject ( Object notificationObject) .

Since this is only class referring Callable in whole grizzly-framework API, notificationObject will never be an instance of Callable, so nofiyListernObject method doesn’t need care about noticationObject as instance of Callable

public static final void notifyListenerObject(Object notificationObject) {
if (notificationObject instanceof CountDownLatch) {
….
} else if (notificationObject instanceof Callable) {
…….
}
} else if (notificationObject instanceof Runnable) {
………
} else {
synchronized (notificationObject) {
notificationObject.notify();
}
}
}

Posted by Ming Qin on June 15, 2009 at 03:49 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

oleksiys

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today