blazeDS集成spring的remote访问
欢迎交流转载,请注明出处: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实现。
blazeDS集成spring的remote访问的更多相关文章
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- Spring Boot中集成Spring Security 专题
check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...
- Thymeleaf 集成spring
Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...
- Spring实战6:利用Spring和JDBC访问数据库
主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...
- SpringMVC 3.1集成Spring Security 3.1
这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...
- SpringMVC 3.2集成Spring Security 3.2
参考:http://www.cnblogs.com/Beyond-bit/p/springmvc_and_springsecurity.html SpringMVC 3.2集成Spring Secur ...
- 开涛spring3(7.5) - 对JDBC的支持 之 7.5 集成Spring JDBC及最佳实践
7.5 集成Spring JDBC及最佳实践 大多数情况下Spring JDBC都是与IOC容器一起使用.通过配置方式使用Spring JDBC. 而且大部分时间都是使用JdbcTemplate类(或 ...
- Spring系列之访问数据库
一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结合JDBC API的最佳实践和统一集成各种ORM方案,完成Java平台的数据访问. 二.JDBC API的最佳实践 Spring ...
- SpringBoot 集成Spring security
Spring security作为一种安全框架,使用简单,能够很轻松的集成到springboot项目中,下面讲一下如何在SpringBoot中集成Spring Security.使用gradle项目管 ...
随机推荐
- TC SRM 665 DIV2 A LuckyXor 暴力
LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...
- delphi 取得汉字的第一个字母
功能说明://取得汉字的第一个字母 function GetPYIndexChar( hzchar:string):char;begin caseWORD(hzchar[1])shl8+WORD(h ...
- MKMapView的内存释放问题
MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...
- 【虚拟化实战】VM设计之一vCPU
作者:范军 (Frank Fan) 新浪微博:@frankfan7 虚拟机需要多少个vCPU呢?是不是个数越多性能越好呢?这方面存在着很多误区.给VM配置CPU资源的时候,要精打细算才能最大可能的利用 ...
- Linux shell 脚本攻略之文件查找与文件列表
摘自:<Linux shell 脚本攻略>
- launch failed.Binary not found in Linux/Ubuntu解决方案
Linux下出现launch failed.Binary not found的解决方案: 首先当你把网上关于mingw的解决方案都看晕了的时候,告诉你,别看关于mingw的了.Linux下不用ming ...
- ASP.NET 之 网页快照 (DrawToBitmap)
一.添加引用 在解决方案上单击右键,选择“Add Reference...”,添加“System.Windows.Forms”,添加完后,Web.Config 中应该有类似下面的内容: <sys ...
- 使用c3p0连接池
首先我们需要知道为什么要使用连接池:因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),连接就会被自动释放掉,每次新建连接都需要140毫秒左右的时间而C3P0连接池会池化连接,随 ...
- css笔记18:盒子模型案例分析示范
- Maven 仓库
Maven仓库分为本地仓库和远程仓库(中央仓库,私服,其他公共仓库)一张图就可以很清楚的看出结构 本地仓库:本地仓库是在我们当前电脑系统盘的user-administrator-m2-reposito ...