The core of the Transport API:
Channel interface
The class diagram indicates that the channel contains the pipeline and config interfaces, as described in the previous section of pipeline.
The channel is thread-safe, which means operating the same channel in a multi-line environment with no data problems.
Final Channel channel = null;final bytebuf buf = Unpooled.copiedbuffer ("Your Data", charsetutil.utf_8); #1Runnable writer = new Runnable () {//#2 @Overridepublic void Run () {Channel.write (Buf.duplicate ());}}; Executor Executor = Executors.newcachedthreadpool (); #3//write in one threadexecutor.execute (writer); #4//write in another threadexecutor.execute (writer); #5 #1 Create BYTEBUF, holds data to write#2 create Runnable which writes data to channel#3 obtain reference to the Executor which uses threads to execute tasks#4 Hand over write task to Executor for execution in thread#5 Hand over anothe R Write task to executor for execution in thread
This method guarantees the messages is written in the same order as you passed them to the Write method . (This method guarantees that the order in which information is written to the channel is consistent with the order in which the Write method is called.) )
Transports:
Each mode of transmission has a corresponding EventLoop:
The last Threadperchanneleventloop is the event listener for Oio (Old-io).
nio–nonblocking I/O: the NIO transport is currently the most used. It provides a full asynchronous implementation of all I/O operations by using the selector-based approach that ' s included In Java since Java 1.4 and the NIO subsystem. (NiO is the most widely used transmission method, and it provides a fully asynchronous implementation of IO operations, based on the selector approach.) )
Real no-blocking (a very important passage):
One feature that offers only the NIO transport on the moment is called "zero-file-copy". This feature allows-quickly and efficiently transfer content from your file system. The feature provides a by transfer the bytes from the file system to the Networkstack without copying the bytes from T He kernel space to the user space. Be aware, that isn't all operation systems support this. Refer to operating system's documentation to find out if it ' s supported. Also, be aware, then you'll have only been able to benefit from this if you don ' t use any encryption/compression of the data. Otherwise it would need to copy the bytes first to the user space to do the actual work, so only transferring the raw conte NT of a file makes use of this feature. What actually would are to? pre-encrypt "a file before transfer it. One application that can really make use of the is a FTP or HTTP server that downloads big files. The next transport I ' ll discuss is the OIO transport, which provides a blocking transport.
Presumably: "Zero-file-copy" is a feature that is only available to NiO in the way it is transmitted. This feature allows you to transfer content from the file system in a fast and efficient manner. This feature allows byte content in a local file to be transmitted over the network without being copied from the kernel space to the user space. Please note that not all operating systems support this feature. Please refer to the specific operating system documentation to see how this feature is implemented. Also note that if you want to use this feature, do not make any codec operations on the source data. Otherwise, the source data will be copied from the kernel space to the user space to do the codec operation, so only the source data will be sent to use this feature. The more practical point is to save the source data in advance in encrypted form. Applications that download large files based on FTP or HTTP services can benefit from this feature.
Oio–old blocking I/O: the OIO transport is a compromise in Netty. It builds on the known unified API but isn ' t asynchronous by nature because it uses the blocking java.net implementations Under the hood. At the first glance, this transport is not a look for useful to you, but it had its use cases.
When using these classes, you usually has one thread that handles the acceptance of new sockets (Server-side) and then CR Eates a new thread for each accepted connection to serve the traffic over the socket. This is needed as every I/O operation on the socket could block at any time.If you share the same thread over more than one connection (socket), this could leads to a situation where blocking an Operation could block all and sockets from doing their work.
knowing that operations could block, you could start to wonder what Netty uses it while still providing the same of the "the Bui" Lding APIs. Here Netty makes use of the so_timeout so can set on a socket. This timeout is specifies the maximum number of milliseconds to wait for an I/O operation to complete. If the operation doesn ' t complete within the specified timeout, a sockettimeoutexception is thrown. Netty catches this sockettimeoutexception and moves in with its work. Then on the next EventLoop run, it tries again. Unfortunately, the the only-to-do and still confirm the inner working of Netty. The problem with this approach are that firing the sockettimeoutexception isn ' t free, as it needs to fill the stracktrace, and so on.Local–in VM Transport:
Netty contains the so-called local transport. This transport implementation can is used to communicate within a VM and still use the same API for you ' re used to. The transport is fully asynchronous as NIO. Embedded Transport:netty also includes the Embedded transport. This isn ' t a real transport if you compare it with the others listed previously, but it's included to complete this sect Ion. If It's not a real transport, what can it is used for? The embedded transport allows you and interact with your different Channelhandler implementation more easily. It's also easy-to-embed Channelhandler instances in the other channelhandlers and use them like a helper class. This was often used in test cases to test a specific channelhandler implementation, but can also being used to re-use some Cha Nnelhandler in your own Channelhandler without event extend it. For the purpose, it comes with a concrete Channel implementation.