Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Using Flex with Java

0 (0 Likes / 0 Dislikes)
[ADOBE DEVELOPER CONNECTION] [James Ward, Technical Evangelist] Hi, I'm James Ward, a Technical Evangelist for Flex in Adobe. I want to show you three different ways that we can connect Flex applications to Java back-ends. I'm going to go through just a simple XML example, then a SOAP example, and then an AMF remoting example with BlazeDS. So let's walk through those different options. So first, let me show you what I have configured on my Java server. I have a census service object, and let's take a look at this object. So this object is very simple. It just has one method, getElements. It takes two parameters, 'begin' and 'account'. Then it goes out to the database using JWC, gets some records, and then returns them. So a simple Java back-end. Then I've configured this to be exposed as XML using a JSP. Let me show you that JSP. So here I have the census XML JSP, and in this JSP, what I do is I just iterate through that 'getElements' method call when it returned and output to XML. So let me show you in the browser what this really looks like. So I'm going to load up this JSP file, and it's going out to the server, fetching that data from the database. And then I return that XML for the 5000 rows that I specified. So that's a simple example of just how we can get some data out of the database. Now let's build the Flex application that will connect to that JSP. In Flash Builder, I'm going to create a new Flex project. And I'm going to call this one 'census test'. I'll specify that I want it to be a web application. I don't need to specify any particular server type for this application. And then I'll hit finish. So the first method that we're going to use in Flex to communicate with this Java back-end is just what's called the HTTP service. So in this declarations tag, I'm going to just create an instance of the HTTP service. I'll give it an ID, I'll call it 'srv', and I'm going to set the url to the url of that gsp. Now I need to specify a little bit more information here. In this application, I need to build the UI. So I'm going to add in a data grid, and I'm going to set the data provider on the data grid to actually watch this HTTP service for changes. And when it changes, then render the data into the data grid. So to do that, I'm going to use a binding expression. I'm going to bind to 'srv.lastresult'. That's just the last thing that we got back from calling that JSP. And then if we look back at this XML data, what we see is that there's an items node and then repeated item nodes. So the way that we actually get to the list of data that we want to render, is by using 'items.item' in the binding expression. Then the last thing here is to tell this application to go fetch that data. So to do that, I'm going to add an event handler for application complete, and then I'm going to call that HTTP service 'service.send'. And I'm going to do 'srv.send'. And in this case, I don't need to specify any parameters. So it's going to make the HTTP request when the application is initialized, and then the data binding will render the data into the data grid. So I'm going to save this and compile it. And now let's run it. So when I run this, it's going to load the Flex application. create the data grid--which initially doesn't know what its columns are-- fetch the data, and then, once we get the data back, it's going to render that data into the data grid and figure out, based on the data, what the columns should be. So that was pulling that data from the server, out of the database, into XML, and then rendering in Flex. So that's the first method for how we can communicate from a Flex application to a Java backend. And this method would also work with any back-end. It doesn't necessarily have to be Java. Now let me show you the next method. If we take a look back in this Java code at that census service object, what we see is that I've added some annotations here to this census service. I'm using the jacks standard in Java to actually expose this object out as a SOAP web service. And if we take a look at the web XML config file, we'll see where I actually give this SOAP web service a url. So here I've mapped that SOAP web service to this census SOAP service url. So we now pull up that WSDL for that service in our browser, so we can see what that WSDL looks like. So here's the WSDL that was automatically generated for that census service, and we can see that there is an operation for that 'get elements' method. And that takes that operation 'get elements' takes two parameters. So now I'm going to switch this Flex application from using that plain old XML JSP, to using a SOAP web service. Let me go into the declarations tag and change out this HTTP service instead for the web service tag. And I'm still going to leave the ID of SRV, but I'm going to take out this URL parameter and instead specify a WSDL parameter. And I'll put in the location of the WSDL. So now the next thing that I need to do is, I can't just buy in directly to this last element, 'lastresult.items.item'. I actually have to tell it what method is going to be returning the data. So I'm going to do 'srv.getElements.lastResult'. So this will return the last result from calling the getElements method. So now up here, in our application complete handler, instead of calling it srv.send, I'm going to call it 'srv.getElements' just like I was making that method call on that object on the server side, and then pass it the two parameters. Where do you want to start? We'll start at zero and go to 2000 rows. So that's all the parameters that we need there. So let's save that. It recompiles, and then back in the browser I can reload that application. Let's rerun it again here. So when I run this application again, we'll see that it looks very similar. At the beginning, we see the data grid without any columns. It's going out, fetching data. In this case, 2000 rows. We can see all 2000 rows there. We can sort this data on the client side. But this time, instead of using plain old XML, you used a SOAP web service. So that's the second option for how we can communicate from Flex to Java. So let me show you the last option. The last option with BlazeDS, we can use an optimized binary protocol called 'AMF', which is really fast for transferring data back and forth across the wire between Flex and Java. So to set that up, I downloaded BlazeDS, and I copied the libraries from BlazeDS into my WEB INF lib. We can see those there. And then I created a servlet-mapping in my web XML. So we can see this message broker servlet. And we can see the servlet there for the message broker. So now, what I've done is I've exposed an endpoint to be able to access BlazeDS. Then there's some Flex config files here. There's a remoting config. And the remoting config allows me to set up a destination for that census service that points to the census service object. So now, back in my Flex code, I'm going to make another change to this web service. Instead of a web service, I'm going to use what's called a 'remote object'. So remote object is going to use that AMF protocol to transfer the data back and forth between the Java server and the Flex client. And instead of a WSDL parameter, I'm going to specify an endpoint. And the endpoint is going to be 'http://localhost:8080/simple' that's my application--and then '/messagebroker'. Then AMF will tell it to use the AMF protocol. I also need to specify the destination to use, so I'm going to put in that census service destination. So now everything else will actually stay the same in my application. I'm still going to call the getElements method. I'm going to get 2000 rows, but ,instead of 2000, let's go up to 20,000, and then my data binding expression for my data grid stays the same. So let me save this and recompile it. And let's go reload the application in the browser. And now, when we run this application, we'll see that, amazingly fast, we get this 20,000 rows, all the way down from the server--which, in this case, isn't very far away-- and down to the client rendered in the data grid. So we're running a large amount of information, transferring across the wires, serializing and deserializing that data very quickly with AMF. [Flex and Java-adobe.com/devnet/flex/flex java.html] So you can use any of these options to connect Flex front ends to your Java back-ends. Thanks for watching. [ADOBE DEVELOPER CONNECTION]

Video Details

Duration: 8 minutes and 59 seconds
Country:
Language: English
License: All rights reserved
Genre: None
Views: 550
Posted by: adobetv on Oct 21, 2010

In this video, Adobe Technical Evangelist James Ward demonstrates three different ways to connect Flex applications to Java backends. His examples highlight XML, SOAP and AMF remoting with BlazeDS.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.