java 推送消息给 flex,java服务器向flex客户端一对一推送数据

结合其他前辈所编写的采用BlazeDS的发布-订阅机制实现的一对多的实现,在此基础上采用flex的消息服务的选择过滤功能实现一对一.(其中Tick文件和TickCacheServlet主要部分和flex_blazeds.mxml部分代码为网上前辈所作)

Tick.java

packagecn.bestwiz.design.tc;

importjava.math.BigDecimal;

importjava.util.Date;

publicclassTick {

privateBigDecimal askPrice;

privateBigDecimal bidPrice;

privateBigDecimal midPrice;

privateDate tickTime;

privateString seqno;

publicString getSeqno() {

returnseqno;

}

publicvoidsetSeqno(String seqno) {

this.seqno = seqno;

}

publicBigDecimal getAskPrice() {

returnaskPrice;

}

publicvoidsetAskPrice(BigDecimal askPrice) {

this.askPrice = askPrice;

}

publicBigDecimal getBidPrice() {

returnbidPrice;

}

publicvoidsetBidPrice(BigDecimal bidPrice) {

this.bidPrice = bidPrice;

}

publicBigDecimal getMidPrice() {

returnmidPrice;

}

publicvoidsetMidPrice(BigDecimal midPrice) {

this.midPrice = midPrice;

}

publicDate getTickTime() {

returntickTime;

}

publicvoidsetTickTime(Date tickTime) {

this.tickTime = tickTime;

}

}

TickCacheServlet.java

packagecn.bestwiz.design.tc.servlet;

importjava.io.IOException;

importjava.util.HashMap;

importjava.util.Map;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importcn.bestwiz.design.tc.Tick;

importflex.messaging.MessageBroker;

importflex.messaging.messages.AsyncMessage;

importflex.messaging.util.UUIDUtils;

publicclassTickCacheServletextendsHttpServlet {

privatestaticfinallongserialVersionUID = 1L;

privatestaticFeedThread thread;

protectedvoiddoGet(HttpServletRequest req, HttpServletResponse resp)

throwsServletException, IOException {

//      String cmd = req.getParameter("cmd");

//      if (cmd.equals("start")) {

//          start();

//      }

//      if (cmd.equals("stop")) {

//          stop();

//      }

doPost(req, resp);

}

protectedvoiddoPost(HttpServletRequest req, HttpServletResponse resp)

throwsServletException, IOException {

start();

}

publicvoiddestroy() {

super.destroy();

}

publicvoidinit()throwsServletException {

super.init();

}

publicvoidstart() {

if(thread ==null) {

thread = newFeedThread();

thread.start();

}

System.out.println("start!!");

}

publicvoidstop() {

thread.running = false;

thread = null;

}

publicstaticclassFeedThreadextendsThread {

publicbooleanrunning =true;

publicvoidrun() {

MessageBroker msgBroker = MessageBroker.getMessageBroker(null);

String clientID = UUIDUtils.createUUID();

inti =0;

while(running) {

i++;

Tick tick = newTick();

tick.setSeqno(String.valueOf(i));

System.out.println(i);

AsyncMessage msg = newAsyncMessage();

msg.setDestination("tick-data-feed");

msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME, "tick");

/*

* 在此添加过滤

*/

Map headers=newHashMap();

headers.put("prop1",10);

msg.setHeaders(headers);

msg.setClientId(clientID);

msg.setMessageId(UUIDUtils.createUUID());

msg.setTimestamp(System.currentTimeMillis());

msg.setBody(tick);

msgBroker.routeMessageToService(msg, null);

try{

Thread.sleep(2000);

} catch(InterruptedException e) {

}

}

}

}

}

flex_blazeds.mxml

creationComplete="submsg()"height="378"width="426">

Tick.as

package

{

[RemoteClass(alias="cn.bestwiz.design.tc.Tick")]

[Bindable]

publicclassTick

{

publicvaraskPrice:Number;

publicvarbidPrice:Number;

publicvarmidPrice:Number;

publicvartickTime:Date;;

publicvarseqno:String;

}

}

triggerServlet.mxml为促发servlet服务

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"minWidth="955"minHeight="600">

}

]]-->

在messging-config.xml中添加destination

true

.

在services-config.xml中添加channel

0

10

5000

在web.xml文件中添加

This is the description of my J2EE component

This is the display name of my J2EE component

TickCacheServlet

cn.bestwiz.design.tc.servlet.TickCacheServlet

TickCacheServlet

/TickCacheServlet

至此大功告成,首先将项目发布到tomcat上,然后先运行triggerservlet订阅,在运行flex_blazeds.mxml会看到结果。

,在未发布(触发)之前,订阅端flex_blazeds.mxml是接收不到任何消息,触发之后则能够接到消息.

测试可用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值