欢迎交流转载,请注明出处:http://www.cnblogs.com/shizhongtao/p/3490037.html
上一篇我只是简单实用blazeds创建了一个实例,大多数开发中,都是结合spring的spring BlazeDS Integration来进行开发。首先你要去官方网站上下载个jar包,我这里提供了我项目中的所有jar包的打包供你测试本示例实用,当然里面有很多多余jar包。项目中所有jar包地址(如地址问题,请联系博主)。
集成开始,当然你要创建一个web项目,然后配置web.xml如下,这个就多添加了个spring的servlet,来代替"flex.messaging.MessageBrokerServlet"这个servlet。
<?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>BlazeDS Spring Integration Application</display-name>
<description>BlazeDS Spring Integration Application</description> <!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring Dispatcher Servlet -->
<servlet>
<servlet-name>flex</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 就是你项目中的‘applicationContext.xml’,作用一样 -->
<param-value>/WEB-INF/flex-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>flex</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>
然后修改/WEB-INF/flex-servlet.xml这个配置文件,为了使用 Spring BlazeDS Integration 的 tag,增加命名空间flex。我还是文件内容全部贴上,说明写到注释里面吧。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.bing">
</context:component-scan>
<!-- 请求路由给 MessageBroker -->
<flex:message-broker>
<flex:message-service default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf,my-amf" />
</flex:message-broker>
<!-- <flex:message-broker /> -->
<!-- 定义 Bean,并用 remoting-destination tag 把它暴露给 Flex,因为我用了annotation的方式,所以下面的注解掉了 -->
<!-- <bean id="myService" class="com.bing.service.MyServiceImpl" />
<flex:remoting-destination ref="myService" /> -->
</beans>
然后修改/WEB-INF/flex/services-config.xml这个配置文件,默认的话这个文件夹是有四个文件的(services-config.xml、remoting-config.xml、proxy-config.xml、messaging-config.xml),集成spring后,其他三个都没有什么用了,我删掉了。
<?xml version="1.0" encoding="UTF-8"?>
<services-config> <!-- <services>
<service-include file-path="remoting-config.xml" />
<service-include file-path="proxy-config.xml" />
<service-include file-path="messaging-config.xml" />
</services> -->
<services>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
</services>
<security>
<login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
<!-- Uncomment the correct app server
<login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
<login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>
<login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
<login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
--> <!--
<security-constraint id="basic-read-access">
<auth-method>Basic</auth-method>
<roles>
<role>guests</role>
<role>accountants</role>
<role>employees</role>
<role>managers</role>
</roles>
</security-constraint>
-->
</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-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>
<channel-definition id="my-longpolling-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amflongpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>5</polling-interval-seconds>
<wait-interval-millis>60000</wait-interval-millis>
<client-wait-interval-millis>1</client-wait-interval-millis>
<max-waiting-poll-requests>200</max-waiting-poll-requests>
</properties>
</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" />
<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>
<user-agent-settings>
<!--
MSIE 5, 6, 7 default max number of permanent HTTP connections is
2.
-->
<user-agent match-on="MSIE" kickstart-bytes="2048"
max-streaming-connections-per-session="1" />
<!-- MSIE 8 max number is 6. -->
<user-agent match-on="MSIE 8" kickstart-bytes="2048"
max-streaming-connections-per-session="5" />
<!-- Firefox 1, 2 max number is 2. -->
<user-agent match-on="Firefox" kickstart-bytes="2048"
max-streaming-connections-per-session="1" />
<!-- Firefox 3 max number is 6. -->
<user-agent match-on="Firefox/3" kickstart-bytes="2048"
max-streaming-connections-per-session="5" />
<!-- Safari 3, 4 max number is 4. -->
<user-agent match-on="Safari" kickstart-bytes="2048"
max-streaming-connections-per-session="3" />
<!-- Chrome 0, 1, 2 max number is 6. -->
<user-agent match-on="Chrome" kickstart-bytes="2048"
max-streaming-connections-per-session="5" />
<!-- Opera 7, 9 max number is 4.-->
<user-agent match-on="Opera" kickstart-bytes="2048"
max-streaming-connections-per-session="3" />
<!-- Opera 8 max number is 8. -->
<user-agent match-on="Opera 8" kickstart-bytes="2048"
max-streaming-connections-per-session="7" />
<!-- Opera 10 max number is 8. -->
<user-agent match-on="Opera 10" kickstart-bytes="2048"
max-streaming-connections-per-session="7" />
</user-agent-settings>
</properties>
</channel-definition>
<!--
<channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
</channel-definition> <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</channel-definition>
-->
</channels> <logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<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>
<redeploy>
<enabled>false</enabled>
<!--
<watch-interval>20</watch-interval>
<watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
<touch-file>{context.root}/WEB-INF/web.xml</touch-file>
-->
</redeploy>
</system> </services-config>
基本的配置差不多都好了,当然你得加入jar包。下面就开始编写代码部分。代码是网上拷贝的,自己懒得写了。
创建实体类,模拟用,其实你可以这返回一句话。
package com.bing.vo; public class MyEntity {
private String frstName;
private String lastName;
private String emailAddress; public String getFirstName() {
return frstName;
} public void setFirstName(String frstName) {
this.frstName = frstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public String getEmailAddress() {
return emailAddress;
} public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
创建service接口类及其实现类。
package com.bing.service; public interface MyService {
String getMyEntities();
}
package com.bing.service; import java.util.ArrayList;
import java.util.List; import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Component; import com.bing.vo.MyEntity;
import com.thoughtworks.xstream.XStream;
@Component("myService")
@RemotingDestination(channels = { "my-amf" })
public class MyServiceImpl implements MyService { @Override
@RemotingInclude
public String getMyEntities() {
List<MyEntity> list = new ArrayList<MyEntity>();
MyEntity entity = new MyEntity();
entity.setFirstName("Hello");
entity.setLastName("World");
entity.setEmailAddress("hello@world.com");
list.add(entity);
MyEntity entity2 = new MyEntity();
entity2.setFirstName("你好");
entity2.setLastName("jone");
entity2.setEmailAddress("hello@space.com");
list.add(entity2);
XStream x=new XStream();
x.alias("vo", MyEntity.class);
System.out.println(x.toXML(list));
return x.toXML(list);
} }
后台代码基本搞定,就是返回一个xml的对象字符串。中间用了一下Xstream这个jar工具。发布一下项目到tomcat下,然后启动看看……
前台就简单了,打开flash builder,创建一个flash项目,和上一篇例子基本一样。不过这里我把输出的路径改到tomcat下了。
然后编写成徐代码:进行测试
<?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.channels.StreamingAMFChannel;
import mx.messaging.messages.IMessage;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent; private function dotest():void {
//var myStreamingAMF:StreamingAMFChannel = new StreamingAMFChannel("my-amf", "/blazedsfirst/messagebroker/amf"); /* var myStreamingAMF:StreamingAMFChannel=new StreamingAMFChannel();
var channelSet:ChannelSet=new ChannelSet();
channelSet.addChannel(myStreamingAMF);
remoteObject.channelSet=channelSet;*/
remoteObject.getMyEntities();
}
// Handle the recevied message.
private function resultHandler(event:ResultEvent):void {
Alert.show(event.result.toString());
} // Handle a message fault.
private function faultHandler(event:FaultEvent):void {
Alert.show("Received fault: " + event.fault);
} ]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="remoteObject"
destination="myService"
result="resultHandler(event);"
fault="faultHandler(event);"/> </fx:Declarations> <s:layout>
<s:VerticalLayout gap="0" horizontalAlign="center" paddingTop="50"/>
</s:layout>
<s:Label text="测试spring集成remote"/>
<s:Button label="Send" click="dotest();"/> </s:Application>
直接运行进行测试,弹出结果如下:
<list>
<vo>
<frstName>Hello</frstName>
<lastName>World</lastName>
<emailAddress>hello@world.com</emailAddress>
</vo>
<vo>
<frstName>你好</frstName>
<lastName>Space</lastName>
<emailAddress>hello@space.com</emailAddress>
</vo>
</list>
下一篇我将介绍一下,push推送技术,如何利用spring和blazeds实现。