The abstractioacceptor class inherits from the abstractioservice base class and implements the ioacceptor interface. Its main member variable is the local binding address.
Private final list <socketaddress> defaultlocaladdresses =
New arraylist <socketaddress> ();
Private final list <socketaddress> unmodifiabledefalocallocaladdresses =
Collections. unmodifiablelist (defaultlocaladdresses );
Private final set <socketaddress> boundaddresses =
New hashset <socketaddress> ();
When calling the bind or unbind method, you must first obtain the bindlock. The specific binding operation is still implemented in the bind0 method. Once the binding is successful, serviceactivated event will be sent to the service listener. Similarly, unbinding is implemented in the unbind0 method. Once the binding is unbound, servicedeactivated event is sent to the service listener ).
The abstractioconnector class inherits from the abstractioservice base class and implements the ioconnect interface. The connection timeout check interval is 50 milliseconds by default, and the timeout time is 1 minute by default. You can configure it on your own. The important method in this class is the connect method. Specifically, the connection logic is called to achieve connect0,
Protected abstract connectfuture connect0 (socketaddress remoteaddress,
Socketaddress localaddress, iosessioninitializer <? Extends connectfuture> sessioninitializer );
Abstractioconnector adds a function at the end of Session Initialization Based on abstractioservice, that is, adding a listener to terminate the session immediately when the connection request is canceled.
Protected final void finishsessioninitialization0 (
Final iosession session, iofuture future ){
// In case that connectfuture. Cancel () is invoked before
// Setsession () is invoked, add a listener that closes
// Connection immediately on cancellation.
Future. addlistener (New iofuturelistener <connectfuture> (){
Public void operationcomplete (connectfuture future ){
If (future. iscanceled ()){
Session. Close ();
}
}
});
}
Next, let's take a look at the basic implementation class simpleioprocessorpool of an ioprocessor interface. Its generic parameter is a subclass of abstractiosession, indicating the specific session type managed by this processor. This class also implements pooling, which distributes multiple iosessions to multiple ioprocessor for management. The following is an example in the document:
// Create a shared pool.
Simpleioprocessorpool <niosession> pool =
New simpleioprocessorpool <niosession> (nioprocessor. Class, 16 );
// Create two services that share the same pool.
Socketacceptor acceptor = new niosocketacceptor (pool );
Socketconnector conne= new niosocketconnector (pool );
// Release related resources.
Connector. Dispose ();
Acceptor. Dispose ();
Pool. Dispose ();
The following member variables are related to the processor pool:
Private Static final int default_size = runtime. getruntime (). availableprocessors () + 1; // processing pool size. The default value is the number of processors + 1, which facilitates distributed processing of multiple cores.
Private Final ioprocessor <t> [] pool; // ioprocessor pool
Private Final atomicinteger processordistributor = new atomicinteger ();
The construction process of the processor pool. Three constructor types are available to construct a processor:
- The constructor with the executorservice parameter.
- The constructor with the executor parameter.
- Default constructor
Pool = new ioprocessor [size]; // build the pool
Boolean success = false;
Try {
For (INT I = 0; I <pool. length; I ++ ){
Ioprocessor <t> processor = NULL;
// Three constructor types are available to construct a processor.
Try {
Try {
Processor = processortype. getconstructor (executorservice. Class). newinstance (Executor );
} Catch (nosuchmethodexception e ){
// To the next step
}
If (processor = NULL ){
Try {
Processor = processortype. getconstructor (Executor. Class). newinstance (Executor );
} Catch (nosuchmethodexception e ){
// To the next step
}
}
If (processor = NULL ){
Try {
Processor = processortype. getconstructor (). newinstance ();
} Catch (nosuchmethodexception e ){
// To the next step
}
}
} Catch (runtimeexception e ){
Throw E;
} Catch (exception e ){
Throw new runtimeioexception (
"Failed to create a new instance of" + processortype. getname (), e );
}
Pool [I] = processor;
}
Success = true;
} Finally {
If (! Success ){
Dispose ();
}
}
The process of allocating a processor from the processor pool. Note thatOneProcessorYes. You can manage multipleSessionOf.
Private ioprocessor <t> getprocessor (T session)
{// Return the processor of the session. If no processor is allocated,
Ioprocessor <t> P = (ioprocessor <t>) Session. getattribute (processor); // check whether the corresponding processor is saved in the session attribute.
If (P = NULL)
{// The processor has not been assigned for this session
P = nextprocessor (); // obtain a processor from the pool
Ioprocessor <t> oldp =
(Ioprocessor <t>) Session. setattributeifabsent (processor, P );
If (oldp! = NULL)
{// Original Processor
P = oldp;
}
}
Return P;
}
Private ioprocessor <t> nextprocessor ()
{// Allocate a processor from the pool
Checkdisposal ();
Return pool [math. Abs (processordistributor. getandincrement () % pool. Length];
}
Author: phinecos)
Source: http://phinecos.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it, but please keep this statement and provide the original article connection clearly on the article page.
Author: Dongting sangren
Source: http://phinecos.cnblogs.com/
This blog complies with the Creative Commons Attribution 3.0 License. If it is used for non-commercial purposes, you can reprint it freely, but please keep the original author information and article URL.