Several conclusions and arguments about Remoting

Source: Internet
Author: User

Overall:

There are two methods to activate Remoting: client activation and server activation, server activation means that the server is responsible for maintaining the creation and destruction of remote service objects, while client activation means that the client is responsible for the creation and activation of remote objects. In server activation mode, an object is created based on the request. Singleton is the object created for all requests, and SingleCall is the object created for each request. The object system created by SingleCall does not have a life cycle. For Singleton, you can manage the life cycle of remote service objects through programming or configuration. In the client-activated mode, the remote server creates a remote object client for each client to hold reference to this remote object. In short, the client activation time is when the client requests, and the time for the server to activate remote objects is when the object method is called.

1: SingleCall and Singleton

Conclusion: each time the user calls the server proxy, the server constructs a new object for each request, while Singleton creates an object for all requests.

Argument: we can create a Server Object and make it have an integer variable, assign a value of 1 to its constructor, and add the value of 1 to the method. For SingleCall, each request is always 2, while Singleton increases with the increase in the number of requests.

Code diagram:

Code Public class Test: externalbyrefobject, ITest
{
Private int Num = 0;
Public Test ()
{
Num = 1;
}

# Region ITest Member

Public string GetUserName (string Name)
{
// Return TestSingle. GetInstance (). GetUserName (Name );
Return string. Format ("UserName: {0}", Name );
}

Public int Cout ()
{
// Return TestSingle. GetInstance (). Cout ();
Num ++;
Return Num;
}

# Endregion
}

 

Deep Dive:

From this we can see that SingleCall is inherently capable of load balancing and can be used to process large-scale requests. But how can we make SingleCall have the capability of Singleton? Therefore, we need to understand the meaning of SingleCall. In fact, SingleCall only creates a new object upon each request, that is, the object you declare, but the container on the server is still one. At this time, we can create a single-piece object on the server for storage or computing. In this way, the advantages of SingleCall and Singleton can be taken into account. If you do not quite understand it, you can refer to the following code:

Code Public class TestSingle
{
Private static readonly object obj = new object ();
Private static TestSingle Instance;

Private int Num = 0;

Private TestSingle ()
{
Num = 1;
}

Public static TestSingle GetInstance ()
{
If (Instance = null)
Lock (obj)
{
If (Instance = null)
Instance = new TestSingle ();
}

Return Instance;
}

Public string GetUserName (string Name)
{
Return string. Format ("UserName: {0}", Name );
}

Public int Cout ()
{
Num ++;
Return Num;
}
}

 

2: client Activation

Conclusion: In client activation mode, the server creates a remote service object for each client.

Argument: Use the above example

Code diagram:

Code TcpChannel channel = new tcpchannels (61000 );
ChannelServices. RegisterChannel (channel );
RemotingConfiguration. ApplicationName = "RemoteServer ";
RemotingConfiguration. RegisterActivatedServiceType (typeof (ITest. Test ));

Object [] attrs = {new UrlAttribute ("tcp: // localhost: 61000/RemoteServer ")};
ObjectHandle handle = Activator. CreateInstance ("ITest ",
"ITest. Test", attrs );
ITest. Test obj = (ITest. Test) handle. Unwrap ();

Console. WriteLine (obj. Cout ());

 

Every time we run the client code, we always get 2

3: Lease

In Singleton mode activated by the server or in client activation mode, you can set the lifecycle of the server object as follows:

The LeaseTime option defines the maximum lease time of a remote object. The default value is 300 seconds.

RenewOnCallTime: the incremental lease time. The default value is 120 seconds.

The default time before the end of the SponsorshipTimeout call. The default value is 120 seconds.

LeaseManagerPollTime the initiator must return the extended lease time. The default value is 10 seconds.

That is to say, the remote service object will save the LeaseTime seconds on the server. If a request occurs within the LeaseTime seconds, the server's lifecycle will be extended by the RenewOnCallTime seconds.

Conclusion: The Remote Object activated by Singleton in server activation mode will survive LeaseTime seconds.

Argument: The above example is used.

 

Sample Code:

Code LifetimeServices. LeaseTime = TimeSpan. FromSeconds (1 );
LifetimeServices. RenewOnCallTime = TimeSpan. FromSeconds (1 );
LifetimeServices. LeaseManagerPollTime = TimeSpan. FromSeconds (1 );
LifetimeServices. inclusorshiptimeout = TimeSpan. FromSeconds (1 );

TcpServerChannel channel = new tcpserverchannels (61000 );
ChannelServices. RegisterChannel (channel, false );
RemotingConfiguration. RegisterWellKnownServiceType (typeof (ITest. Test), "SVRCALL. rem ",
WellKnownObjectMode. Singleton );

ITest. ITest test = (ITest. ITest) Activator. GetObject (typeof (ITest. ITest), @ "tcp: // 10.53.139.138: 61000/SVRCALL. rem ");
Console. WriteLine (test. Cout ());

 

Running the Client Multiple times is Singleton, but Num does not increase to 2 each time.

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.