Apache Mina study notes (3) - IoService

黄舟
Release: 2023-03-04 22:18:01
Original
1323 people have browsed it

In this chapter, we describe MINA's IoService - a basic class that provides all I/O services on the server and client. It is used to send and receive messages with the peer, manage seions, connections, etc.

It is an interface implemented by IoAcceptor on the server side and IoConnector on the client side.

We will introduce this interface in the following parts:

IoService Introduction
IoService Details
IoAcceptor
IoConnector

IoService Introduction

IoService provides basic I/O services in the Mina framework and manages I/O Sessions. It is a very critical part of Mina.

Apache Mina study notes (3) - IoService

As mentioned in the above figure, IoService has many responsibilities:

Session management
Filter chain management
Handler call
Statistics management
Listening management
Communication management

Methods in this interface:

getTransportMetadata();

When IoAcceptor or IoConnector is running, this method returns Transmitted metadata, usually including name (nio, apr, txtx), connection type (connection-oriented or connectionless).

addListener

Allows adding an IoServiceListener to listen to specified events

removeListener

Removes a specified IoserviceListener

isDisposing

If the service is Disposed, then return the status of the service

isDisposed

If the service has been Disposed, then return the status of the service

dispose

Used to release all resources requested by the service. Users need to use the above two methods to first check whether the service has been completely disposed. When you want to shut down the service, be sure to call this method

getHandler

Return the method associated with the service

setHandler

Set the IoHandler to solve the problem that the server gets request, the Handler contains your program logic

getSessionConfig

Returns the session configuration

getFilterChainBuilder

Returns to the Filter chain builder, when filtering needs to be added When, this method needs to be called.

IoService Details

Ioservice This interface is implemented by two important classes in MINA: IoAcceptor and IoConnector. On the server side, you need to choose IoAcceptor, and on the client side, you need to choose IoConnector.

IoAcceptor

In MINA, there are some already implemented classes:

NioSocketAcceptor : the non-blocking Socket transport IoAcceptor
NioDatagramAcceptor : the non-blocking UDP transport IoAcceptor
AprSocketAcceptor : the blocking Socket transport IoAcceptor, based on APR
VmPipeSocketAcceptor : the in-VM IoAcceptor

Choose the IoAcceptor you need.

The following figure is the IoAcceptor class and interface

Apache Mina study notes (3) - IoService

IoConnector

NioSocketConnector : the non-blocking Socket transport IoConnector
NioDatagramConnector : the non-blocking UDP transport IoConnector
AprSocketConnector : the blocking Socket transport IoConnector, based on APR
ProxyConnector : a IoConnector providing proxy support
SerialConnector : a IoConnector for a serial transport
VmPipeConnector : the in-VM IoConnector

Apache Mina study notes (3) - IoService


##The following is an example as a summary:


For example, when you want to create a TCP server, you You can write code similar to the following

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...");  
}
Copy after login

In this way, a Tcp Server is created. If you want to create a UDP server, you can change it to IoAcceptor acceptor = new NioDatagramAcceptor();


To release For this connection, you can call

acceptor.dispose(true)
Copy after login

to add a filter chain and add it to the service

// Add a logger filter  
DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();  
chain.addLast("logger", new LoggingFilter());  
  
// And inject the created chain builder in the service  
acceptor.setFilterChainBuilder(chain);
Copy after login

. The above is the content of Apache Mina study notes (3) - IoService. For more related content, please Follow the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template