首页 > 代码库 > Java+Flex+Blaze推送
Java+Flex+Blaze推送
在上篇Flex+Java+Blazeds 建立了项目的基础上,实现Flex订阅,Java发送消息的推送功能。
一、运行环境:Flex4.6+Jdk1.7+Blazedz某版本+Tomcat6
此处注意,使用的服务器是Tomcat6,如果使用Tomcat7还存在问题,不能实现推送,在网上查过此问题,有人说换Blazeds4版本可以实现,尝试过没有实现,尚待解决。
二、代码
1、Java端
(1)新建类Person,用于前后台传输数据
package com.java; public class Person { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
(2)新建Servlet——HelloServlet.java,用于调用发送信息的线程
package com.java; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import flex.messaging.MessageBroker; import flex.messaging.messages.AsyncMessage; import flex.messaging.util.UUIDUtils; public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 6331466335470329744L; // 线程 private static FeedThread thread; public HelloServlet() { super(); } public void destroy() { super.destroy(); } public void init() throws ServletException { super.init(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cmd = request.getParameter("cmd"); if (cmd.equals("start")) { start(); } if (cmd.equals("stop")) { stop(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doPost(request, response); } // 启动监听 public void start() { if (thread == null) { thread = new FeedThread(); thread.start(); } System.out.println("start!!!"); } // 停止监听 public void stop() { thread.running = false; thread = null; } // 循环发送消息进程 public static class FeedThread extends Thread { public boolean running = true; public void run() { // 总是无法获取msgBroker,web.xml需要有MessageBrokerServlet的配置信息 MessageBroker msgBroker = MessageBroker.getMessageBroker(null); String clientID = UUIDUtils.createUUID(); int i = 0; while (running) { Person person = new Person(); person.setAge(i); person.setName(String.valueOf(i)); System.out.println(i); AsyncMessage msg = new AsyncMessage(); msg.setDestination("person-data-feed"); msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME, "person"); msg.setClientId(clientID); msg.setMessageId(UUIDUtils.createUUID()); msg.setTimestamp(System.currentTimeMillis()); msg.setBody(person); msgBroker.routeMessageToService(msg, null); i++; try { Thread.sleep(2000); } catch (InterruptedException e) { } } } } }
2、Flex端
(1)新建PersonClient.mxml应用程序,用于订阅消息
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.messaging.ChannelSet; import mx.messaging.Consumer; import mx.messaging.channels.StreamingAMFChannel; import mx.messaging.events.MessageEvent; import vo.Person; protected function button1_clickHandler(event:MouseEvent):void { var consumer:Consumer=new Consumer(); consumer.destination="person-data-feed"; consumer.subtopic="person"; consumer.addEventListener(MessageEvent.MESSAGE,messageHandler); var url:String ="http://localhost:8080/FlexToJava/"; var myStreamingAMF:StreamingAMFChannel = new StreamingAMFChannel(url+"my-streaming-amf",url+"messagebroker/streamingamf"); var channelSet:ChannelSet = new ChannelSet(); channelSet.addChannel(myStreamingAMF); consumer.channelSet= channelSet; //consumer.channelSet = newChannelSet(["my-streaming-amf"]); consumer.subscribe(); //开始接收 Alert.show("消费者初始化完毕!"); } protected function messageHandler(event:MessageEvent):void { var person:Person=event.message.body as Person; this.lb_age.text=person.age.toString(); this.lb_name.text=person.name; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Label id="lb_age" x="113" y="61" text="Label"/> <s:Label id="lb_name" x="113" y="81" text="标签"/> <s:Button x="164" y="54" label="订阅" click="button1_clickHandler(event)"/> </s:Application>
(2)新建Person.as,用于接收后台Person对象
package vo { import mx.rpc.remoting.RemoteObject; [RemoteClass(alias="com.java.Person")] //用于和java后台类转换alias为java类的命名空间,否则不能转换 [Bindable] public class Person { public function Person() { } public var age:int; public var name:String; } }
3、配置文件
(1)WebRoot\WEB-INF\flex\messaging-config.xml
增加如下一段代码
<destination id="person-data-feed"> <properties> <server> <allow-subtopics>true</allow-subtopics> <subtopic-separator>.</subtopic-separator> </server> </properties> <channels> <channel ref="my-polling-amf" /> <channel ref="my-streaming-amf" /> </channels> </destination>
(2)WebRoot\WEB-INF\flex\services-config.xml
增加如下一段代码
<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel"> <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint" > </endpoint> <properties> <idle-timeout-minutes>0</idle-timeout-minutes> <max-streaming-clients>100</max-streaming-clients> <server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis> <user-agent-settings> <user-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="8"/> <user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="8"/> </user-agent-settings> </properties> </channel-definition>
(3)WebRoot\WEB-INF\web.xml
建立Servlet的时候可能会已有,如没有,增加如下一段代码
<servlet> <description>This is the description of my J2EE component </description> <display-name>This is the display name of my J2EE component </display-name> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.java.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping>
4、运行
(1)启动Servlet中的线程
http://localhost:8080/FlexToJava/HelloServlet?cmd=start
(2)启动Flex页面
点击订阅,将后台的结果动态显示
(3)关闭Servlet中的线程
http://localhost:8080/FlexToJava/HelloServlet?cmd=stop
演示完毕!
Java+Flex+Blaze推送
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。