Grizzly Study Notes (1)

Source: Internet
Author: User

Grizzly is a network communication framework based on java nio. According to tests by some online users, grizzly has better performance.
So I decided to learn about this framework.
Grizzly currently has two versions: 1.9.x and 2. x. currently, except 2. x there are some other guides. There are no documents in Versions 1.9 and earlier, and there is much less online information than MINA.
What's even more incredible is that the API of each version of Grizzly has changed a lot (I have never heard of it), which is very incompatible and inconvenient to use. A Grizzly1.3 code was called some time ago. Frankly speaking, the Code is also messy because it is not well written. The new version of code has changed a lot in terms of code style, which is why I decided to learn about it.
Enter the subject.
 
After a morning call, I finally wrote a demo without a document. I can only refer to his test case and put the comments in the code.
 
Package org. guojje. grizzly;
 
Import java. io. IOException;
 
Import com. sun. grizzly. Controller;
Import com. sun. grizzly. DefaultProtocolChain;
Import com. sun. grizzly. ProtocolFilter;
Import com. sun. grizzly. TCPSelectorHandler;
Import com. sun. grizzly. DefaultProtocolChain. EventHandler;
Import com. sun. grizzly. DefaultProtocolChain. Phase;
Import com. sun. grizzly. filter. EchoFilter;
Import com. sun. grizzly. filter. LogFilter;
Import com. sun. grizzly. filter. ReadFilter;
Import com. sun. grizzly. util. WorkerThreadImpl;
 
Public class Server {
Public static void main (String args []) throws IOException {
// Control center,
Controller controller = new Controller ();
// Add SelectorHandler. If no SelectorHandler is added
// TCPSelectorHandler is added by default.

TCPSelectorHandler tcpHandler = new TCPSelectorHandler ();
TcpHandler. setPort (1900 );
Controller. setSelectorHandler (tcpHandler );

// Create a protocol Chain,
DefaultProtocolChain protocolChain = new DefaultProtocolChain ();
 
// ReadFilter must be added. Otherwise, data cannot be read.
ProtocolChain. addFilter (new ReadFilter ());
ProtocolChain. addFilter (new LogFilter ());
// Add the echo Function
ProtocolChain. addFilter (new EchoFilter ());
// Note that the ReadFilter and EchoFilter sequence cannot be reversed. Only when the data is read first can the echo be performed.
// The filter is executed in the queue order. For another example, LogFilter must be placed before EchoFilter,
// Otherwise, if there is no data in the Buffer, LogFilter will have no data to LOG.
// I don't know if it is a design problem.
 

// Add the Protocol Chain
Controller. getProtocolChainInstanceHandler (). offer (protocolChain );
// Start Controller
Controller. start ();
}
}
Start the Server, start the client, and view the console output. The communication is successful.
Package org. guojje. grizzly;
 
Import java. io. IOException;
Import java.net. InetAddress;
Import java.net. InetSocketAddress;
Import java.net. UnknownHostException;
Import java. nio. ByteBuffer;
 
Import com. sun. grizzly. TCPConnectorHandler;
Import com. sun. grizzly. util. OutputWriter;
 
Public class Client {

Public static void main (String [] args) throws UnknownHostException, IOException {

TCPConnectorHandler tch = new TCPConnectorHandler ();
Tch. connect (new InetSocketAddress (InetAddress. getLocalHost (), 1900 ));
OutputWriter. flushChannel (tch. getUnderlyingChannel (), ByteBuffer. wrap ("Hello". getBytes ()));
ByteBuffer bb = ByteBuffer. allocate (100 );
Tch. read (bb, true );
System. out. println (new String (bb. array (), 0, bb. remaining ()));
}
}
 
This article is from the "no thieves in the world" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.