结合其他前辈所编写的采用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是接收不到任何消息,触发之后则能够接到消息.
测试可用。