2.4. Yet Another Hello to the World

The simple application in this section, which shows Flex and Java integration using BlazeDS, says "Hello." The example is first sketched out so that it will be easy to comprehend the functional flow and the rudimentary, yet working, use case. This sketch is shown in Figure 2-10.

Figure 2.10. Figure 2-10

Two types of remote calls are supported in this example application. These are:

  • A call to the sayHelloInString method on a Java object, which takes in a string parameter and returns a string value

  • A call to the sayHelloInObject method on the same Java object, which takes in an object parameter and returns an object

In the case where objects are passed back and forth between Flex and Java, parameters are sent from Flex as AS3 objects, and return values are sent from the remote Java service as Java objects. BlazeDS does the important translation and marshaling between these two languages.

To create this example application, start up your Flex Builder IDE and create a new Flex project. Choose to create a project with a J2EE server and specify the parameters as shown in Figure 2-11.

Figure 2.11. Figure 2-11

By choosing the option to create a joint Java/Flex project, you ease the process of development and deployment, as the entire bundle of source can be compiled and deployed together. In addition, you can configure a target server environment to deploy the build. I have chosen a JBoss AS server instance as the target.

As you move through the wizard for project creation, you will be prompted to provide a path to a blazeds.war file and to the context root of your target. The blazeds.war file can be anywhere in the file system. I have intentionally pointed it to an instance in one of my Tomcat folders. Figure 2-12 shows this wizard screen.

The final screen before you complete the project creation allows you to change the name of your default Flex application file and lets you provide the path to the application source. I choose to go with the defaults. Look at Figure 2-13 for specifics.

The project shell is ready, and now you can begin to code.

Figure 2.12. Figure 2-12

Figure 2.13. Figure 2-13

No Flex Builder? Don't Worry!

The "HelloBlazeDS" example is the simplest of the examples you will see in this book. I decided to go with Flex Builder for this one because I wanted to keep the focus on the essentials of remoting and on basic BlazeDS usage. In all other examples I will include illustrations, tips, or guidelines, which will help you setup, build, and deploy them without an IDE as well. In some cases, recommendations for building the examples using IntelliJ IDEA will also be included.


The task at hand requires that you do the following:

  • Create a Flex client that would invoke the remote service and consume the response returned by the service. The Flex code could reside in an MXML file called HelloBlazeDS.mxml.

  • Create a HelloService class on the Java server.

  • Make a remoting-config.xml entry to expose HelloService so that it's accessible from the Flex client.

  • Create Greeting classes both in Java and AS3 to exchange objects.

  • Build and run the application on the server (in my case JBoss AS. You could also choose to use Tomcat or any other server.)

The HelloBlazeDS.mxml Flex client code is printed in Listing 2-2.

Example 2.2. HelloBlazeDS.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="#FFFFFF">
    <mx:Script>
        <![CDATA[
            import problazeds.ch02.Greeting;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.controls.Alert;

            private var flexGreeting:Greeting = new Greeting();

            public function getGreetingFromFlex():Greeting {
                flexGreeting.sender = "Flex";
                flexGreeting.message = "Hello";
                flexGreeting.lastMessage = "";

                return flexGreeting;
            }


            public function handleResult(event:ResultEvent):void {
                // Handle result by populating the TextArea control.
                outputText.text=remoteService.sayHelloInText.lastResult;
            }

            public function handleFault(event:FaultEvent):void {
                // Handle fault.
                Alert.show(event.fault.faultString, "Fault");
            }
        ]]>
    </mx:Script>

    <!-- Connect to a service destination.-->
    <mx:RemoteObject id="remoteService"
        destination="helloService"
        result="handleResult(event);"
        fault="handleFault(event);"/>

    <mx:Panel>

        <!-- Provide input data for calling the service. -->
        <mx:TextInput id="inputText"/>

<!-- Call the hello service, use the text in a TextInput control as input data.-->

        <mx:Button click="remoteService.sayHelloInText(inputText.text)"/>

        <!-- Display results data in the user interface. -->
        <mx:TextArea id="outputText"/>

    </mx:Panel>

</mx:Application>

The Flex client code invokes a service called the HelloService. HelloService resides in a Java server that also hosts the BlazeDS files. The HelloService can reside in a single Java file as follows:

package problazeds.ch02;

public class HelloService {
        public Greeting sayHelloInObject(Greeting incomingGreeting) {
            greeting = new Greeting();
            greeting.setSender("Java");
            greeting.setLastMessage(incomingGreeting.getLastMessage());
            greeting.setMessage("Hello");

            return greeting;
       }

       public String sayHelloInText(String textGreeting) {
           return "Hello from Java" + "Received your message:" + textGreeting;
       }

       private Greeting greeting;

}

Flex client calls this Java service without explicitly specifying the path to the Java class name or the fully qualified class name. This is possible because all of this information is available through a configuration entry in remoting-services.xml, which is:

<destination id="helloService">
    <properties>
        <source>problazeds.ch02.HelloService</source>
    </properties>
</destination>

When exchanging objects between Flex and Java, you keep copies of the object definition on both sides. The AS3 class is:

package problazeds.ch02
{
    [Bindable]
    [RemoteClass(alias="problazeds.ch02.Greeting")]

public class Greeting
    {
             public var sender:String="";
             public var message:String="";
             public var lastMessage:String="";

    }
}

The RemoteClass metadata above the class keyword specifies an alias as problazeds.ch02.Greeting. The Bindable metadata annotation specifies that all changes to any of the class attributes are propagated to all points that consume this class using data binding. This is the Java object across the wire. The Java Greeting class is:

/**
 *
 */
package problazeds.ch02;

/**
 * @author Shashank Tiwari
 *
 */
public class Greeting {

       public String getSender() {
           return sender;
       }

       public void setSender(String sender) {
           this.sender = sender;
       }

       public String getMessage() {
           return message;
       }

       public void setMessage(String message) {
           this.message = message;
       }

       public String getLastMessage() {
           return lastMessage;
       }

       public void setLastMessage(String lastMessage) {
           this.lastMessage = lastMessage;
       }

       private String sender;
       private String message;
       private String lastMessage;

}

That covers all the elements that make up the example. The example is simple and further explanation may be redundant, so I will stop right here.

With the completion of this example, I am ready to wrap up this chapter. Let's recap what you learned in the last few pages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.147.193