14 Remote Access
Parsley is a framework of client applications and does not require any specific server-side technology. However, in this chapter, we want to describe some remote call solutions.
For how to integrate services into the application system framework, see 11.
In addition to the basic AMF remote solutions, you can also integrate HTTP services or WebServices into the Controller in this chapter. This method is similar to the introduction in the MVC chapter: you write a controller handler class to process messages distributed from the view, wait for the results, and finally send messages, which contains a parsley message.
14.1 flex remote service
For remote services, the framework can help you in two aspects. First, you can declare your remoteobjects In the parsley configuration class and inject these classes into your command class. Secondly (starting from Version 2.2), you can easily use Asynchronous commands to send remote call results and errors in a decoupling method.
Configuration
Because parsley provides mxml-based container configuration, you can use other parsley configuration labels to integrate existing mxml labels conveniently for remoteobjects, as shown below:
<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>
You can inject remoteobjects to the Controller. However, because the nature of remoteobjects is not type-safe, you need to continue to use ID injection:
[Inject (ID = "loginservice")]
Public var service: remoteobject;
Public Function execute (message: loginmessage): asynctoken {
Return service. login (message. username, message. Password );
}
For commands, the Framework manages the asynctoken for you. Other objects can listen to the results:
[Commandresult]
Public Function loginresult (User: User, trigger: loginmessage): void {
[...]
}
[Commanderror]
Public Function loginfault (fault: fault, trigger: loginmessage): void {
[...]
}
Note: If you want the result processor of the command itself, you have to omit two metadata and trigger parameters because the ing information is not required. The framework knows that it must call the result processing program in the command for the result generated by the command.
Using businessdelegates
If you prefer to use a proxy instead of Directly Injecting remoteobjects, you can define the proxy and remoteobjects in mxml.
<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>
You can use the proxy to inject data by type:
[Inject]
Public var logindelegate: logindelegate;
Public Function execute (message: loginmessage): aynctoken {
Return logindelegate. login (message. username, message. Password );
}
14.2 pimento Data Services
Pimento integrates spring of JPA/hibernate and flex, flash and air .. This is another open-source project of spicefactory. For more information, see pimento info page.
Pimento supports parsley extensions (on the parsley download page), including providing custom configuration tags for pimento, allowing you to define pimento configuration information and custom services.
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>
This minimal setting requires the ability to inject pimento as3 entitymanager to any object (in this example, a command is mapped to a message ):
[Inject]
Public var entitymanager: entitymanager;
Public Function execute (message: deletecartmessage): servicerequest {
Return entitiymanager. Remove (message. Cart );
}
You can also use parameters and return values to configure custom services for pimento hosting:
<Pimento: Service
Name = "loginservice"
Type = "{loginserviceimpl }"
/>
Service interfaces and remote stubs are usually generated by pimentos ant tasks. These services can also be injected into other objects.
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>
Because this is an XML extension, you must initialize it explicitly before using xmlcontextbuilder:
Pimentoxmlsupport. initialize ();
14.3 cinnamon remoting
If you do not need the pimentos data management function, you can use cinnamon to use the remote service from Flex/flash to Java based on AMF.
Pimento's extended support for parsley (on the parsley download page) includes cinnamon custom configuration tags, mxml and XML, allowing you to define channels and services.
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>
If you define only one channel (like in most use cases), you do not need to explicitly reference its service definition. Parsley automatically connects a single channel to all services. If there are multiple channels, you have to set the channel ID attribute and reference it in the service definition:
<Cinnamon: Channel
Id = "mainchannel"
Url = "http: /localhost: 8080/test/service /"
Timeout = "3000"
/>
<Cinnamon: Service
Name = "loginservice"
Type = "{loginserviceimpl }"
Channel = "mainchannel"
/>
Then you can inject the service into your command (or other objects)
[Inject]
Public var loginservice: loginservice;
Public Function execute (Event: loginmessage): servicerequest {
Return loginservice. login (event. username, event. Password );
}
There is no need to use businessdelegates to use cinnamon: the remote service implements the business interface itself, so you can directly inject them into the action. These interfaces are usually generated by cinnamons ant task, and the existing Java service interfaces are automatically transplanted to 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>
Because this is an XML extension, it must be explicitly initialized before using xmlcontextbuilder:
Cinnamonxmlsupport. initialize ();