Developer Resources:

Customer Quotes:

RTMP Data Push From Java to Flex

Data push is a mechanism of data delivery from server to the connected client applications. Data push between Java and Flex can be accomplished via HTTP polling or using a dedicated connection from the client program to the server-side process. The dedicated connection can use the RTMP protocol supported by Flash and Flex and also implemented by WebORB for Java. The example reviewed in this post uses the latter approach. The example establishes a connection from a Flex client to the WebORB for Java server process. The server-side code schedules a timer task to push a Java object to all connected clients. When the client program receives the object sent by the server it updates the user interface with the received information.

The example’s server-side code runs inside of the WebORB server. For the RTMP applications, WebORB requires a Java class which is the main handler of all events and actions for the given messaging application. Consider the following server-side Java code. The code is the handler class for the example:

The class shown above extends from weborb.messaging.WebORBApplication, so it can plug in into the product’s messaging framework. The appStart method is invoked when the WebORB server process is started (or when the application server is launched if WebORB is deployed into a 3rd party server). The implementation of the appStart method creates a timer which executes DataPushTask every second. The task creates an instance of the StockQuote class and delivers it to all connected clients by calling the pushDataToClients( StockQuote ) method. The actual data push occurs in the pushDataToClients method. The code obtains a collection of the client connections and iterates over it. For each connection the code invokes the “invoke” method. The method arguments are the name of the client-side function to invoke and the argument list to send to the client.

There are a few steps to deploy the Java code:

  1. Compile the code and copy the .class files into the corresponding folders under WEB-INF/classes. Specifically, the class files for the code shown above must be copied to the WEB-INF/classes/samples/datapush directory.
  2. Create and save the messaging application deployment file in WEB-INF/classes. Save the file with the name of datapushexample-web.xml. The file must contain the following:

  1. Notice the “datapushexample.” prefix in various IDs throughout the document. The prefix must be unique between all deployed messaging applications. Additionally, the bean definition with the “datapushexample.handler” ID references the handler class reviewed above. Finally, the “contextPath” property specifies the path of the application which will be used in the client code.

  2. Update beanRefContext.xml located in WEB-INF/classes, so it references datapushexample-web.xml from item #2.
  3. Restart WebORB for Java. If you are using the standalone version of the product, run “java -jar weborb.jar” from the command prompt (make sure to set the current directory to the WebORB’s installation directory).

At this point you are done with the server-side, so we’re shifting to Flex. Run Flex/Flash Builder and create a new Flex application project. See the article describing how to create a Flex/Flash Builder project to work with WebORB. The code below is the complete Flex application which connects to the server and processes data push messages:

Notice the quotePushed function. The function is invoked by the server and that is where the data push takes place. The StockQuote class referenced in the code is below:

Below is a screenshot of the application in action: