zoukankan      html  css  js  c++  java
  • Spring.net Service 配置Remoting示例

    相关概念:

    SAO,CAO

    SingleTon(默认),SingleCall

    用Spring.net 配置发布Remoting,服务器对象不需要继承MarshByRefObject,而是一个原生的PONO(plain .net object)。spring.net在运行时创建一个proxy继承MarshByRefObject.

    服务器端配置示例
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>

      
    <configSections>
        
    <sectionGroup name="spring">
          
    <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
          
    <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
          
    <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
        
    </sectionGroup>
      
    </configSections>

      
    <spring>

        
    <parsers>
          
    <parser type="Spring.Remoting.Config.RemotingNamespaceParser, Spring.Services" />
        
    </parsers>

        
    <context>
          
    <resource uri="config://spring/objects" />
          
    <!--<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao-aop.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall-aop.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton-aop.xml" />
    -->
        
    </context>

        
    <objects xmlns="http://www.springframework.net"
                         xmlns:r
    ="http://www.springframework.net/remoting">
          
    <description>Definitions of objects to be exported.</description>

          
    <r:configurer filename="TestSpringService.ServerApp.exe.config" />

          
    <!--singleton="false" 即为SAO的SingleCall,默认为Singleton-->
          
    <object id="singletonCalculator" type="TestSpringService.Common.Calculator, TestSpringService.Common"
                  singleton
    ="false"></object>

          
    <!--导出接口-->
          
    <r:saoExporter
              
    targetName="singletonCalculator"
              serviceName
    ="RemotedSaoSingletonCalculatorWucg" />
          
    <!--
            Old fashion way without remoting config parser...    
    -->
          
    <object name="saoSingletonCalculator" type="Spring.Remoting.SaoExporter, Spring.Services">
            
    <property name="TargetName" value="singletonCalculator" />
            
    <property name="ServiceName" value="RemotedSaoSingletonCalculatorWucgOld" />
          
    </object>
        
    </objects>

      
    </spring>

      
    <system.runtime.remoting>
        
    <application>
          
    <channels>
            
    <channel ref="tcp" port="8080" />
            
    <!--<channel ref="http" port="8080" />-->
          
    </channels>
        
    </application>
      
    </system.runtime.remoting>
    </configuration>

    Server端程序:

    static void Main(string[] args)
            {
                try
                {
                    // Force Spring to load configuration
                    ContextRegistry.GetContext();
                    Console.Out.WriteLine("Server listening...");
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e);
                }
                finally
                {
                    Console.Out.WriteLine("--- Press <return> to quit ---");
                    Console.ReadLine();
                }
            }

    Client端配置
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>

      
    <configSections>
        
    <sectionGroup name="spring">
          
    <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
          
    <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
          
    <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
        
    </sectionGroup>
      
    </configSections>

      
    <spring>

        
    <parsers>
          
    <parser type="Spring.Remoting.Config.RemotingNamespaceParser, Spring.Services" />
        
    </parsers>

        
    <context>
          
    <resource uri="config://spring/objects" />
          
    <!--<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao-aop.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall-aop.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton.xml" />
          <resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton-aop.xml" />
    -->
        
    </context>

        
    <objects xmlns="http://www.springframework.net"
                         xmlns:r
    ="http://www.springframework.net/remoting">
          
    <description>配置服务</description>

          
    <!--注:serviceInterface一定为接口-->
          
    <r:saoFactory id="calculatorService"
                    serviceInterface
    ="TestSpringService.Common.ICalculator, TestSpringService.Common"
                    serviceUrl
    ="http://localhost:8080/RemotedSaoSingletonCalculatorWucg"/>

          
    <!--Old fashion way without remoting config parser...-->
          
    <object id="calculatorServiceOld" type="Spring.Remoting.SaoFactoryObject, Spring.Services">
            
    <property name="ServiceInterface" value="TestSpringService.Common.ICalculator, TestSpringService.Common" />
            
    <property name="ServiceUrl" value="tcp://localhost:8080/RemotedSaoSingletonCalculatorWucgOld" />
          
    </object>


        
    </objects>

      
    </spring>

    </configuration>

    Client程序:

     static void Main(string[] args)
            {
                IApplicationContext ctx = ContextRegistry.GetContext();

                Console.WriteLine("Get Calculator...");
                ICalculator firstCalc = (ICalculator)ctx.GetObject("calculatorServiceOld");


                Console.WriteLine("Divide(11, 2) : " + firstCalc.Divide(11, 2));

                Console.WriteLine("Divide(11, 2) : " + firstCalc.Add(11, 2));

                Console.WriteLine("Get Calculator...");
                ICalculator secondCalc = (ICalculator)ctx.GetObject("calculatorServiceOld");
                Console.WriteLine("Divide(20, 3) : " + secondCalc.Divide(20, 3));

                Console.ReadLine();

            }

    项目文件下载

    PS:

    CAO导出

    Server端CAO配置
          <object id="caoCalculator" type="TestSpringService.Common.Calculator, TestSpringService.Common"
          lazy-init
    ="true"  ></object>


          
    <!--CAO导出接口 old fashion-->
          
    <!--<object id="caoCalculatorOld" type="Spring.Remoting.CaoExporter, Spring.Services">
            <property name="TargetName" value="caoCalculator" />
            <property name="Infinite" value="false" />
            <property name="InitialLeaseTime" value="2m" />
            <property name="RenewOnCallTime" value="1m" />
          </object>
    -->

          
    <!--最新导出方式-->
          
    <r:caoExporter targetName="caoCalculator" infinite="false">
            
    <r:lifeTime initialLeaseTime="2m" renewOnCallTime="1m" />
          
    </r:caoExporter>
    Client端配置及调用
          <!--cao Services-->
          
    <object id="caoCalculatorServiceOld" type="Spring.Remoting.CaoFactoryObject, Spring.Services">
            
    <property name="RemoteTargetName" value="caoCalculator" />
            
    <property name="ServiceUrl" value="tcp://localhost:8080" />
          
    </object>

          
    <r:caoFactory id="caoCalculatorService"
    remoteTargetName
    ="caoCalculator"
    serviceUrl
    ="tcp://localhost:8080" />


    cs代码:
                ICalculator caoCalc = (ICalculator)ctx.GetObject("caoCalculatorServiceOld");
                Console.WriteLine("cao multiply:"+ caoCalc.Multiply(10, 20));


                ICalculator caoNewCalc = (ICalculator)ctx.GetObject("caoCalculatorService");
                Console.WriteLine("cao multiply:" + caoNewCalc.Add(10, 20));
                

     

    ILease.RenewOnCallTime Property
     

    Gets or sets the amount of time by which a call to the remote object renews the CurrentLeaseTime.

    Namespace:  System.Runtime.Remoting.Lifetime
    Assembly:  mscorlib (in mscorlib.dll)

     

    ILease Properties

    The ILease type exposes the following members.

    Properties

      Name Description
    Public property CurrentLeaseTime Gets the amount of time remaining on the lease.
    Public property CurrentState Gets the current LeaseState of the lease.
    Public property InitialLeaseTime Gets or sets the initial time for the lease.
    Public property RenewOnCallTime Gets or sets the amount of time by which a call to the remote object renews the CurrentLeaseTime.
    Public property SponsorshipTimeout Gets or sets the amount of time to wait for a sponsor to return with a lease renewal time.
    Top

  • 相关阅读:
    树莓派配置Jdk环境并设置开机启动jar
    缓存架构之路(一)缓存概述及应用
    并发编程学习历程(零)JMM内存模型
    并发编程学习历程(一)Synchronized
    深入Disruptor源码分析(一)入门指南
    JAVA Synchronized和Lock实现原理
    Vscode断点调试PHP
    php环境搭建
    github使用总结
    面试笔记
  • 原文地址:https://www.cnblogs.com/wucg/p/1806381.html
Copyright © 2011-2022 走看看