Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3247916
  • 博文数量: 530
  • 博客积分: 13360
  • 博客等级: 上将
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-13 13:32
文章分类

全部博文(530)

文章存档

2017年(1)

2015年(2)

2013年(24)

2012年(20)

2011年(97)

2010年(240)

2009年(117)

2008年(12)

2007年(8)

2006年(9)

分类:

2010-05-20 20:46:38

12 远程调用(Remoting)

    Parsley is a client-side application framework and does not require any particular server-side technology to be useful. Nevertheless in this chapter we want to describe some common scenarios when integrating remoting solutions with Parsley.

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

    For a general discussion on how to integrate services into the application architecture see .

    如何集成服务到应用系统框架中参见.

    Of course apart from AMF based remoting solutions presented in this chapter you can also integrate HTTP Services or WebServices into your controller actions. The approach is similar to that presented in the MVC chapter: You write a controller action class that registers handlers for messages or events dispatched from the View or from Mediators, transform them into any kind of asynchronous service invocation, wait for the result, and finally dispatch an application message containing the result through Parsleys Messaging Framework.

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

12.1 FLEX远程服务(Flex Remoting)

    For remoting the framework can help you in two respects. First you can declare your RemoteObjects in a Parsley configuration class alongside other objects and inject them into your commands. Second (since version 2.2) you can use the convenient support for asynchronous commands to also route result and faults of remote invocations in a decoupled manner.

    对于远程服务,框架提供了二类帮助,首先你可以在parsley配置类中声明你的RemoteObjects,并注入这些类到你的命令类中。其次,你可以很方便地使用异步命令发送结果和错误。

Configuration

    Since Parsley offers MXML-based container configuration you can easily integrate the existing MXML tags for RemoteObjects with other Parsley configuration tags:

    因为parsley提供了MXML的配置信息,你可以为RemoteObjects 简单集成现有的MXML标签,如下所示:

            xmlns:mx="">

id="loginService"
destination="loginService"
showBusyCursor="true"/>




    You can then chose to inject those RemoteObjects into controller actions. But due to the nature of RemoteObjects not being type-safe you'd need to stick with id-based injection then:

    你可以选择注入那些RemoteObjects到控制器的方法。但由于RemoteObjects不是类型案例的,你需要用ID注入方式。

          [Inject(id="loginService")]
public var service:RemoteObject;
[Command]
public function login (event:LoginEvent) : AsyncToken {
return service.login(event.username, event.password);
}

    For Commands the framework will manage the AsyncToken for you. Other objects (or the same instance) can then listen for the results:

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

        [CommandResult]
private function loginResult (user:User, trigger:LoginEvent) : void {
[...]
}
[CommandError]
private function loginFault (fault:FaultEvent, trigger:LoginEvent) : void {
[...]
}

使用商业代码(Using BusinessDelegates)

    If you prefer to use delegates instead of injecting the RemoteObjects directly you could define both the RemoteObjects and the delegates in Parsley MXML:

    如果你想通过代理,而不是直接注入RemoteObjects,你可以在Parsley MXML中定义RemoteObjects和delegates。

    xmlns:mx=""
xmlns="">


import com.bookstore.services.*;
]]>


id="loginService"
destination="loginService"
showBusyCursor="true"
/>










    With delegates you can then return to injection by type:

    通过代理,你可以用类型注入方式:

     [Inject]
public var loginDelegate:LoginDelegate;

[Command]
public function login (event:LoginEvent) : AyncToken {
return loginDelegate.login(event.username, event.password);
}

12.2 Pimento数据服务(Pimento Data Services)

    Pimento integrates JPA/Hibernate and Spring with Flex, Flash and AIR clients. It is another Open Source Project under the Spicefactory umbrella. See the for more details.

    Pimento集成JPA/Hibernate和Flex, Flash 和 AIR的Spring。它是另一个开源项目。参见。

    Parsley includes custom configuration tags for Pimento for MXML and XML that allow you to define the Pimento configuration and custom services.

    Parsley包含为Pimento 提供了自定义配置标签,允许你定义Pimento配置信息和自定义服务。

MXML Example

    xmlns:mx=""
xmlns:pimento="/pimento">

url=""
timeout="3000"
/>




    This minimal setup is all that is required to be able to inject Pimentos AS3 EntityManager into any object:

    这是简单的设置,将Pimento AS3 EntityManager注入到任何一个对象:

     [Inject]
public var entityManager:EntityManager;

[Command]
public function deleteCart (message:DeleteCartMessage) : ServiceRequest {
return entitiyManager.remove(message.cart);
}

    You can additionally configure custom services with parameters and return values managed by Pimento:

    你可以额外配置带参数的自定义服务,并返回由Pimento管理的数值。

               name="loginService"
type="{LoginServiceImpl}"/>

    The service interfaces and remote stubs are usually generated by Pimentos Ant task. These services can then of course be injected into other objects, too.

    这种服务接口和远程桩通常由Pimentos Ant task生成。这些服务当然可以注入到任何对象。

XML Example

    xmlns=""
xmlns:pimento="/pimento"
xmlns:xsi=""
xsi:schemaLocation="
/schema/2.0/parsley-core.xsd
/pimento
/schema/2.0/parsley-pimento.xsd"
>
url=""
timeout="3000"
/>
name="loginService"
type="com.bookstore.services.LoginServiceImpl"
/>

    Since this is an XML extension it has to be initialized explicitly before using the XmlContextBuilder:

    因为这是一个XML扩展,所以必须在使用XmlContextBuilder前进行初始化:

          PimentoXmlSupport.initialize();

12.3Cinnamon远程服务(Cinnamon Remoting)

    If you don't need Pimentos data management capabilities and just want to use AMF-based Flex/Flash-to-Java Remoting you can stick with Cinnamon instead. See the for more details.

    如果你不想用Pimentos的数据管理功能,而想用基于AMF的FLEX/Flash-to-java远程服务,你可以使用Cinnamon。参见。

    Parsley includes custom configuration tags for Cinnamon for MXML and XML that allow you to define the channel and services.

    Parsley提供自定义配置标签,允许你定义管道和服务。

MXML Example

    xmlns:mx=""
xmlns:cinnamon="/cinnamon">


import com.bookstore.services.*;
]]>


url=""
timeout="3000"
/>
name="loginService"
type="{LoginServiceImpl}"
/>
name="cartService"
type="{CartServiceImpl}"
/>




    If you define only a single channel (like in most use cases) you don't have to explicitly refer to it in the service definitions. Parsley will automatically wire the single channel to all services then. In case of multiple channels you'd have to set the id property for the channel and reference it in service definitions:

    如果你仅定义了一个简单的管道,你不用明确在服务定义中指定它。parsley将自动注入简单管道到所有的服务。对于多个管道,你必须设置管理的ID属性,并在服务定义中指定它。

            id="mainChannel"
url=""
timeout="3000"/>

name="loginService"
type="{LoginServiceImpl}"
channel="mainChannel"/>

    You can then inject services into your actions:

    你也可以注入服务到你的方法中:

    [Inject]
public var loginService:LoginService;

[Command]
public function login (event:LoginEvent) : ServiceRequest {
return loginService.login(event.username, event.password);
}

    With Cinnamon there is no need for BusinessDelegates: The remote services implement business interfaces themselves, so you can directly inject them into actions. These interfaces are usually generated by Cinnamons Ant Task, automatically porting existing Java service interfaces to AS3.

    通过Cinnamon,我们就不需要BusinessDelegates:远程服务执行商业接口。因此,你可以直接注入他们到方法上。这些接口通常由Cinnamons Ant Task生成,自动将存在的JAVA服务接口与AS3关联。

XML Example

    xmlns=""
xmlns:pimento="/cinnamon"
xmlns:xsi=""
xsi:schemaLocation="
/schema/2.0/parsley-core.xsd
/cinnamon
/schema/2.0/parsley-cinnamon.xsd"
>
url=""
timeout="3000"
/>
name="loginService"
type="com.bookstore.services.LoginServiceImpl"
/>
name="cartService"
type="com.bookstore.services.CartServiceImpl"
/>

    Since this is an XML extension it has to be initialized explicitly before using the XmlContextBuilder:

    因为这是一个XML扩展,所以必须初始化XmlContextBuilder:

          CinnamonXmlSupport.initialize();
阅读(1350) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~