| By Victor Rasputnis, Yakov Fain, Anatole Tartakovsky | Article Rating: |
|
| October 12, 2007 11:00 AM EDT | Reads: |
28,681 |
Configuring Flex Messaging Destination
Let's start
by configuring the topic on the Flex side. We will modify the file
messaging-config.xml in /WEB-INF/flex, and create the destination for
the "ticker-topic-jms" as in Listing 9.
Please note the difference between the destination ID
"ticker-topic-jms," which will be used by the Flex client, and the
jndi-name "ticker," which the Flex JMS adapter will use for look up in
the JNDI tree. The middleman subscriber created by the Flex JMS adapter
won't be durable; it's okay to lose the quote if you aren't online.
Note the channel tags. The RTMP channel is listed first, and the AMF polling is the second option. This means the following: try the push first, but if it doesn't work (because of firewalls), start polling.
Configuring ActiveMQ JMS
There are different ways
to start ActiveMQ. For purposes of this chapter it's convenient to
start ActiveMQ with the same process that's running the Tomcat when the
Web application starts. Accordingly, we use a Web application context
listener and start the ActiveMQ broker from the contextInitialized()
method:
public void contextInitialized(ServletContextEvent arg0) {
try{
broker.addConnector("tcp://localhost:61616?trace=true");
broker.start();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
Naturally, we have to guarantee the availability of the ActiveMQ classes on the Web application class path. To that end we dropped activemq-4.0-M4.jar into the common/lib folder off the Tomcat root. Listing 10 is the application listener.
To register the listener with the Web application we modify the web.xml file and add the following XML snippet (according to the DTD it should go right after the filter mappings. If your app does not have a filter, the placement is right after the application context parameters):
<listener>
<listener-class>com.theriabook.jms.ActiveMQBrokerListener</listener-class>
</listener>
Writing the TickerFeed Java Program
In our data
feeding program the quotes will be published to a JMS topic, but we are
still going to reuse the class Porfolio.java, which we used to pull
quotes via the RemoteObject. When it comes to sending the message, we
will be putting the return of the Portofolio getQuotes() method into
the serializable QuotesHolder:
Portfolio port = new Portfolio();
. . . .
StockQuoteDTO[] quotes = port.getQuotes();
QuotesHolder holder = new QuotesHolder();
The listing for the QuotesHolder class is below:
package com.theriabook.jms;
import java.io.Serializable;
import com.theriabook.jms.dto.StockQuoteDTO;
public class QuotesHolder implements Serializable{
private static final long serialVersionUID = -8823588238987890758L;
public StockQuoteDTO[] quotes;
}
To avoid hard-coding the JNDI properties in the TickerFeed program, we will make sure that the file jndi.properties as shown in Listing 11 belongs to the application classpath.
Now we're ready to complete our TickerFeed.java program. It continuously publishes the QuotesHolder objects to the JMS topic called "ticker." On your console you should see output similar to this one:
Publishing quotes...
Publishing quotes...
The complete code for TickerFeed.java is in Listing 12.
Modifying the Flex Client to Consume Messages
The
final part is to make changes to the Flex client. We will use the last
version of PortfolioView from the remoting series and make the
following modifications.
First, we will replace the <mx:RemoteObject> tag with the following <mx:Consumer> tag:
<mx:Consumer id="consumer" destination="ticker-topic-jms" message="applyQuotes(event.message.body.quotes)" />
This is an example of decoupling the application components. If you need to change the MOM provider, or you decide to use message queues instead of topics, no code modification is needed. Just change the destination parameter and you're set.
The second change is to actually start consuming messages. The modified startQuotes will look as follows:
private function startQuotes():void{
consumer.subscribe();
}
The third and last change is in the processFault method, due to the differences in mx.messaging.events.ChannelFaultEvent instead of mx.rpc.events.FaultEvent:
private function processFault(evt:ChannelFaultEvent):void{
errorText = "Server Error";
errorTip = evt.faultDetail;
startQuotes();
}
In all other respects the JMS code specific to the PortfolioView is the same as in its remoting counterpart (see Listing 13).
Summary
In this excerpt we went through different
ways of establishing RPC communications between Flex and Java. In
particular, we've been using RemoteObject, HTTPService, and Flex JMS
adapters to establish communications.
Published October 12, 2007 Reads 28,681
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Victor Rasputnis
Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at vrasputnis@faratasystems.com
More Stories By Yakov Fain
Yakov Fain is a Managing Director of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. He is an Adobe Certified Flex Instructor. Currently Yakov works on the book for O'Reilly "Enterprise Application Development with Flex". He twits at twitter.com/yfain.
More Stories By Anatole Tartakovsky
Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at atartakovsky@faratasystems.com
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe & Salesforce Cut Cloud Deal
- Adobe Fiddles with its Web Apps
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF





















