Parsley 开发指南 14 远程访问

14 远程访问

Parsley 是一个客户端应用程序的框架,并不需要任何特定的服务器端技术。不过,在本章中,我们想描述一些远程调用解决方案。

如何集成服务到应用系统框架中参见 11 构建MVC架构

除了基础的AMF的远程解决方案,在本章中你也可以融入HTTP服务或WebServices到控制器。该方法类似于在MVC章介绍的:你写一个控制器动作类,用于处理从视图中派发的消息,等待结果,最后发送消息,其中包含一个parsley消息。

14.1 Flex 远程服务

对于远程服务,框架可以在两个方面帮助你,首先你可以在parsley配置类中声明你的RemoteObjects,并注入这些类到你的命令类中。其次(从2.2版开始),你可以很方便地使用异步命令发送远程调用的结果和错误用一个解耦的方式。

Configuration

因为Parsley提供了基于MXML的容器配置,你可以使用其他的Parsley配置标签为RemoteObjects 方便的集成现有的MXML标签,如下所示:

<Objects 

    xmlns:fx="http://ns.adobe.com/mxml/2009"

    xmlns="http://www.spicefactory.org/parsley">

    <fx:Declarations>

        

        <fx:RemoteObject 

            id="loginService" 

            destination="loginService" 

            showBusyCursor="true"

        />

        <!-- other object definitions -->

        

    </fx:Declarations>

   </Objects>

你可以选择那些RemoteObjects注入到控制器中。但由于RemoteObjects的本质不是类型安全的,你需要继续使用ID注入:

[Inject(id="loginService")]

public var service:RemoteObject;

public function execute (message: LoginMessage): AsyncToken {

return service.login(message.username message.password);

}

对于命令,框架负责为你管理AsyncToken。其它对象可以监听结果:

[CommandResult]

public function loginResult (user: User trigger:LoginMessage) : void {

    [...]

}

[CommandError]

public function loginFault (fault: Fault trigger:LoginMessage) : void {

[...]

}

注意,如果你想要命令本身的结果处理器,你不得不省略两个元数据和触发器参数,因为这个映射信息不是必需的。框架知道它必须为该命令的产生的结果调用命令中结果处理程序。

Using BusinessDelegates

如果你更喜欢使用代理而不是直接注入RemoteObjects,你可以在MXML中定义代理和RemoteObjects 。

<Objects 

    xmlns:fx="http://ns.adobe.com/mxml/2009"

    xmlns="http://www.spicefactory.org/parsley">

    <fx:Script>

        <![CDATA[

            import com.bookstore.services.*;

        ]]>

    </fx:Script>

    

    <fx:Declarations>

    

        <fx:RemoteObject 

            id="loginService" 

            destination="loginService" 

            showBusyCursor="true"

        />

        <Object id="loginDelegate" type="{LoginDelegate}">

            <ConstructorArgs>

                <ObjectRef idRef="loginService"/>

            </ConstructorArgs>

        </Object>

       

        <!-- other objects -->

        

    </fx:Declarations>

    

</Objects>

 使用代理你可以通过类型注入:

[Inject]

public var loginDelegate:LoginDelegate;

public function execute (message: LoginMessage): AyncToken {

return loginDelegate.login(message.username message.password);

}

14.2 Pimento Data Services

Pimento集成JPA/Hibernate和Flex, Flash 和 AIR的Spring。.这是Spicefactory的另一个开源项目,详细查看 Pimento Info Page 

Pimento 有支持Parsley的扩展(在Parsley下载页面),包含为Pimento 提供了自定义配置标签,允许你定义Pimento配置信息和自定义服务。

MXML Example

<Objects 

    xmlns:fx="http://ns.adobe.com/mxml/2009"

    xmlns="http://www.spicefactory.org/parsley"

    xmlns:pimento="http://www.spicefactory.org/parsley/pimento">

    

    <fx:Declarations>

    

        <pimento:Config

            url="http://localhost:8080/test/service/"

            timeout="3000"

        />

    

        <!-- other objects -->

        

    </fx:Declarations>

    

</Objects>

这种最小限度的设置所需要的是能够注入Pimento AS3 EntityManager到任何一个对象(在本例中是一个命令映射到一个消息):

