Appendix B. Selectable Channels SPI

If you build it, he will come.

An Iowa Cornfield

The selectable-channel architecture, like several other components of the Java platform, is pluggable by means of a Service Provider Interface (SPI). Chapter 6 showed how to use the pluggable Charset SPI, and the Channel SPI works essentially the same way. Channel, Selector, and even SelectionKey implementations can be quite complex and are necessarily operating system-dependent. A sample channel implementation is beyond the scope of this book. This appendix only summarizes the SPI at a high level. If you are setting out to create your own custom channel implementation, you will require more detailed information than can be presented here.

As we saw in Section 6.3, services are facilitated by a provider class instantiated by the low-level services mechanism. For channels, the base provider class is java.nio.channels.spi.SelectorProvider. Note that this isn’t named ChannelProvider. This SPI applies only to selectable channels, not all channel types. There’s a tight dependency between selectable channels and related selectors (and selection keys, which associate one with the other). Channels and selectors from different providers will not work together.

package java.nio.channels.spi; public abstract class SelectorProvider { public static SelectorProvider provider( ) // The following methods all throw IOException public abstract AbstractSelector openSelector( ) public abstract ServerSocketChannel ...

Get Java NIO now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.