zoukankan      html  css  js  c++  java
  • 第2章 对象激活上下文对象激活

    对象激活主要包括

    • 构造字符串
    • 及时激活
    • 对象池
    • 私有组件
    1.构造字符串

    服务器苏建只能使用默认的构造函数,这样在对象创建的时候你就不能通过构造函数初始化数据.但是你可以使用构造字符串实现类似的功能,只是每次实例化的时候都只能使用相同的构造字符串.系统管理员可以改变构造字符串.(比如用到配置数据库的连接字符串).

    通过[ConstructionEnabled]特性和其Default属性把默认的构造字符串添加到配置元数据中.在类内部你必须重写基类SericedComponent的Construct方法.当每次创建对象时,这种方法会被COM+基础结构调用.

    2.即时激活(Just-in-Time Activation JITA)

    JITA是一个用于减少服务器负载的特性.对于打开lJITA支持的组件,他的生命周期和他使用的客户端应用程序无关.这个服务器组件自己通过设置完成位来决定对象什么时候应该被终止.如果客户应用程序通过客户端的同一个引用来调用一个对象的方法,而这个对象在服务器端已经被终止的话,一个新的对象会被自动创建并激活.

    JITA是通过设置[JustInTimeActiveation]来启用的.

    要使用JITA,必须重写两个基类ServicedComponent的方法:Activate和Deactive.当对象生成后Activate方法会被运行时自动调用.当对象终止前Deactive方法会被自动调用.

    为组件设置完成的两种方法:

    • [AutoComplate]
    • 设置ContextUtil的静态属性DeactivateOnReturn为True.
    3.对象池

    对象池对于那种初始化过程很长的对象(比如,连接到一个老系统的服务器,或创建一个复杂的矩阵以进行数学运算)是个有用的服务.如果调用方法所需要的时间少于创建所需要的时间,应该考虑对象池技术.

    对象的初始化过程在客户端第一次使用它之前进行:在应用程序启动后,为对象池设定的最小的对象就会被创建和初始化.

    4.私有组件

    私有组件是COM+1.5带来的新特性之一.被标记为[PrivateComplent]特性的组件只能由应用程序内部的对象激活,客户端应用程序不行.

      1using System;
      2using System.EnterpriseServices;
      3using System.Xml;
      4using System.Globalization;
      5using System.IO;
      6
      7
      8namespace Demo.Introduction
      9{
     10    [ObjectPooling(true,100,1000)]
     11    [JustInTimeActivation]
     12    [ConstructionEnabled(Default = @"C:\Temp\")]
     13    [EventTrackingEnabled]
     14    public class CoursesComponent : ServicedComponent, ICourseOrder
     15    {
     16        private string path;
     17
     18        public CoursesComponent() { }
     19
     20        protected override void Construct(string s)
     21        {
     22            path = s;
     23        }

     24
     25        ICourseOrder 成员
     52
     53        private StreamWriter writer = null;
     54
     55        /// <summary>
     56        /// Create and opens aunique file
     57        /// </summary>

     58        private void CreateFile()
     59        {
     60            string uniqueName = Guid.NewGuid().ToString();
     61            writer = new StreamWriter(path + uniqueName + ".txt");
     62        }

     63
     64        private void CloseFile()
     65        {
     66            writer.Close();
     67        }

     68
     69        protected override void Activate()
     70        {
     71            CreateFile();
     72        }

     73
     74        protected override void Deactivate()
     75        {
     76            CloseFile();
     77        }

     78
     79        /// <summary>
     80        /// Write course information to the comma-separated file
     81        /// </summary>
     82        /// <param name="courseNumber"></param>
     83        /// <param name="title"></param>
     84        /// <param name="startDate"></param>
     85        /// <param name="attendee"></param>

     86        private void WritToFile(string courseNumber, string title, DateTime startDate, string attendee)
     87        {
     88            writer.WriteLine("{0};{1};{2};{3}", courseNumber, title, startDate.ToString("d", CultureInfo.InstalledUICulture), attendee);
     89        }

     90
     91        /// <summary>
     92        /// parses the xml data of a single course for the xml element
     93        /// tagName to return the inner text elements
     94        /// </summary>
     95        /// <param name="xmlCourse"></param>
     96        /// <param name="tagName"></param>
     97        /// <returns></returns>

     98        private string[] GetText(XmlElement xmlCourse, string tagName)
     99        {
    100            string[] text = null;
    101            XmlNodeList nodeList = xmlCourse.GetElementsByTagName(tagName);
    102            if (nodeList.Count < 1)
    103                throw new Exception("No elements of type <" + tagName + "> available");
    104            //CourseException("No elements of type <" + tagName + "> available");
    105            text = new string[nodeList.Count];
    106            for (int i = 0; i < nodeList.Count; i++)
    107            {
    108                XmlElement element = nodeList[i] as XmlElement;
    109                if (element != null)
    110                {
    111                    text[i] = element.InnerText;
    112                }

    113            }

    114            return text;
    115        }

    116    }

    117}

    118
  • 相关阅读:
    除草第一季 1 简单日常心得
    [搬家from qzone] 读书笔记 人间情味.半成品
    [搬家from qzone] 读书笔记 最好的告别
    [搬家from qzone] 读书笔记 爱是一种选择
    [搬家from qzone] 读书笔记 童年的王国
    [搬家from qzone] 我不是一个很好的学长,所以毕业前,给学习学妹们写写自己犯的错误吧
    博客重新开张 第一篇灌水
    我眼中的vim
    linux启动kdump失败
    centos7 minimal安装之后要做的事情
  • 原文地址:https://www.cnblogs.com/nasa/p/629168.html
Copyright © 2011-2022 走看看