Apache OFBiz 学习笔记 之 服务引擎 一

概述
    服务定义为一段独立的逻辑顺序,当多个服务组合一起时可完成不同类型的业务需求
    服务有很多类型,WorkFlow、Rules、Java、SOAP、BeanShell等。java类型的服务更像一个静态方法实现的事件,然而使用服务框架就不会局限在web应用程序中。
    服务需要使用Map传入参数,结果同样从Map中返回。这样很妙,因为Map可以被序列化并保存或通过HTTP(SOAP)传输。服务通过服务定义来定义并指派给具体的服务引擎。每个服务引擎通过适当的方式负责调用服务定义。因为服务没有和web应用程序绑定在一起,就允许在没有响应对象可用时仍可以执行。这就允许在指定时间由工作调度程序在后台调用服务
    服务能够调用其他服务。因此,将多个小的服务串联起来实现一个大的任务使重用更容易。在不同应用程序中使用的服务可以通过创建全局服务定义文件(只能创建一个)或者一个应用程序的特定服务(这样的服务受限制且只能用于这个应用程序)。
    当在web应用程序中使用时,服务可以用于web事件,这允许同时在服务框架中并重用现成的逻辑。同样,服务可以定义成“exportable",允许外部程序访问。目前,SOAP EventHandler允许服务通过SOAP来产生。其他形式的远程访问将来会加入到框架中。
 
特点
1、不可知的服务调用:实现不可知,位置不可知,你需要调用的服务仅仅按照要求知道服务名和传递的参数即可,你不需要知道在哪里实现的,文件在哪个服务器上或者其他的这类事情。你也不必关心是用java、simplemethod、还是其他脚本语言实现的。这就提供了在调用服务代码和服务间的松耦合,提供了高伸缩性、增加重用性。
2、不同类型的服务调用:同步、异步、预定时间。相同的服务定义、相同的服务实现,可以根据应用的需要用不同的方式调用。
3、服务提供输入和输出参数的校验。
4、不同的实现方式:有许多不同方式实现服务,处理这些不同方式实现服务的对象称之为服务引擎。处理不同类型服务实现的每个对象都要实现GenericEngine interface。所以你想实现自己的服务,基本上实现这个接口即可。
 
服务引擎的定义
在serviceengine.xml文件中定义了存在的引擎
复制代码
<!-- Service Engine Configuration -->
        <engine name="entity-auto" class="org.ofbiz.service.engine.EntityAutoEngine"/>
        <engine name="group" class="org.ofbiz.service.group.ServiceGroupEngine"/>
        <engine name="interface" class="org.ofbiz.service.engine.InterfaceEngine"/>
        <engine name="java" class="org.ofbiz.service.engine.StandardJavaEngine"/>
        <engine name="simple" class="org.ofbiz.minilang.SimpleServiceEngine"/>
        <engine name="script" class="org.ofbiz.service.engine.ScriptEngine"/>
        <!-- Engines that can be replaced by the generic script engine -->
        <engine name="bsh" class="org.ofbiz.service.engine.BeanShellEngine"/>
        <engine name="groovy" class="org.ofbiz.service.engine.GroovyEngine">
            <parameter name="scriptBaseClass" value="org.ofbiz.service.engine.GroovyBaseScript"/>
        </engine>
        <engine name="jacl" class="org.ofbiz.service.engine.BSFEngine"/>
        <engine name="javascript" class="org.ofbiz.service.engine.ScriptEngine"/>
        <engine name="jpython" class="org.ofbiz.service.engine.BSFEngine"/><span style="display: none; width: 0px; height: 0px;" id="transmark"></span>
        <!--  -->
        <engine name="route" class="org.ofbiz.service.engine.RouteEngine"/>
        <engine name="http" class="org.ofbiz.service.engine.HttpEngine"/>
        <engine name="jms" class="org.ofbiz.service.jms.JmsServiceEngine"/>
        <engine name="rmi" class="org.ofbiz.service.rmi.RmiServiceEngine"/>
        <engine name="soap" class="org.ofbiz.service.engine.SOAPClientEngine"/>
        <engine name="ofbiz-workflow" class="org.ofbiz.workflow.WorkflowEngine"/>
        <engine name="workflow" class="org.ofbiz.shark.service.SharkServiceEngine"/>
