欢迎交流转载,请注明出处: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访问的更多相关文章

  1. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  2. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  3. Thymeleaf 集成spring

    Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...

  4. Spring实战6:利用Spring和JDBC访问数据库

    主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...

  5. SpringMVC 3.1集成Spring Security 3.1

    这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...

  6. SpringMVC 3.2集成Spring Security 3.2

    参考:http://www.cnblogs.com/Beyond-bit/p/springmvc_and_springsecurity.html SpringMVC 3.2集成Spring Secur ...

  7. 开涛spring3(7.5) - 对JDBC的支持 之 7.5 集成Spring JDBC及最佳实践

    7.5 集成Spring JDBC及最佳实践 大多数情况下Spring JDBC都是与IOC容器一起使用.通过配置方式使用Spring JDBC. 而且大部分时间都是使用JdbcTemplate类(或 ...

  8. Spring系列之访问数据库

    一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结合JDBC API的最佳实践和统一集成各种ORM方案,完成Java平台的数据访问. 二.JDBC API的最佳实践 Spring ...

  9. SpringBoot 集成Spring security

    Spring security作为一种安全框架,使用简单,能够很轻松的集成到springboot项目中,下面讲一下如何在SpringBoot中集成Spring Security.使用gradle项目管 ...

随机推荐

  1. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  2. Android自己定义DataTimePicker(日期选择器)

    Android自己定义DataTimePicker(日期选择器)  笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...

  3. Open gl 的不规则图形的4联通种子递归填充和扫描线种子递归填充算法实现

    实验题目:不规则区域的填充算法 实验目的:验证不规则区域的填充算法 实验内容:利用VC与OpenGL,实现不规则区域的填充算法. 1.必做:实现简单递归的不规则区域填充算法. 2.选做:针对简单递归算 ...

  4. springmvc03 非注解和注解处理器映射器和适配器

    1其它非注解处理器映射器和适配器 .1BeanNameUrlHandlerMapping(映射器) 根据请求url(XXXX.action)匹配spring容器bean的 name 找到对应的bean ...

  5. HOW MYSQL USES INTERNAL TEMPORARY TABLES

    HOW MYSQL USES INTERNAL TEMPORARY TABLES Table of Contents [hide] 1)UNION queries 2)Some views 3)SQL ...

  6. 1050. String Subtraction (20)

    this problem  is from PAT, which website is http://pat.zju.edu.cn/contests/pat-a-practise/1050. firs ...

  7. 用PC浏览器模拟手机浏览器(一):无扩展版

    想浏览手机版,打开对应网址却跳转到PC版?怎么办? 下面咱们来说下在只是安装了浏览器,无需其他安装操作的情况下来怎么用PC浏览器模拟手机浏览器,然后访问手机站点. 浏览器众多,IE系列的咱就不考虑了, ...

  8. 01 MySQL锁概述

    锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O 等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有 ...

  9. Debian下的PPPOE服务器配置

    参考: http://blog.csdn.net/zhangwenjianqin/article/details/7655375 http://blog.sina.com.cn/s/blog_8043 ...

  10. centos、linux改变ll命令显示颜色

    服务器用的centOS,用putty管理时,在命令行下目录用蓝色显示,很难看清.可以用以下两种方法改变: 方法一: 1.直接修改个人帐户目录下的.color.rc文件. 找到 DIR 01;34,修改 ...