Java Server Push To Flex (ServerPush)

Java Server Push To Flex (ServerPush)
2010年06月14日
  在网上找了许多的Flex与java 通信做ServerPush 的例子,发现有许多不正确,今天就做了一小程序。希望对大家有用
  1. Product 类 package com.east.flex.serverpush.entity; /** * 产品类 * @author East(张栋芳) * * 2010-6-12 */ public class Product { private String month; private int profit; private int expenses; private int amount; public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public int getProfit() { return profit; } public void setProfit(int profit) { this.profit = profit; } public int getExpenses() { return expenses; } public void setExpenses(int expenses) { this.expenses = expenses; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } 2. ServerPushThread package com.east.flex.serverpush.chart; import java.util.ArrayList; import java.util.List; import java.util.Random; import com.east.flex.serverpush.entity.Product; import flex.messaging.MessageBroker; import flex.messaging.messages.AsyncMessage; import flex.messaging.util.UUIDUtils; /** * 构造数据后台线程 * * @author East(张栋芳) * * 2010-6-13 */ public class ServerPushThread extends Thread { public boolean running = true; @Override public void run() { MessageBroker msgBroker = MessageBroker.getMessageBroker(null); String clientId = UUIDUtils.createUUID(); while (running) { /* ============================== */ // TODO do someing /* ============================== */ List productList = getProductList(); AsyncMessage msg = new AsyncMessage(); msg.setDestination("serverpush"); msg.setClientId(clientId); msg.setMessageId(UUIDUtils.createUUID()); msg.setTimestamp(System.currentTimeMillis()); /* 设置消息信息,发布到客户端去 */ msg.setBody(productList); msgBroker.routeMessageToService(msg, null); try { Thread.sleep(10000); } catch (InterruptedException se) { } } } /** * 模拟数据信息 * * @return */ public List getProductList() { List productList = new ArrayList(); for (int i = 1; i --> 5. remoting-config.xml com.east.flex.serverpush.CreateProduct com.east.flex.serverpush.chart.ServerPushServer 6. Product.as package com.east.flex.entity { import mx.rpc.remoting.RemoteObject; [RemoteClass(alias="com.east.flex.serverpush.entit y.Product")] public class Product { public var month:String; public var profit:int; public var expenses:int; public var amount:int; public function Product() { } } } 7.ServerPushChart.mxml import com.east.flex.entity.Product Flex Server Push 功能*/ import mx.messaging.messages.IMessage; import mx.messaging.events.MessageEvent; import mx.utils.ArrayUtil; import mx.rpc.events.ResultEvent; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.messaging.ChannelSet; import mx.messaging.Consumer; [Bindable] private var p:Product; private var array:ArrayUtil; [Bindable] private var expensesAC:ArrayCollection = null; public var consumer2:Consumer = null; public function messageHandler(event:MessageEvent):void { linechart.dataProvider = event.message.body; trace("server push data to client.............."); } public function remoteServerCallback(event:ResultEvent):void { trace("Server is start...."); //Alert.show("Server is start....."+event.message); } public function startServer(event:MouseEvent):void { server.start(); } public function stopServer(event:MouseEvent):void { server.stop(); } /* public function submsg():void { Alert.show("click start"); consumer= new Consumer(); consumer.destination = "serverpush"; consumer.subtopic = "tick"; consumer.channelSet = new ChannelSet(["my-streaming-amf"]); consumer.addEventListener(MessageEvent.MESSAGE, messageHandler); consumer.subscribe(); Alert.show("click end"); } */ ]]-->
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值