The following example applications show client-side and server-side code that you can compile and deploy to get started with LiveCycle Data Services ES. You typically use the following steps to build an application:
The LiveCycle Data Services ES installer creates a directory structure on your computer that contains all of the resources necessary to build applications. As part of the installation, the installer creates three web applications that you can use as the basis of your development environment. The lcds-samples web application contains many LiveCycle Data Services ES examples.
You can run the following examples if you compile them for the lcds-samples web application and deploy them to the lcds-samples directory structure. For more information on building and running the examples, see Building and deploying LiveCycle Data Services ES applications.
The Remoting Service is one of the RPC services included with LiveCycle Data Services ES. The Remoting Service lets clients access methods of Plain Old Java Objects (POJOs) on the server.
In this example, you deploy a Java class, EchoService.java, on the server that echoes back a String passed to it from the client. The following code shows the definition of EchoService.java:
package remoting;
public class EchoService
{
public String echo(String text) {
return "Server says: I received '" + text + "' from you";
}
}
The echo() method takes a String argument and returns it with additional text. After compiling EchoService.java, place EchoService.class in the WEB-INF/classes/remoting directory. Notice that the Java class does not have to import or reference any LiveCycle Data Services ES resources.
Define a destination, and reference one or more channels that transport the data. Configure EchoService.class as a remoting destination by editing the WEB-INF/flex/remoting-config.xml file and adding the following code:
<destination id="echoServiceDestination" channels="my-amf">
<properties>
<source>remoting.EchoService</source>
</properties>
</destination>
The source element references the Java class, and the channels attribute references a channel called my-amf.
Define the my-amf channel in WEB-INF/flex/services-config.xml, as the following example shows:
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>
The channel definition specifies that the Flex client uses a non-polling AMFChannel to communicate with the AMFEndpoint on the server.
The Flex client application uses the RemoteObject component to access EchoService. The RemoteObject component uses the destination property to specify the destination. The user clicks the Button control to invoke the remote echo() method:
<?xml version="1.0"?>
<!-- intro\intro_remoting.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%" height="100%">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
// Send the message in response to a Button click.
private function echo():void {
var text:String = ti.text;
remoteObject.echo(text);
}
// Handle the recevied message.
private function resultHandler(event:ResultEvent):void {
ta.text += "Server responded: "+ event.result + "\n";
}
// Handle a message fault.
private function faultHandler(event:FaultEvent):void {
ta.text += "Received fault: " + event.fault + "\n";
}
]]>
</mx:Script>
<mx:RemoteObject id="remoteObject"
destination="echoServiceDestination"
result="resultHandler(event);"
fault="faultHandler(event);"/>
<mx:Label text="Enter a text for the server to echo"/>
<mx:TextInput id="ti" text="Hello World!"/>
<mx:Button label="Send" click="echo();"/>
<mx:TextArea id="ta" width="100%" height="100%"/>
</mx:Application>
Compile the client application into a SWF file by using Flex Builder or the mxmlc compiler, and then deploy it to your web application.
The Messaging Service lets client applications send and receive messages from other clients. In this example, create a Flex application that sends and receives messages from the same LiveCycle Data Services ES destination.
Define the messaging destination in WEB-INF/flex/messaging-config.xml, as the following example shows:
<destination id="MessagingDestination" channels="my-amf-poll"/>
Define the my-amf-poll channel in WEB-INF/flex/services-config.xml, as the following example shows:
<channel-definition id="my-amf-poll" class="mx.messaging.channels.AMFChannel">
<endpoint
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpoll"
class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>1</polling-interval-seconds>
</properties>
</channel-definition>
This channel definition creates a polling channel with a polling interval of 1 second. Therefore, the client sends a poll message to the server every second to request new messages. Use a polling channel because it is the easiest way for the client to receive updates. Other options include polling with piggybacking, long-polling, and streaming.
The following Flex client application uses the Producer component to send a message to the destination, and the Consumer component to receive messages sent to the destination. To send the message, the Producer first creates an instance of the AsyncMessage class and then sets its body property to the message. Then, it calls the Producer.send() method to send it. To receive messages, the Consumer first calls the Consumer.subscribe() method to subscribe to messages sent to a specific destination.
<?xml version="1.0"?>
<!-- intro\intro_messaging.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%" height="100%"
creationComplete="consumer.subscribe();">
<mx:Script>
<![CDATA[
import mx.messaging.events.MessageFaultEvent;
import mx.messaging.events.MessageEvent;
import mx.messaging.messages.AsyncMessage;
import mx.messaging.Producer;
import mx.messaging.Consumer;
// Send the message in response to a Button click.
private function sendMessage():void {
var msg:AsyncMessage = new AsyncMessage();
msg.body = "Foo";
producer.send(msg);
}
// Handle the received message.
private function messageHandler(event:MessageEvent):void {
ta.text += "Consumer received message: "+ event.message.body + "\n";
}
// Handle a message fault.
private function faultHandler(event:MessageFaultEvent):void {
ta.text += "Received fault: " + event.faultString + "\n";
}
]]>
</mx:Script>
<mx:Producer id="producer"
destination="MessagingDestination"
fault="faultHandler(event);"/>
<mx:Consumer id="consumer"
destination="MessagingDestination"
fault="faultHandler(event);"
message="messageHandler(event);"/>
<mx:Button label="Send" click="sendMessage();"/>
<mx:TextArea id="ta" width="100%" height="100%"/>
</mx:Application>
Compile the client application into a SWF file by using Flex Builder or the mxmlc compiler, and then deploy it to your web application.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/livecycle/8.2/programLC/programmer/lcds/lcoverview_4.html