Can I use a remote object in flex to poll a java class on the server(Blazeds)? I want to return live information into my datagrid
So here is the code I have so far:
Flex Code
<?xml version="1.0" encoding="utf-8"?>
<fx:Declarations>
<s:RemoteObject id="getStockPrices" result="result(event)" destination="blazeDsService"
endpoint="http://localhost:8400/flexspring/messagebroker/streamingamf"/>
</fx:Declarations>
<mx:DataGrid x="10" y="295" width="910" height="211" creationComplete="getStockPrices.getQuotes();"
dataProvider="{getStockPrices.getQuotes.lastResult}" >
<mx:columns>
<mx:DataGridColumn headerText="Stock Ticker" dataField="name" />
<mx:DataGridColumn headerText="Price" dataField="price"/>
<mx:DataGridColumn headerText="Hi" dataField="col3"/>
<mx:DataGridColumn headerText="Low" dataField="col4"/>
<!--<mx:DataGridColumn headerText="Adverage" dataField="col5"/>
<mx:DataGridColumn headerText="Graph" dataField="col6"/>-->
</mx:columns>
</mx:DataGrid>
<mx:Button label="Retrieve Stocks" click="retrieveStocks()"/>
And this is the java Class file. This returns and arraylist:
package flex;
import java.util.ArrayList; import java.util.List; import java.util.Random;
import org.springframework.flex.remoting.RemotingDestination; import org.springframework.flex.remoting.RemotingInclude; import org.springframework.stereotype.Service;
import supportingClasses.StockQuote;
@Service
@RemotingDestination
public class BlazeDsService {
private static final String[] MASTER_LIST = {"C", "FNM", "FRE", "F", "GOOG", 开发者_JAVA技巧"AIG", "CSCO", "MSFT", "AAPL", "YHOO", "BSX", "PORT","F", "TNT", "ESP", "RET", "VBN", "EES"};
@RemotingInclude
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;
}
}
At the minute a button needs to be pressed to refresh the data. I want to get rid of this and poll the information every x seconds. Can this be done?
Do I need to change my set-up? create some publish - subscribe pattern and use Streaming channels?
Thanks
If you just want to call the getQuotes method at every X seconds you can use a timer in Flex, no need for messaging. If you want to be notified when a stock value changed you can use a consumer on the flex side which is subscribed to the same destination where the server is adding messages. You can find a sample doing just that in the BlazeDS samples folder, under traderdesktop folder.
精彩评论