Webcast.NET Remoting学习笔记(6)租约

首先说一下对租约的理解,租约就是服务端保留激活的对象的实例的一个时间,如果在这段时间里一直没有访问,那么租约分布式垃圾回收器就将销毁这个实例。这里指服务器端singleton和客户端激活两种方式激活的对象。为了让这个对象存在,我们就需要续约,续约有三种方法:
1 隐式续约,就是访问对象的方法,即可实现续约
2 显式续约,使用ILease.Renew(),不调用任何方法就可续约
3 发起租约,上面两种方式是由客户端自己来实现的,而发起租约是由一个发起者来实现续约,通过ILease.Register方法将租约注册,让发起者来续约。

租约的配置主要有四个属性
1 InitialLeaseTime 它表示激活一个对象之后,不再访问该对象的方法时,服务端保留该对象的最长时间。它为0时表示永远不过期。
2 RenewOnCallTime  它表示当客户端调用远程对象的方法时,服务端确定租约还剩下多长时间,如果时间RenewOnCallTime,便根据RenewOnCallTime的值更新租约。
3  SponsorshipTimeout 它表示的是当租约过期后并不是立即撤销该对象而还等待的时间。
4  CurrentLeaseTime表示当前租约的剩余时间。

下面用一个例子来说明
下面是解决方案的视图
1.bmp
client.cs

None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Channels.Http;
None.gif
using  System.Runtime.Remoting.Activation;
None.gif
using  System.Runtime.Remoting.Lifetime;
None.gif
using  System.IO;
None.gif
None.gif
namespace  RemotingSamples 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Client
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//使用TCP通道得到远程对象
InBlock.gif
            ChannelServices.RegisterChannel(new TcpChannel());
InBlock.gif
InBlock.gif            HelloServer obj 
= (HelloServer)Activator.GetObject(
InBlock.gif              
typeof(RemotingSamples.HelloServer),
InBlock.gif              
"tcp://localhost:8085/SayHello");
InBlock.gif            
if (obj == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Console.WriteLine(
InBlock.gif                    
"Could not locate TCP server");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
//object[] attrs = { new UrlAttribute("tcp://localhost:8085/Hello") };
InBlock.gif            
//HelloServer obj = (HelloServer)Activator.CreateInstance(typeof(HelloServer), null, attrs);
InBlock.gif

InBlock.gif            ILease lease 
= (ILease)obj.GetLifetimeService();
InBlock.gif            
if (lease != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Lease Configuration:");
InBlock.gif                Console.WriteLine(
"InitialLeaseTime: " +
InBlock.gif                    lease.InitialLeaseTime);
InBlock.gif                Console.WriteLine(
"RenewOnCallTime: " +
InBlock.gif                    lease.RenewOnCallTime);
InBlock.gif                Console.WriteLine(
"SponsorshipTimeout: " +
InBlock.gif                    lease.SponsorshipTimeout);
InBlock.gif                Console.WriteLine(lease.CurrentLeaseTime);
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
client.exe.config
None.gif < configuration >
None.gif    
< system.runtime.remoting >
None.gif        
< application >
None.gif            
< client >
None.gif                
< wellknown type = " HelloServer, General "  url = " http://localhost:8086/SayHello "   />
None.gif            
</ client >
None.gif            
< channels >
None.gif                
< channel  ref = " http "  port = " 0 " ></ channel >
None.gif            
</ channels >
None.gif        
</ application >
None.gif    
</ system.runtime.remoting >
None.gif
</ configuration >
None.gif
HelloServer.cs
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  RemotingSamples
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HelloServer : MarshalByRefObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public HelloServer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"HelloServer activated");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public String HelloMethod(String name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
InBlock.gif                
"Server Hello.HelloMethod : {0}", name);
InBlock.gif            
return "Hi there " + name;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
server.cs
None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Channels.Http;
None.gif
None.gif
namespace  RemotingSamples 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
public class Server
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static int Main(string [] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            TcpChannel chan1 
= new TcpChannel(8085);
InBlock.gif            HttpChannel chan2 
= new HttpChannel(8086);
InBlock.gif
InBlock.gif            ChannelServices.RegisterChannel(chan1);
InBlock.gif            ChannelServices.RegisterChannel(chan2);
InBlock.gif
InBlock.gif
InBlock.gif            
//服务器端激活。
InBlock.gif
            RemotingConfiguration.RegisterWellKnownServiceType
InBlock.gif                (
InBlock.gif                
typeof(HelloServer),
InBlock.gif                
"SayHello",
InBlock.gif                WellKnownObjectMode.Singleton
InBlock.gif                );
InBlock.gif
InBlock.gif            
//客户端激活
InBlock.gif            
//RemotingConfiguration.ApplicationName = "Hello";
InBlock.gif            
//RemotingConfiguration.RegisterActivatedServiceType(
InBlock.gif            
//    typeof(HelloServer));
InBlock.gif
            
InBlock.gif
InBlock.gif            System.Console.WriteLine(
"Press Enter key to exit");
InBlock.gif            System.Console.ReadLine();
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
运行后服务器端会显示HelloServer activated,如果在五分钟内重新运行客户端,服务器端不会有变化,因为是Singleton激活方式,只有一个实例,但是如果五分钟后在运行一个客户端,服务器端会再次出现HelloServer activated,因为上一个对象实例已经被撤销,这是再次激活了一个新实例。
最后我们在介绍一下如何更改租约时间,主要有两种方法:
1 代码
在helloserver中加入一个Object InitializeLifetimeService()的重写来实现租约的更改
None.gif   public   override  Object InitializeLifetimeService()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            ILease lease 
= (ILease)base.InitializeLifetimeService();
InBlock.gif            
// Normally, the initial lease time would be much longer.
InBlock.gif            
// It is shortened here for demonstration purposes.
InBlock.gif
            if (lease.CurrentState == LeaseState.Initial)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lease.InitialLeaseTime 
= TimeSpan.FromSeconds(0);
InBlock.gif                lease.SponsorshipTimeout 
= TimeSpan.FromSeconds(10);
InBlock.gif                lease.RenewOnCallTime 
= TimeSpan.FromSeconds(2);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return lease;
ExpandedBlockEnd.gif        }
2 配置文件
在配置文件中加入<lifetime
      leaseTime="7M"
      sponsorshipTimeout="7M"
      renewOnCallTime="7M"
      />
来实现租约的更改

转载于:https://www.cnblogs.com/stuhrbeu/archive/2007/02/06/641722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值