[Inject]

public var entityManager:EntityManager;

public function execute (message:DeleteCartMessage) : ServiceRequest {

return entitiyManager.remove(message.cart);

}

你可以另外使用参数和返回值配置自定义的服务来让Pimento托管:

<pimento:Service

    name="loginService"

type="{LoginServiceImpl}"

/>

服务接口和远程存根通常是由Pimentos  Ant Task生成。这些服务当然也可以被注入到其他对象。

XML Example

<objects 

    xmlns="http://www.spicefactory.org/parsley"

    xmlns:pimento="http://www.spicefactory.org/parsley/pimento"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.spicefactory.org/parsley 

      http://www.spicefactory.org/parsley/schema/2.3/parsley-core.xsd 

    http://www.spicefactory.org/parsley/pimento 

      http://www.spicefactory.org/parsley/schema/2.3/parsley-pimento.xsd"

    >

    <pimento:config 

        url="http://localhost/test/service/"

        timeout="3000"

    />

    <pimento:service

        name="loginService"

        type="com.bookstore.services.LoginServiceImpl"

/>

</objects>

由于这是一个XML扩展,必须使用XmlContextBuilder之前显式地进行初始化:

PimentoXmlSupport.initialize();

14.3 Cinnamon Remoting

如果你不需要Pimentos 数据管理功能,只想用基于AMF的Flex/Flash到Java的远程服务,你可以使用Cinnamon。

Pimento 对Parsley 的扩展支持(在Parsley下载页面)包括Cinnamon 自定义配置标签,MXML和XML,允许你定义通道和服务。

MXML Example

<Objects 

    xmlns:fx="http://ns.adobe.com/mxml/2009"

    xmlns="http://www.spicefactory.org/parsley"

    xmlns:cinnamon="http://www.spicefactory.org/parsley/cinnamon">

    <fx:Script>

        <![CDATA[

            import com.bookstore.services.*;

        ]]>

    </fx:Script>

    

    <fx:Declarations>

        

        <cinnamon:Channel

            url="http://localhost:8080/test/service/"

            timeout="3000"

        />

        <cinnamon:Service

            name="loginService"

            type="{LoginServiceImpl}"

        />

        <cinnamon:Service

            name="cartService"

            type="{CartServiceImpl}"

        />

    

        <!-- other objects -->

        

    </fx:Declarations>

    

</Objects>

如果你只定义一个通道(就像在大多数用例)你不必显式地引用它的服务定义。Parsley会自动连接单通道到所有服务。如果有多个通道你不得不设置通道的id属性并且在服务定义中引用它:

<cinnamon:Channel

    id="mainChannel"

    url="http://localhost:8080/test/service/"

    timeout="3000"

/>

<cinnamon:Service

    name="loginService"

    type="{LoginServiceImpl}"

channel="mainChannel"

/>

然后你可以把服务注入到你的命令中(或者其他对象)

[Inject]

public var loginService:LoginService;

public function execute (event:LoginMessage) : ServiceRequest {

return loginService.login(event.username event.password);

}

使用Cinnamon 没有必要用BusinessDelegates:远程服务实现业务接口本身,所以你可以直接将它们注入到Action中。这些接口通常由Cinnamons Ant Task生成,自动移植现有的Java服务接口到AS3中。

XML Example

<objects 

    xmlns="http://www.spicefactory.org/parsley"

    xmlns:pimento="http://www.spicefactory.org/parsley/cinnamon"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.spicefactory.org/parsley 

      http://www.spicefactory.org/parsley/schema/2.3/parsley-core.xsd 

    http://www.spicefactory.org/parsley/cinnamon 

      http://www.spicefactory.org/parsley/schema/2.3/parsley-cinnamon.xsd"

    >

    <cinnamon:channel

        url="http://localhost:8080/test/service/"

        timeout="3000"

    />

    <cinnamon:service

        name="loginService"

        type="com.bookstore.services.LoginServiceImpl"

    />

    <cinnamon:service

        name="cartService"

        type="com.bookstore.services.CartServiceImpl"

/>

</objects>

由于这是一个XML扩展,必须在使用XmlContextBuilder之前显式地初始化:

CinnamonXmlSupport.initialize();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值