The NoteBook of EricKong

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

In my previous tutorial on BlazeDS, I went over an example over how to setup remote objects with data push. There are however, a lot of instances where someone just wanted to receive data and they aren’t concerned with it being updated., so it turns more into a ‘client requesting data whenever it wants it’ scenario. This article will not be nearly as indepth as the last blazeDS article, but if you have not setup your environment for BlazeDS before, it will probably be in your best interest to check it out.

Lets begin by making sure that we have our server and BlazeDs war file. Both of them can be found here:

Tomcat 6
BlazeDS

Once again, we are going to extract the contents into our BlazeDS server project. This time, we are only going to edit remoting-config.xml. Make sure that the following lines are in there.

<default-channels>
   <channel ref="my-amf"/>
</default-channels>
<destination id="BlazeDsService"> 
  <properties> 
     <source>com.codeofdoom.BlazeDsService</source> 
  </properties> 
</destination>

We tell the remoting-config to use the channel “my-amf”, which is already defined with the services-config.xml file. Next we are telling it to use the the destination “BlazeDsService”. This is what our remote object will be looking at. The properties also contains a source. This source is going to be a java class with the methods it needs in order to process the calls from the front end. You must make sure you include the fully qualified name, so we will call it com.codeofdoom.BlazeDsService. So what does this file look like?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.codeofdoom;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import flex.messaging.io.ArrayCollection;
 
public class BlazeDsService {
    private static final String[] MASTER_LIST = {"C", "FNM", "FRE", "F", "GOOG", "AIG", "CSCO", "MSFT", "AAPL", "YHOO", "BSX", "PORT","F", "TNT", "ESP", "RET", "VBN", "EES"};
    public BlazeDsService(){}
    public List<StockQuote> getQuotes(){
        List<StockQuote> list = new ArrayList<StockQuote>();
        Random r = new Random();
        for (String s:MASTER_LIST){
            StockQuote sq = new StockQuote();
            sq.setName(s);
            sq.setPrice(r.nextInt(50));
            list.add(sq);
        }
        return list;
    }
}

Yep. Thats it. It not implementing anything crazy, not extending any service adapters, anything like that. It’s just a java class with some functions on it. Next we are going to create the object that we will be sending back and forth. In this example, we are going to be simulating retrieving some stock data, so I called the class StockQuote.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.codeofdoom;
 
public class StockQuote {
    private String name;
    private int price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}

On the front end, we are going to need the matching object, and here that is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.codeofdoom.dto
{
    [Bindable]
    [RemoteClass(alias="com.codeofdoom.StockQuote")]
    public class StockQuote{
        private var _name:String
        private var _price:int;
 
        public function get name():String{
            return _name;
        }
        public function get price():int{
            return _price;
        }
        public function get name(name:String):void{
            _name = name;
        }
        public function get price(price:int):void{
            _price = price;
        }
    }
}

I mentioned this in the last article, but I am going to mention this again, just to stress the importance. When we are using Remote Object, we must make sure that

  • All the properties are the same.
  • We have getters and setters for all properties on back the java side and the AS side.
  • We must have our AS object set as Bindable and the RemoteClass set to the path of our remote object.
  • We must make sure that the constructors match. I have seen where flex will swallow the error message if they don’t match, making debugging a pain.

Now lets take a look at the mxml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;
            import mx.messaging.messages.IMessage;
            import mx.messaging.events.MessageAckEvent;
            import mx.messaging.messages.AsyncMessage;
            import mx.messaging.events.MessageEvent;
 
            public function retrieveStocks():void{
                ro.getQuotes();
            }
            private function result(e:ResultEvent):void{
                stockChart.dataProvider = e.result as ArrayCollection;
            }
        ]]>
    </mx:Script>
    <mx:RemoteObject id="ro" result="result(event)" destination="BlazeDsService"/>
    <mx:VBox width="100%">
        <mx:BarChart width="100%" id="stockChart">
            <mx:horizontalAxis>
                <mx:CategoryAxis categoryField="name"/>
            </mx:horizontalAxis>
            <mx:series>
                <mx:ColumnSeries xField="name" yField="price"/>
            </mx:series>
        </mx:BarChart>
        <mx:Button label="Retrieve Stocks" click="retrieveStocks()"/>
    </mx:VBox>   
</mx:Application>

Once again, not much to it. There are three areas of importance.

  • Our remote object is that is subscribed to our destination that we declared within our remoting-config.xml.
  • The retrieveQuotes function is what calls our backend. Note that it just calls ‘getQuotes’ directly from the remote object.
  • The result function that is set on the RemoteObject is what comes back from the back end. Now a lot of examples will do something to the grid such as dataprovider = {ro.getQuotes.lastResult}. We could do the same here, but i wanted to give you a chance to inspect the object to see what exactly is coming back.

Once you click the Retrieve Stocks button, it then calls the getQuotes() function on the remote object. That then builds our List of objects and passes them to the front end. When it comes back, we then just apply it to the dataprovider of our chart.

I really wanted to take the time out to show you some other ways of handling your data within BlazeDS. It is an extremely powerful tool that every Flex developer should have under their belt. Its a lot less cumbersome than standard calls to the back end and the amount of data that is sent back and forth is a lot smaller than sending plane XML over the wire. Good luck!

Here is the source

posted on 2011-07-02 16:59 Eric_jiang 阅读(484) 评论(0)  编辑  收藏 所属分类: Flex

只有注册用户登录后才能发表评论。


网站导航: