zoukankan      html  css  js  c++  java
  • 翻译:Why Singletons are destroyed after five minutes

     以Singletons方式建立的远程对象会在5分钟后被回收的原因

    原文地址:http://www.thinktecture.com/Resources/RemotingFAQ/SINGLETON_IS_DYING.html

    Q:

    I have a C# class that is being exposed via remoting as a Singleton Server Activated Object.

    采用服务端激活的Singleton方式公开的一个类

     

    However, sometimes this object just disappears and a new one is created. Shouldn't only a single instance ever exist for a Singleton?

    然而,有时远程对象被销毁,并且一个新的远程对象被建立。不是说Singleton方式建立的远程对象只有单一实例吗?

    A:

    Remoting uses a so called "lease based" lifetime services. When you create an object it has a certain time-to-live (normally 5 minutes) upon every methodcall you place from your client onto the object an additional time ("RenewOnCallTime", usually 2 minutes) will be added to the object's lifetime.

    Remoting使用所谓的基于租约的生命期管理。一个远程对象被建立后即被安排了5分钟的默认租期,每当有来自客户端的方法调用时,这个租期再被增加2分钟

     

    This is also valid for Singletons - the only difference is that the server employs a guarantee, that there either exists exactly one instance or none at all.

    When you don't call the server for about 5 minutes, the object will be garbage collected.

    Singleton方式即是这样,特殊之处在于:远程对象被服务端租用,只存在一个实例,或者一个都不存在。在5分钟内没有另一次的对远程对象的访问请求时,对象将被GC回收

     

     You can work around this using two schemes:

    有两种变通的方法:

     

    First, you can provide different leases than the default:

    首先,你可以自定义租期:

     

     

    public class Foo : MarshalByRefObject
    {
        
    public override Object InitializeLifetimeService()
        
    {
            ILease lease 
    = (ILease)base.InitializeLifetimeService();
            
    if (lease.CurrentState == LeaseState.Initial) 
            
    {
                lease.InitialLeaseTime 
    = TimeSpan.FromMinutes(100);
                lease.SponsorshipTimeout 
    = TimeSpan.FromMinutes(2);
                lease.RenewOnCallTime 
    = TimeSpan.FromSeconds(100);
            }

            
    return lease;
        }

    }

     

    This piece of code will ensure that the MarshalByRefObject will live for at least 100 minutes.

    让远程对象被建立后拥有100分钟的默认周期。(当然这个租期会随着这期间的方法调用请求而增加。)

     

    If you want your singleton to live forever, you can simply return null:

    第二种方式:让singleton方式建立的远程对象长生不老

     

     

    public class Foo : MarshalByRefObject 
    {
        
    public override Object InitializeLifetimeService()
        
    {
            
    return null;
        }

    }

     

     

    The second way is to use different sponsor-object [on the client, the server or somewhere else]. Upon running out of lease time the .NET-Remoting framework will call the sponsor of the object an ask it, if it wants to renew or increase the lease time. The sponsor now has 2 Minutes (ILease SponsorshipTimeout) to answer this request, else the object will time-out.

    这种方式利用租用者(sponsor)对象。当租用者租用的远程对象到期时,Remoting基础结构会征求租用者是否需要重新租用或者增加租期。租用者有2分钟的时间回应,过了这个期限将无法访问对象。

     

    (在Singleton方式下,这个租用者将从建立在服务端的租约管理期中的远程对象租用者列表中移除,对象并不一定被回收)

  • 相关阅读:
    使用libgdx及其中的box2d 2.1的注意事项
    Android.mk file syntax specification(ndkr8)
    NDK Note
    Problems of Android NDK
    Android.mk of NDK
    Android IM Note
    Regular Expression
    Some Efficient Algorithms
    libgdx use TexturePacker
    The Conversion Of JNI
  • 原文地址:https://www.cnblogs.com/yicone/p/238207.html
Copyright © 2011-2022 走看看