flex+spring3.0+blazds 数据推送整理版
最近有个项目要使用adobe Air 做客户端,需要用到推送功能,网上找不了少资料,很纠结。基本上没调试成功,即使跑起来了。AIR也接收不到数据,
花了一上午,调试了网上给的配置。算是修轮子吧。
blazds官方给了4个配置文件,我只用了3个。(proxy-config.xml被我删了,注意同时删掉services-config.xml 中的<service-include file-path="proxy-config.xml" />)
messaging-config.xml
<?xml version="1.0" encoding="UTF-8"?> <service id="message-service" class="flex.messaging.services.MessageService"> <adapters> <adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" /> <!-- <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/> --> </adapters> <default-channels> <channel ref="my-polling-amf"/> <channel ref="my-streaming-amf" /> </default-channels> </service>
remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 定义服务,并设置参数 --> <service id="remoting-service" class="flex.messaging.services.RemotingService"> <!-- 定义Adapter,将请求转换为java的对象 --> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> </adapters> <!-- 服务的默认通道,这个通道是在service-config.xml中定义的 --> <default-channels> <channel ref="my-amf"/> </default-channels> </service>
services-config.xml
<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <service-include file-path="remoting-config.xml" /> <service-include file-path="messaging-config.xml" /> </services> <security/> <channels> <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"> <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/> </channel-definition> <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>1000</server-to-client-heartbeat-millis> </properties> </channel-definition> <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel"> <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/> <properties> <add-no-cache-headers>false</add-no-cache-headers> </properties> </channel-definition> <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel"> <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/> <properties> <polling-enabled>true</polling-enabled> <polling-interval-seconds>4</polling-interval-seconds> </properties> </channel-definition> </channels> <logging> <target class="flex.messaging.log.ConsoleTarget" level="Info"> <properties> <prefix>[BlazeDS] </prefix> <includeDate>false</includeDate> <includeTime>false</includeTime> <includeLevel>false</includeLevel> <includeCategory>false</includeCategory> </properties> <filters> <pattern>Endpoint.*</pattern> <pattern>Service.*</pattern> <pattern>Configuration</pattern> </filters> </target> </logging> <system/> </services-config>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Spring and Flex</display-name> <description>Spring and Flex</description> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>RDSDispatchServlet</servlet-name> <display-name>RDSDispatchServlet</display-name> <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class> <init-param> <param-name>useAppserverSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>messageBrokerId</param-name> <param-value>_messageBroker</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping id="RDS_DISPATCH_MAPPING"> <servlet-name>RDSDispatchServlet</servlet-name> <url-pattern>/CFIDE/main/ide.cfm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
spring 配置
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <flex:message-destination id="simple-feed" /> <flex:message-broker> <flex:remoting-service default-channels="my-amf"/> <flex:message-service default-channels="my-streaming-amf" /> </flex:message-broker> <bean id="simpleFeedStarter" class="包路径.SimpleFeed" init-method="start"> <constructor-arg ref="defaultMessageTemplate" /> </bean> <bean id="defaultMessageTemplate" class="org.springframework.flex.messaging.MessageTemplate" /> <context:annotation-config /> <context:component-scan base-package="com" /> </beans>
SimpleFeed类
import java.util.Random; import org.springframework.flex.messaging.MessageTemplate; public class SimpleFeed { private static FeedThread thread; private final MessageTemplate template; public SimpleFeed(MessageTemplate template) { this.template = template; } public void start() { if (thread == null) { thread = new FeedThread(this.template); thread.start(); } } public void stop() { thread.running = false; thread = null; } public static class FeedThread extends Thread { public boolean running = false; private final MessageTemplate template; public FeedThread(MessageTemplate template) { this.template = template; } @Override public void run() { this.running = true; Random random = new Random(); double initialValue = 35; double currentValue = 35; double maxChange = initialValue * 0.005; while (this.running) { double change = maxChange - random.nextDouble() * maxChange * 2; double newValue = currentValue + change; if (currentValue < initialValue + initialValue * 0.15 && currentValue > initialValue - initialValue * 0.15) { currentValue = newValue; } else { currentValue -= change; } this.template.send("simple-feed", new Double(currentValue)); System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxx" + currentValue); try { Thread.sleep(1000); } catch (InterruptedException e) { } } } } }
SpringPush.mxml
<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.messaging.events.ChannelFaultEvent; import mx.messaging.events.MessageEvent; import mx.messaging.events.MessageFaultEvent; import mx.messaging.messages.IMessage; private function messageHandler(message:IMessage):void { Alert.show("sss"); pushedValue.text = "sss:"+ message.body; } protected function consumer_faultHandler(event:MessageFaultEvent):void { Alert.show("MessageFaultEvent:"+event.message.faultString); } protected function consumer_channelFaultHandler(event:ChannelFaultEvent):void { Alert.show("服务端链接关闭"); } protected function consumer_messageHandler(event:MessageEvent):void { pushedValue.text = "sss:"+ event.message.clientId; } protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void { consumer.subscribe(); } ]]> </fx:Script> <fx:Declarations> <s:ChannelSet id="cs"> <s:StreamingAMFChannel url="http://localhost:8080/flex/messagebroker/streamingamf"/> </s:ChannelSet> <s:Consumer id="consumer" destination="simple-feed" channelSet="{cs}" message="consumer_messageHandler(event)" fault="consumer_faultHandler(event)" channelFault="consumer_channelFaultHandler(event)"/> </fx:Declarations> <s:TextInput id="pushedValue" width="250" verticalCenter="0" horizontalCenter="0"/> <s:Button label="Subscribe" click="consumer.subscribe()" enabled="{!consumer.subscribed}" verticalCenter="30" horizontalCenter="-50"/> <s:Button label="Unsubscribe" click="consumer.unsubscribe()" enabled="{consumer.subscribed}" verticalCenter="30" horizontalCenter="50"/> </s:WindowedApplication>
相关依赖jar下载地址
https://files.cnblogs.com/montya/lib.part1.rar
https://files.cnblogs.com/montya/lib.part2.rar
https://files.cnblogs.com/montya/lib.part3.rar
https://files.cnblogs.com/montya/lib.part4.rar