Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Java EE and Flex, Part 2: Can we talk?

Communicating between Flex clients and Java EE servers

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

There are a variety of ways a Flex client can communicate with your back-end Java server, so it's good to explore your options. In this follow up to his introduction to rich-client development with Flex and Java EE, Dustin Marx shows you how to connect a Flex client and Java enterprise back-end via SOAP-based Web services and object remoting. He also introduces BlazeDS and demonstrates its support for proxied HTTPService and WebService connections and object remoting.

The first part of this two-article series covers some of the advantages that Flex offers Java developers who want to deliver rich-client experiences in the Web browser. That article introduces key pieces of Flex such as MXML (Flex's XML grammar), ActionScript 3, Cascading Style Sheets (CSS) syntax support, some of Flex's built-in components, and creation of custom Flex components that extend the built-in components. It concludes by introducing Flex's out-of-the-box HTTPService, which enables a Flex client to communicate with a Java EE server via HTTP.

In this article I'll introduce you to more advanced ways to comfortably integrate Flex clients into enterprise Java applications. You'll learn about more out-of-the-box communication options, namely Flex's WebService and RemoteObject classes. I'll also discuss BlazeDS, an open source web messaging and Java remoting solution from Adobe that complements Flex.

Using Flex with Web services

The HTTPService is not the only mechanism that Flex provides for out-of-the-box communication with a remote server. Flex also supports communication with a SOAP-based Web service, via the WebService object.

Preliminary setup

You need the Flex SDK to build the examples in this article. If you don't already have it, follow the download and installation instructions in "Java EE and Flex: A compelling combination, Part 1." You can build all the examples with the command-line SDK and tools, but some readers may prefer to use Adobe's Flex Builder.

To illustrate Flex communication with SOAP-based Web services, I need a Web Services Description Language (WSDL) file. For the example in this article, I'll take advantage of Java EE 5's ability to generate WSDL files from classes annotated with the appropriate Java API for XML-Based Web Services (JAX-WS) annotations. (In situations where top-down Web service design is preferred, a preexisting WSDL file can be used directly.) Listing 1 shows the annotated Java class, which will also provide the server-side functionality invoked by the Web service call.

Listing 1. AuthorServer.java: Web service implementation

package javaworld.dustin.endpoints;

import java.util.HashMap;
import java.util.Map;
import javaworld.dustin.common.Author;
import javax.jws.WebService;
import javax.jws.WebMethod;

/**
 * Simple class to be exposed as a Web service endpoint to illustrate using
 * Flex with Web services. This particular examples also happens to return
 * information about the requested JavaWorld author.
 * 
 * @author Dustin
 */
@WebService(name="AuthorServer",
            serviceName="AuthorService",
            targetNamespace="http://www.javaworld.com/articles/authors/dustin"
)
public class AuthorServer
{
   /** Authors information. */
   private static final Map<String, Author> authors =
      new HashMap<String, Author>();

   static
   {
      final Author marx = Author.newInstance(
         "Marx", "Dustin",
         "http://marxsoftware.blogspot.com/",
         "Author of 'JSP Best Practices' and 'More JSP Best Practices'");
      authors.put(marx.getFullName(), marx);

      final Author holub = Author.newInstance(
         "Holub", "Allen",
         "http://www.holub.com",
         "Author of 'Why Getter and Setter Methods are Evil' and 'Why Extends is Evil'");
      authors.put(holub.getFullName(), holub);

      final Author venners = Author.newInstance(
         "Venners", "Bill",
         "http://www.artima.com/consulting.html",
         "Author of 'Designing with Exceptions' and 'Inheritance Versus Composition'");
      authors.put(venners.getFullName(), venners);

      final Author hunter = Author.newInstance(
         "Hunter", "Jason",
         "http://www.servlets.com/jason/",
         "Author of 'New Features Added to Servlet 2.5'");
      authors.put(hunter.getFullName(), hunter);

      final Author sletten = Author.newInstance(
         "Sletten", "Brian",
         "http://www.nofluffjuststuff.com/conference/speaker/brian_sletten.html",
         "Author of 'REST for Java Developers, Part 1'");
      authors.put(sletten.getFullName(), sletten);
   }

   /**
    * Provide the biography of the author whose last name and first name are
    * provided.
    * 
    * @param authorLastName Last name of author for which biography information
    *    is sought.
    * @param authorFirstName First name of author for which biography information
    *    is sought.
    * @return Biography information for specified author.
    */
   @WebMethod
   public String obtainAuthorBiography(
      final String authorLastName, final String authorFirstName)
   {
      return authors.get(
         Author.buildFullName(authorFirstName, authorLastName)).getBiography();
   }

   /**
    * Provide the biography of the author whose name is provided.
    * 
    * @param authorFullName Full name of author for which biography is desired.
    * @return Biography information for specified author.
    */
   @WebMethod
   public String obtainAuthorBiographyWithFullName(final String authorFullName)
   {
      return authors.get(authorFullName).getBiography();
   }
}

The AuthorServer class in Listing 1 contains internal data intended only to demonstrate Flex calling Java on the back end. In a real application, this data would likely be retrieved from the database or other single data source.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

xlentBy Anonymous on June 19, 2009, 8:38 pmvery nicely explained with example code

Reply | Read entire comment

Source CodeBy Anonymous on March 3, 2009, 3:50 pmGreat articles. Do you have the source code from javaworld.dustin.common.Author and javaworld.dustin.remoting anywhere for download. I would like to get these examples...

Reply | Read entire comment

Be careful with crossdomain.xmlBy jlward4th on February 13, 2009, 10:39 amGreat article! One comment... For testing it's ok to have a "*" crossdomain.xml policy. But you have to be very careful with those in production. My two general...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources

More from JavaWorld