To build a server, you must select an ioacceptor interface implementation class. The ioacceptor interface is basically named by the accpet () method. It is mainly responsible for creating connections between a client and the service. Then, the service can accept requests from these connections. Sometimes, we call it a "server" interface. Because we need to provide multiple transmission protocols (TCP/UDP /...), therefore, we provide multiple implementations of this interface, and we have provided comprehensive coverage. Basically, you do not need to implement a new one yourself. We have many such implementation classes:
- Niosocketacceptor: non-blocking implementation of TCP-based ioacceptor
- Niodatagramacceptor: non-blocking UDP-based ioacceptor implementation
- Aprsocketacceptor: The implementation of TCP ioacceptor Based on Apr.
- Vmpipesocketacceptor: ioacceptor Implementation Based on Virtual Machine pipeline Communication
You only need to select a class diagram that meets your needs. The following is the class diagram of ioacceptor and its subclass and sub-interface:
To create a service, you must first select an ioacceptor type that you want to instantiate. In fact, you have decided before that, because it depends on the communication protocol you want to use. Let's see how an instance works:
public TcpServer() throws IOException { // Create a TCP acceptor IoAcceptor acceptor = new NioSocketAcceptor(); // Associate the acceptor to an IoHandler instance (your application) acceptor.setHandler(this); // Bind : this will start the server... acceptor.bind(new InetSocketAddress(PORT)); System.out.println("Server started...");}
In this way, you have completed the creation process of a TCP server. To create a UDP server, you only need to change the first line of code:
...// Create an UDP acceptorIoAcceptor acceptor = new NioDatagramAcceptor();...
Stop Service
The service can be stopped by calling the dispose () method, but it must be stopped only after all the pending sessions are processed.
// Stop the service, waiting for the pending sessions to be inactiveacceptor.dispose();
Of course, you can also pass a Boolean variable to this method to determine whether to stop the service after every running thread is fully executed.
// Stop the service, waiting for the processing session to be properly completedacceptor.dispose( true );
Server Status
You can use any of the following methods to obtain the ioservice status:
- Isactive (): returns true if the service can receive incoming requests.
- Isdisposing (): If the method dispose () is called, true is returned, but we do not know whether the service has actually stopped (some sessions may still be processed)
- Isdisposed (): If the method dispose (Boolean) is called and the executing thread has completed the execution, true is returned.
After Service instantiation, you only need to call the sethandler (iohandler) and gethandler () Methods to set or obtain the iohandler management filter chain associated with the service.
If you want to manage the filter chain, you only need to call the getfilterchain () method, as shown in the following example:
// Add a logger filterDefaultIoFilterChainBuilder chain = acceptor.getFilterChain();chain.addLast("logger", new LoggingFilter());
You can also create the chain before and set it into the service:
You can also create a filter chain before setting it to the Service, as shown below:
// Add a logger filterDefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();chain.addLast("logger", new LoggingFilter());// And inject the created chain builder in the serviceacceptor.setFilterChainBuilder(chain);
(Supplement: as the manual is constantly being updated, it may change. The last update date is. It is strongly recommended that you read the original document)