属性名
 
是否必须
描述
name
Y
服务引擎的名字,必须唯一
class
Y
GenericEngine接口的实现类
服务引擎的一些配置
服务引擎的名称:<service-engine name = "default"/>

authorization 标签设置服务授权需要调用的服务。这个标签只有一个属性service-name:属性值应该是用来授权的服务名。默认使用通用的OFBiz的UserLogin服务

 <!-- Name of the service to use for authorization -->
        <authorization service-name="userLogin"/>
服务位置:
        <service-location name="main-rmi" location="rmi://localhost:1099/RMIDispatcher"/>
        <service-location name="main-http" location="http://localhost:8080/webtools/control/httpService"/>
 
        <service-location name="entity-sync-rmi" location="rmi://localhost:1099/RMIDispatcher"/>
        <service-location name="entity-sync-http" location="http://localhost:8080/webtools/control/httpService"/>
 
        <service-location name="rita-rmi" location="rmi://localhost:1099/RMIDispatcher"/>
        <service-location name="eedcc-test" location="http://localhost:8080/webtools/control/httpService"/>

服务引擎的线程池的配置
工作调度器(job scheduler)异步调用工作/服务。它包含池化的线程和几个请求线程。
thread-pool:该标签用来配置每个线程是怎操作。
<!-- Thread pool configuration (max/min threads, uses to live and time to live) -->
        <thread-pool send-to-pool="pool"
                     purge-job-days="4"
                     failed-retry-min="3"
                     ttl="18000000"
                     wait-millis="750"
                     jobs="10"
                     min-threads="5"
                     max-threads="15"
                     poll-enabled="true"
                     poll-db-millis="20000">
            <run-from-pool name="pool"/>
        </thread-pool>

如下属性可用
属性名
是否必须
描述
Ttl
Y
每个请求线程的存活时间。达到时间线程将被销毁
wait-millis
Y
每个请求线程在检查通过运行前休眠的时间
jobs
Y
每个请求线程在销毁之前可运行的工作数
min-threads
Y
线程池中保持的请求线程的最小数
max-threads
Y
线程池中将会创建请求线程的最大数
poll-enabledY为‘true’scheduler就会poll数据库来调度工作
poll-db-millisY如果线程池可用,本属性用来定义池化线程运行的频率
几种服务引擎
BeanShell(BSH):是用BeanShell脚本实现一个服务
服务组:你能用一组服务实现一个服务,结果是通过定义的服务组来调用其他服务
HTTP:http方式是通过另一台通过http通讯接口运行的服务引擎的一个服务,它是通过http或https运行的。指定URL联系到另一台服务器,发送请求到远程服务器,远程服务器通过处理后返回响应。
接口:这个是特殊的服务,其根本没有实现服务,只是设定了一个接口,它有属性定义。只是它的作用在于给其他服务继承和扩展
jacl:另一种脚本语言,基本上是java语言写的解释器。
java引擎:用java实现的静态方法(可能在OFBiz中使用较多的方式是这种)
RMI:远程调用接口,基于java的rmi方式调用
SOAP:另一个远程调用接口。将基于服务定义的自动化映射调用在另一台服务器的soap服务上
simple:用minilang实现的服务
 
服务的定义
服务定义在服务定义文件中,该文件一般在每个组件的servicedef目录下,习惯命名weiservices.xml
服务定义有一个唯一的名字,明确的服务引擎,明确的输入输出参数
参考:framework\common\servicedef\services.xml

