Mina2.0 Framework Source Code Analysis (III.)

Source: Internet
Author: User

The Abstractioacceptor class inherits from the Abstractioservice base class and implements the Ioacceptor interface, whose primary member variable is the locally bound address.

Private Final New Arraylist<socketaddress>(); Private Final list<socketaddress> unmodifiabledefaultlocaladdresses = collections.unmodifiablelist ( defaultlocaladdresses); Private Final New Hashset<socketaddress> ();

The bind or Unbind method needs to get the binding lock Bindlock first, and the specific binding operation is implemented in the Bind0 method. Once the bindings are successful, service-activation events (serviceactivated) are issued to the service listener, and the unbind is implemented specifically in the Unbind0 method. Once the binding is successful, a service activation event (servicedeactivated) is issued to the service listener.

The Abstractioconnector class inherits from the Abstractioservice base class, implements the Ioconnect interface, the connection time-out check interval is 50 milliseconds by default, and the time-out defaults to 1 minutes, which the user can configure themselves. The important method in this class is the Connect method, which invokes the specific connection logic implementation connect0,

protected Abstract connectfuture connect0 (socketaddress remoteaddress, socketaddress localaddress, Iosessioninitializer  Extends connectfuture> Sessioninitializer);

Abstractioconnector on the basis of Abstractioservice, added a function at the end of session initialization, which is to join a listener and end the session as soon as the connection request is canceled.

    protected Final voidFinishSessionInitialization0 (Finaliosession session, Iofuture Future) {        //In case the connectfuture.cancel () is invoked before//setsession () is invoked, add a listener that closes the//connection immediately on cancellation.Future.addlistener (NewIofuturelistener<connectfuture>() {             Public voidOperationcomplete (connectfuture future) {if(future.iscanceled ()) {session.close (); }            }        });}

Let's look at a basic implementation class Simpleioprocessorpool for the Ioprocessor interface, whose generic parameter is a subclass of Abstractiosession, representing the specific session type that this processor manages. And this class also implements pooling, which distributes multiple iosession to multiple ioprocessor to manage. Here is an example given in the documentation:

// Create a shared pool. New Simpleioprocessorpool<niosession> (nioprocessor.  Class, +);   // Create Both services that share the same pool. New  New  niosocketconnector (pool);   // Release related resources. connector.dispose (); Acceptor.dispose (); Pool.dispose ();

The following member variables are associated with the processor pool:

// processing pool size, default is processor number +1, easy to multi-core distribution processing Private Static Final int Default_size = Runtime.getruntime (). Availableprocessors () + 1; Private Final Ioprocessor<t>[] Pool; // Ioprocessor Pond Private Final New Atomicinteger ();

The construction process of the processor pool, where there are three constructors to choose from to construct a processor:

  1. The constructor with the parameter executorservice.
  2. A constructor with a parameter of executor.
  3. Default constructor
    Pool =NewIoprocessor[size];//Build Pool        BooleanSuccess =false; Try {             for(inti = 0; i < pool.length; i + +) {Ioprocessor<T> processor =NULL;            //There are three types of constructors to choose from 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) {Throwe; } Catch(Exception e) {Throw NewRuntimeioexception ("Failed to create a new instance of" +Processortype.getname (), E); } Pool[i]=processor; } Success=true; } finally {            if(!success)            {Dispose (); }        }

Assign a processor process from the processor pool, noting that a processor can manage multiple sessions at the same time .

//returns the processor to which the session is located, if not assigned, assigns aPrivateIoprocessor<t>Getprocessor (T session) {Ioprocessor<T> p = (ioprocessor<t>)        //see if the corresponding processor is saved in the session's propertiesSession.getattribute (PROCESSOR); //we haven't assigned processor for this session .        if(p = =NULL) {           //take a processor from the poolp =nextprocessor (); Ioprocessor<T> OLDP = (ioprocessor<t>) session.setattributeifabsent (PROCESSOR, p); //the original processor            if(OLDP! =NULL) {p=OLDP; }        }        returnp; }    //assign a processor from the pool    PrivateIoprocessor<t>nextprocessor () {checkdisposal (); returnPool[math.abs (processordistributor.getandincrement ())%Pool.length]; }    

Phinecos (The Cave of the Chamber scattered people)
Source:http://phinecos.cnblogs.com/
This article is copyright to the author and the blog Garden is shared, welcome reprint, but please keep this paragraph statement, and in the article page obvious location to the original link.

Mina2.0 Framework Source Code Analysis (III.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.