栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 前沿技术 > 人工智能 > NLP

如何使用XML实现多渠道接入网站的构架

NLP 更新时间:发布时间: 百科书网 趣学号
一.背景

  在现在的网站中,接入的渠道是越来越多了,技术也是越来越先进,WAP, SMS,EMAIL, 传统的Web, Socket等等,如果连数据库和LDAP也算接入的话,那在设计之中需要扩展的空间要做到很好 很好,才保证在添加新的渠道情况下不需要更多的修改代码甚至不改代码的情况。但可能吗?我想也不可能,但有什么方式可以更好的解决这种多渠道接入的框架的完美性呢?

二.构架

 【图一】

 如图一所显示,在现有的所有接入都已经使用上的时候,设计者看的都眼花缭乱了,如果是为了凑份子,那这些程序怎么写都可以,而且也肯定可以实现,但维护起来就会比较痛苦,还是回到那个问题,怎么可以实现更完美呢?如图二显示:

【图二】

 图二看起来象个八爪的章鱼,章鱼腿分别连接所有的接入渠道,进行连接所有这些渠道的核心就是这个章鱼的头xmlRouter,Router在此的作用是沟通所有渠道,实现数据的路由,争强系统在构架上的扩展性和灵活性,好处会很多很多。称呼为XMLRouter是因为如果不使用XML这种灵活而又规范的语言来做为数据传输的媒介,那Router的工作量也同样会成倍的增加,定义好XML的规范后将为以后的扩展带来很多好处。

  三.思想和模式

  XMLRouter的最初想法来自于计算机的主板和<>之中的Builder Pattern, 计算机主板的PCI 插槽定义了PCI的规范,只要你生产的卡符合PCI标准,那你插入到这个主板上就可以工作, 至于里面是怎么工作的则是已经封装好了. Builder Pattern则是提出将复杂的构建分离开来, 一步一步的进行实现.XMLRouter是将这些复杂的渠道分离开来,一个个的进行表现.

  Services思想:为了能和Router进行沟通,在这些渠道接入时必须定义统一的接口,这里成为Services, 只要符合Services规范的程序就可以接入到Router并进行数据的路由.

  Factory模式和Composite模式

  XMLRouter在实际的设计中将采用Factory模式产生,Router由RouterFactory生产, 在投入使用时将放置于队列中,传递数据和接收数据以及返回数据都从队列中取相应的Router来调用,应用了Composite的模式.

  四.XML配置文件

  XML文件对于Router之中的使用分为两个部分, 第一个是Router的配置,如:

以下是引用片段:

                                    …… 


  这是Router的配置文件, service节点代表需要接入的渠道, service节点包含connector子节点, 子节点的配置根据type来区分, 如果是database则包含url, user, passwd,driver等属性, 如果是socket则包含 port, maxthread等属性, 属性值可以根据自己的定义来配置.

  另一种XML文件则是XML交易数据文件,用于在所有的services中传递数据,每个Services自己包涵一个相应的XML文件,比如webtrans.xml格式如下:

以下是引用片段:

                                            


  相应的dbtrans.xml格式如下


以下是引用片段:

                                                                



  其余XML则可按这样的规则来定制

  五.技术实现

  RouterFactory

以下是引用片段:

package com.web.router; import com.web.platform.Exception.RouterException; import java.util.java/util/Hashtable.java.html" target="_blank">Hashtable;




以下是引用片段:

 public class RouterFactory  {      private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairFront = null;      private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairBack = null;     private static java/util/Hashtable.java.html" target="_blank">Hashtable QueueRouter = null;       public static XMLRouter instance = null;      public static RouterDefine routerdefine = null;      public static long routeIndex = 0;      public RouterFactory()    {   }      public static void initFactory() throws java/lang/Exception.java.html" target="_blank">Exception   {       QueuePairFront = new java/util/Hashtable.java.html" target="_blank">Hashtable();        QueuePairBack = new java/util/Hashtable.java.html" target="_blank">Hashtable();        QueueRouter    = new java/util/Hashtable.java.html" target="_blank">Hashtable();       initRouteDefine();   }     private static void initRouteDefine() throws java/lang/Exception.java.html" target="_blank">Exception   {       if( routerdefine == null )         routerdefine = new RouterDefine();       routerdefine.loadRouterDef();   }      public static XMLRouter getInstance(long index) throws RouterException   {      return (XMLRouter)QueueRouter.get(new java/lang/Long.java.html" target="_blank">Long(index));   }       public static XMLRouter popInstance() throws RouterException   {       routeIndex ++;       instance = new XMLRouter(routeIndex);       setDefine( instance );       QueueRouter.put(new java/lang/Long.java.html" target="_blank">Long(routeIndex), instance);           return instance;   }      private static void freeResource() throws java/lang/Exception.java.html" target="_blank">Exception   {       QueuePairFront.clear();       QueuePairBack.clear();       QueueRouter.clear();       QueuePairFront = QueuePairBack = QueueRouter = null; }      public static void removeInstance(XMLRouter instance) throws java/lang/Exception.java.html" target="_blank">Exception    {       instance.clear();      QueueRouter.remove( new java/lang/Long.java.html" target="_blank">Long(instance.getIndex() ) ) ;   }    public static boolean isNull()   {      ……        return false;   } }



  XMLRouter

以下是引用片段:

package com.web.router;  import com.web.platform.Exception.RouterException; import com.web.common.*; import java.util.*; import java.lang.reflect.java/lang/reflect/Method.java.html" target="_blank">Method; import java.lang.reflect.java/lang/reflect/Constructor.java.html" target="_blank">Constructor;   public class XMLRouter  {       private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairFront = null;       private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairBack = null;      private long routeIndex = 0;      private RouterDefine define = null;       private java/lang/String.java.html" target="_blank">String action = "";        private java/lang/String.java.html" target="_blank">String classname = "";          public XMLRouter(long index)      {      routeIndex = index;     }          public void routing(Env env) throws RouterException, java/lang/Exception.java.html" target="_blank">Exception      {             if( action.equalsIgnoreCase( RouterConstant.CFG_FUNC_ROUTETO ) )        {        ……      }            else if( action.equalsIgnoreCase( RouterConstant.CFG_FUNC_ROUTEBACK ) )      {      ……      }            else        throw new RouterException("Set Router action error.");     }        public long getIndex()    {       return routeIndex;        }     public void clear() throws RouterException    {      QueuePairFront.remove(new java/lang/Long.java.html" target="_blank">Long(routeIndex));      QueuePairBack.remove(new java/lang/Long.java.html" target="_blank">Long(routeIndex));            java/lang/System.java.html" target="_blank">System.runFinalization();         }         public void setDefine(RouterDefine def) throws RouterException    {      define = def;      }         public void setAction( java/lang/String.java.html" target="_blank">String actionName )    {      action = actionName;        } }



  Service类

以下是引用片段:

package com.web.common;  import com.web.platform.Exception.RouterException;   public abstract class RouteService  {      public RouteService()    {   }      public abstract void routeto(Env env) throws RouterException;      public abstract void routeback(Env env) throws RouterException;      public abstract void routeaccept(Env env) throws RouterException;        public abstract void routing() throws RouterException;


  接下来则需要实现所有的Services的类了,这里就不做介绍了.

  六.说明

  这个Router到目前为止只能实现同步的交易, 暂时不支持异步的交易,但是由于对Router使用了Composite的模式设计的,实现异步交易也是可以扩展的,这里不做详细分析.

以上就是如何使用XML实现多渠道接入网站的构架的内容,更多相关内容请关注PHP中文网(www.iotsi.net)!

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/794797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号