<service name="createNote" engine="java"
        location="org.ofbiz.common.CommonServices" invoke="createNote">
        <description>Create a new note record</description>
        <attribute name="partyId" type="String" mode="INOUT" optional="true"/>
        <attribute name="noteName" type="String" mode="IN" optional="true"/>
        <attribute name="note" type="String" mode="IN" allow-html="safe"/>
        <attribute name="noteId" type="String" mode="OUT"/>
    </service>

服务元素
service的属性 详细参考framework/service/dtd/service.xsd
属性
是否必须
说明
缺省值
name
Y
服务的唯一名称
 
engine
Y
引擎的名称(在serviceengine.xml中定义)
 
location
N
位置或服务包类的包名
 
invoke
N
服务的方法名
 
auth
N
服务是否要求授权?(true/false)
true
debugN当调用服务时,是否启动详细调试?(true/false)true
default-entity-nameN使用自动属性(auto-attribute)的缺省实体 
exportN这个服务是否允许通过SOAP/HTTP/JMS访问?(true/false)false
validateN是否对服务参数的名字和类型进行验证?(true/false)true
max-retryN异步调用服务失败时,最大重复次数-1(无限次)
use-transactionN是否使用事务true
require-new-transactionN在使用事务的情况下是否启动新事务true
semaphoreN并发调用服务时使用的信号量:
none:当多个线程调用服务时,并发执行服务
wait:当多个线程调用服务时,采用排队的方法调用服务
fail:当服务正在被执行时,其他任何试图要调用该服务的线程都将调用失败
none
semaphore-wait-secondsN当sempaphore="wait"时,调用服务的线程要等待的超时时间,
超时调用失败
300
semaphore-sleepN当sempaphore="wait"时,1毫秒内做几次检查
(检查服务能否被调用)
300
transaction-timeoutN该服务的事务超时时间,当该服务启动新事务才有效0
 
属性
attribute的属性 详细参考framework/service/dtd/service.xsd
属性
是否必须
描述
缺省值
name
Y
这个是属性的名字
 
type
Y
对象的类型(String,java.util.Date)等
 
model
Y
这个参数是输入、输出或输入输出(IN/OUT/INOUT)
 
optional
N
这个参数是否可选?(true/false)
false
default-value
N
默认值
 
form-labelN对应的表单项显示名称 
entity-nameN对象名 
field-nameN属性名 
string-map-prefixNMap类型参数的前缀 
string-list-prefixNList类型参数的后缀 
form-displayN是否在表单中显示?(true/false)true

我们参考几个service的定义看看service
参考一:framework\service\servicedef\services.xm

<!-- simple condition implementation -->
    <service name="effectiveDateEcaCondition" engine="java" auth="false" use-transaction="false"
            location="org.ofbiz.service.ServiceUtil" invoke="genericDateCondition">
        <implements service="serviceEcaConditionInterface"/>
        <attribute name="fromDate" mode="IN" type="java.sql.Timestamp" optional="true"/>
        <attribute name="thruDate" mode="IN" type="java.sql.Timestamp" optional="true"/>
    </service>

参考二:applications/accounting/servicedef/services_finaccount.xml

<service name="refundGcPurchase" engine="java" require-new-transaction="true" use-transaction="true"
            location="org.ofbiz.accounting.payment.GiftCertificateServices" invoke="refundGcPurchase" auth="true">
        <description>Creates return for reload on failure</description>
        <attribute name="orderItem" type="org.ofbiz.entity.GenericValue" mode="IN" optional="false"/>
        <attribute name="partyId" type="String" mode="IN" optional="false"/>
    </service>

参考三:framework\service\servicedef\services.xml

<!-- Service Engine Services -->
    <service name="purgeOldJobs" engine="java"
            location="org.ofbiz.service.ServiceUtil" invoke="purgeOldJobs" auth="true" use-transaction="false" semaphore="wait">
        <description>Cleans out old jobs which have been around longer then what is defined in serviceengine.xml</description>
        <required-permissions join-type="AND">
            <check-permission permission="SERVICE_INVOKE_ANY"/>
        </required-permissions>
        <!-- this service has no attributes -->
    </service>






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值