Remoting Lifetime Leases


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Remoting Lifetime Leases

  1. #1
    Mikael Guest

    Remoting Lifetime Leases



    Has anyone seen some examples of how to handle lifetime of client activated
    remoting objects?

    The lifetime sponsorship mechanism described in the .NET documentation is
    a way to complicated to understand without examples. Unfortunally I have
    not found any examples.

    One way to handle liftime is to periodically call a ReNewLifeTime() method
    on the remoting object, but I Am sure there are more clean ways to do this.

    // Mikael




  2. #2
    Tom Barnaby Guest

    Re: Remoting Lifetime Leases


    "Mikael" <mjs@nospam.beamex.fi> wrote:
    >
    >
    >Has anyone seen some examples of how to handle lifetime of client activated
    >remoting objects?
    >


    Yep, in my courseware and in the Remoting chapter of the book I'm writing
    ;-)

    >The lifetime sponsorship mechanism described in the .NET documentation is
    >a way to complicated to understand without examples.


    Not to mention the fact that the .NET doc gives the wrong XML element names
    when describing how to configure the lifetimes. :-(

    Anyhow, there is a lot to cover in this, since there a 2 or 3 ways to accomplish
    the same thing. But here is an example using the remoting config file to
    set up the lease settings:

    <configuration>
    <system.runtime.remoting>
    <application name="Server">

    <!-- Configure default lease settings -->
    <lifetime leaseTime="5s" renewOnCallTime="5s"
    sponsorshipTimeout="5s" leaseManagerPollTime = "5s" />

    <!-- Configure client activated objects -->
    <service>
    <activated type="MathLibrary.HelloWorld, MathLibrary" />
    </service>

    <!-- Configure channels etc ... -->
    </application>
    </system.runtime.remoting>
    </configuration>

    The meanings of the <lifetime> attributes are as follows:

    leaseTime = The initial lease time period

    renewOnCallTime = How much lease time to give on each method call

    sponsorshipTimeOut = How long to wait for a sponser to give more lease time
    to an object about to be orphaned.

    leaseManagerPollTime = How often the runtime checks the leases of client
    activated objects.


    You can also let each remoted class determine its lease by overriding the
    MarshalByRefObject.InitializeLifeTimeService method:

    Public Class CAOHelloWorld
    Inherits MarshalByRefObject

    Overrides Function InitializeLifeTimeService() As Object
    Dim oLease As ILease

    oLease = CType(MyBase.InitializeLifetimeService(), ILease)
    If oLease.CurrentState = LeaseState.Initial Then
    oLease.InitialLeaseTime = TimeSpan.FromSeconds(2)
    oLease.SponsorshipTimeout = TimeSpan.FromSeconds(2)
    oLease.RenewOnCallTime = TimeSpan.FromSeconds(2)
    End If
    Return oLease
    End Function
    End Class


    Leases are renewed in 2 ways:

    1) Whenever a client invokes a method on a client activated object, the runtime
    renews the lease to the renewOnCallTime value.

    2) When an object's lease expires, the runtime searches for sponsor objects
    (objects that implement the ISponsor interface). The runtime gives each the
    opportunity to renew the lease by invoking the sponsor's Renewal method.

    The following code shows a sponser object that will renew a lease exactly
    3 times.

    Public Class HelloSponsor
    Implements ISponsor
    Inherits MarshalByRefObject

    Private mRenewCount As Integer

    Public Function Renewal(ByVal theLease As ILease) _
    As TimeSpan Implements ISponsor.Renewal
    If mRenewCount < 3 Then
    mRenewCount += 1
    Return TimeSpan.FromSeconds(5)
    Else
    Return TimeSpan.FromSeconds(0)
    End If
    End Function
    End Class

    Here is the code to register this sponser object with a remoted object called
    oHello. Note that the GetLifeTimeService method is used to retrieve the lease
    info for the object. This method is inheritted from MarshalByRefObject.

    'Register a sponsor
    Dim oLease As ILease

    'Get the remote objectís lease.
    oLease = CType(oHello.GetLifetimeService(), ILease)
    oLease.Register(New HelloSponsor())

    HTH
    Tom Barnaby
    www.intertech-inc.com

  3. #3
    Mikael Guest

    Re: Remoting Lifetime Leases


    "Tom Barnaby" <tbarnaby@intertech-inc.cm> wrote:
    >
    >Yep, in my courseware and in the Remoting chapter of the book I'm writing
    >;-)


    When and where will your book be released and what is it's name?

    >Leases are renewed in 2 ways:


    Thank you for your excellent answer. I have however one more question, just
    to verify i got it right!

    <snip..>

    >2) When an object's lease expires, the runtime searches for sponsor
    > objects (objects that implement the ISponsor interface). The runtime
    > gives each the opportunity to renew the lease by invoking the sponsor's


    > Renewal method.
    >
    >The following code shows a sponser object that will renew a lease.
    >
    >Public Class HelloSponsor
    > Implements ISponsor
    > Inherits MarshalByRefObject


    Can this class (implementing ISponsor) be located on the client side, while
    the object whose lifetime need to be renewed is on server side?

    The Renewal function could then look like this:

    Public Function Renewal(ByVal theLease As ILease) _
    As TimeSpan Implements ISponsor.Renewal

    If Not Me.Disposed Then
    Return TimeSpan.FromSeconds(5)
    Else
    Return TimeSpan.FromSeconds(0)
    End If

    End Function

    Is it even possible (or decent design) to crossreference assemblies like
    this?

    Any thoughts?



  4. #4
    Tom Barnaby Guest

    Re: Remoting Lifetime Leases


    "Mikael" <mjs@nospam.beamex.fi> wrote:
    >
    >"Tom Barnaby" <tbarnaby@intertech-inc.cm> wrote:
    >>
    >>Yep, in my courseware and in the Remoting chapter of the book I'm writing
    >>;-)

    >
    >When and where will your book be released and what is it's name?
    >


    It is "Distributed .NET" from A-press. Scheduled publish date is at the end
    of the year, but could be later if I don't stop reading these news boards.
    ;-)

    >
    >Thank you for your excellent answer. I have however one more question, >just

    to verify i got it right!

    >Can this class (implementing ISponsor) be located on the client side, >while

    the object whose lifetime need to be renewed is on server side?
    >


    Yep, in fact, the example I gave is actually from a client side sponsor.
    The sponsor registration code I posted also executes on the client.

    >Is it even possible (or decent design) to crossreference assemblies like
    >this?
    >
    >Any thoughts?
    >


    Its not something I would do consistently. The reason .NET implements lease
    based lifetimes as opposed to the current DCOM ref counting scheme is to
    remove the need for the server to periodically ping clients to ensure they
    are still alive. This advantage is removed if you implement client side sponsors.


    On the other hand, sometimes you may have client side logic that determines
    whether the remote object can be destroyed. You really have no other choice
    in this case.

    Tom Barnaby
    www.intertech-inc.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center