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。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
 3 <web-app>
 4 
 5     <display-name>BlazeDS Spring Integration Application</display-name>
 6     <description>BlazeDS Spring Integration Application</description>
 7     
 8     <!-- Http Flex Session attribute and binding listener support -->
 9     <listener>
10         <listener-class>flex.messaging.HttpFlexSession</listener-class>
11     </listener>
12 
13     <listener>
14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15     </listener>
16 
17     <!-- Spring Dispatcher Servlet -->   
18     <servlet>
19         <servlet-name>flex</servlet-name>
20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
21         <init-param>
22         <param-name>contextConfigLocation</param-name>
23         <!-- 就是你项目中的‘applicationContext.xml’,作用一样 -->
24         <param-value>/WEB-INF/flex-servlet.xml</param-value>
25     </init-param>
26         <load-on-startup>1</load-on-startup>
27     </servlet>
28 
29     <servlet-mapping>
30         <servlet-name>flex</servlet-name>
31         <url-pattern>/messagebroker/*</url-pattern>
32      </servlet-mapping>
33 
34     <welcome-file-list>
35         <welcome-file>index.html</welcome-file>
36         <welcome-file>index.jsp</welcome-file>
37     </welcome-file-list>
38 
39 
40 </web-app>

 然后修改/WEB-INF/flex-servlet.xml这个配置文件,为了使用 Spring BlazeDS Integration 的 tag,增加命名空间flex。我还是文件内容全部贴上,说明写到注释里面吧。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:flex="http://www.springframework.org/schema/flex"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9  http://www.springframework.org/schema/flex 10  http://www.springframework.org/schema/flex/spring-flex-1.5.xsd 11         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
12     
13     <context:component-scan base-package="com.bing">
14     </context:component-scan>
15     <!-- 请求路由给 MessageBroker -->
16      <flex:message-broker>
17         <flex:message-service default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf,my-amf" />
18     </flex:message-broker> 
19     <!--     <flex:message-broker />  -->
20     <!-- 定义 Bean,并用 remoting-destination tag 把它暴露给 Flex,因为我用了annotation的方式,所以下面的注解掉了 -->
21     <!-- <bean id="myService" class="com.bing.service.MyServiceImpl" />  
22     <flex:remoting-destination ref="myService" />   -->
23 </beans>

然后修改/WEB-INF/flex/services-config.xml这个配置文件,默认的话这个文件夹是有四个文件的(services-config.xml、remoting-config.xml、proxy-config.xml、messaging-config.xml),集成spring后,其他三个都没有什么用了,我删掉了。

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <services-config>
  3 
  4   <!--   <services>
  5         <service-include file-path="remoting-config.xml" />
  6         <service-include file-path="proxy-config.xml" />
  7         <service-include file-path="messaging-config.xml" />        
  8     </services> -->
  9      <services>
 10         <default-channels>
 11            <channel ref="my-amf"/>
 12         </default-channels>
 13     </services>
 14     <security>
 15         <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
 16         <!-- Uncomment the correct app server
 17         <login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
 18         <login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>        
 19         <login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
 20         <login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
 21         -->
 22 
 23         <!-- 
 24         <security-constraint id="basic-read-access">
 25             <auth-method>Basic</auth-method>
 26             <roles>
 27                 <role>guests</role>
 28                 <role>accountants</role>
 29                 <role>employees</role>
 30                 <role>managers</role>
 31             </roles>
 32         </security-constraint>
 33          -->
 34     </security>
 35 
 36     <channels>
 37 
 38         <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
 39             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
 40         </channel-definition>
 41 
 42         <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
 43             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
 44             <properties>
 45                 <add-no-cache-headers>false</add-no-cache-headers>
 46             </properties>
 47         </channel-definition>
 48 
 49         <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
 50             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
 51             <properties>
 52                 <polling-enabled>true</polling-enabled>
 53                 <polling-interval-seconds>4</polling-interval-seconds>
 54             </properties>
 55         </channel-definition>
 56         <channel-definition id="my-longpolling-amf" class="mx.messaging.channels.AMFChannel">
 57             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amflongpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
 58             <properties>
 59                 <polling-enabled>true</polling-enabled>
 60                 <polling-interval-seconds>5</polling-interval-seconds>
 61                 <wait-interval-millis>60000</wait-interval-millis>
 62                 <client-wait-interval-millis>1</client-wait-interval-millis>
 63                 <max-waiting-poll-requests>200</max-waiting-poll-requests>
 64             </properties>
 65         </channel-definition>   
 66         <channel-definition id="my-streaming-amf"  
 67             class="mx.messaging.channels.StreamingAMFChannel">  
 68             <endpoint  
 69                 url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf"  
 70                 class="flex.messaging.endpoints.StreamingAMFEndpoint" />  
 71             <properties>  
 72                 <idle-timeout-minutes>0</idle-timeout-minutes>  
 73                 <max-streaming-clients>100</max-streaming-clients>  
 74                 <server-to-client-heartbeat-millis>1000  
 75                 </server-to-client-heartbeat-millis>  
 76                 <user-agent-settings>  
 77                     <!--  
 78                         MSIE 5, 6, 7 default max number of permanent HTTP connections is  
 79                         2.  
 80                     -->  
 81                     <user-agent match-on="MSIE" kickstart-bytes="2048"  
 82                         max-streaming-connections-per-session="1" />  
 83                     <!-- MSIE 8 max number is 6. -->  
 84                     <user-agent match-on="MSIE 8" kickstart-bytes="2048"  
 85                         max-streaming-connections-per-session="5" />  
 86                     <!-- Firefox 1, 2 max number is 2. -->  
 87                     <user-agent match-on="Firefox" kickstart-bytes="2048"  
 88                         max-streaming-connections-per-session="1" />  
 89                     <!-- Firefox 3 max number is 6. -->  
 90                     <user-agent match-on="Firefox/3" kickstart-bytes="2048"  
 91                         max-streaming-connections-per-session="5" />  
 92                     <!-- Safari 3, 4 max number is 4. -->  
 93                     <user-agent match-on="Safari" kickstart-bytes="2048"  
 94                         max-streaming-connections-per-session="3" />  
 95                     <!-- Chrome 0, 1, 2 max number is 6. -->  
 96                     <user-agent match-on="Chrome" kickstart-bytes="2048"  
 97                         max-streaming-connections-per-session="5" />  
 98                     <!-- Opera 7, 9 max number is 4.-->  
 99                     <user-agent match-on="Opera" kickstart-bytes="2048"  
100                         max-streaming-connections-per-session="3" />  
101                     <!-- Opera 8 max number is 8. -->  
102                     <user-agent match-on="Opera 8" kickstart-bytes="2048"  
103                         max-streaming-connections-per-session="7" />  
104                     <!-- Opera 10 max number is 8. -->  
105                     <user-agent match-on="Opera 10" kickstart-bytes="2048"  
106                         max-streaming-connections-per-session="7" />  
107                 </user-agent-settings>  
108             </properties>  
109         </channel-definition> 
110         <!--
111         <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
112             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
113         </channel-definition>
114 
115         <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
116             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
117             <properties>
118                 <add-no-cache-headers>false</add-no-cache-headers>
119             </properties>
120         </channel-definition>
121         -->
122     </channels>
123 
124     <logging>
125         <target class="flex.messaging.log.ConsoleTarget" level="Error">
126             <properties>
127                 <prefix>[BlazeDS] </prefix>
128                 <includeDate>false</includeDate>
129                 <includeTime>false</includeTime>
130                 <includeLevel>false</includeLevel>
131                 <includeCategory>false</includeCategory>
132             </properties>
133             <filters>
134                 <pattern>Endpoint.*</pattern>
135                 <pattern>Service.*</pattern>
136                 <pattern>Configuration</pattern>
137             </filters>
138         </target>
139     </logging>
140 
141     <system>
142         <redeploy>
143             <enabled>false</enabled>
144             <!-- 
145             <watch-interval>20</watch-interval>
146             <watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
147             <watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
148             <watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
149             <watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
150             <watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
151             <touch-file>{context.root}/WEB-INF/web.xml</touch-file>
152              -->
153         </redeploy>
154     </system>
155 
156 </services-config>
View Code

基本的配置差不多都好了,当然你得加入jar包。下面就开始编写代码部分。代码是网上拷贝的,自己懒得写了。

创建实体类,模拟用,其实你可以这返回一句话。

 1 package com.bing.vo;
 2 
 3 public class MyEntity {
 4     private String frstName;
 5     private String lastName;
 6     private String emailAddress;
 7 
 8     public String getFirstName() {
 9         return frstName;
10     }
11 
12     public void setFirstName(String frstName) {
13         this.frstName = frstName;
14     }
15 
16     public String getLastName() {
17         return lastName;
18     }
19 
20     public void setLastName(String lastName) {
21         this.lastName = lastName;
22     }
23 
24     public String getEmailAddress() {
25         return emailAddress;
26     }
27 
28     public void setEmailAddress(String emailAddress) {
29         this.emailAddress = emailAddress;
30     }
31 }

创建service接口类及其实现类。

package com.bing.service;

public interface MyService {
    String getMyEntities();   
}
 1 package com.bing.service;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.springframework.flex.remoting.RemotingDestination;
 7 import org.springframework.flex.remoting.RemotingInclude;
 8 import org.springframework.stereotype.Component;
 9 
10 import com.bing.vo.MyEntity;
11 import com.thoughtworks.xstream.XStream;
12 @Component("myService")
13 @RemotingDestination(channels = { "my-amf" })
14 public class MyServiceImpl implements MyService {
15 
16     @Override
17     @RemotingInclude
18     public String getMyEntities() {
19         List<MyEntity> list = new ArrayList<MyEntity>();
20         MyEntity entity = new MyEntity();
21         entity.setFirstName("Hello");
22         entity.setLastName("World");
23         entity.setEmailAddress("[email protected]");
24         list.add(entity);
25         MyEntity entity2 = new MyEntity();
26         entity2.setFirstName("你好");
27         entity2.setLastName("jone");
28         entity2.setEmailAddress("[email protected]");
29         list.add(entity2);
30         XStream x=new XStream();
31         x.alias("vo", MyEntity.class);
32         System.out.println(x.toXML(list));
33         return x.toXML(list);
34     }
35 
36 }

后台代码基本搞定,就是返回一个xml的对象字符串。中间用了一下Xstream这个jar工具。发布一下项目到tomcat下,然后启动看看……

前台就简单了,打开flash builder,创建一个flash项目,和上一篇例子基本一样。不过这里我把输出的路径改到tomcat下了。

blazeDS集成spring的remote访问_第1张图片

然后编写成徐代码:进行测试

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark" 
 4                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
 5     <fx:Script>
 6         <![CDATA[
 7             import mx.controls.Alert;
 8             import mx.messaging.ChannelSet;
 9             import mx.messaging.channels.StreamingAMFChannel;
10             import mx.messaging.messages.IMessage;
11             import mx.rpc.events.FaultEvent;
12             import mx.rpc.events.ResultEvent;  
13             
14             
15             private function dotest():void {
16                 //var myStreamingAMF:StreamingAMFChannel = new StreamingAMFChannel("my-amf", "/blazedsfirst/messagebroker/amf");
17                 
18             /*    var myStreamingAMF:StreamingAMFChannel=new StreamingAMFChannel(); 
19                 var channelSet:ChannelSet=new ChannelSet(); 
20                 channelSet.addChannel(myStreamingAMF);
21                 remoteObject.channelSet=channelSet;*/
22                 remoteObject.getMyEntities();
23             }
24             // Handle the recevied message.
25             private function resultHandler(event:ResultEvent):void {
26                 Alert.show(event.result.toString());
27             }
28             
29             // Handle a message fault.
30             private function faultHandler(event:FaultEvent):void {
31                 Alert.show("Received fault: " + event.fault);
32             }
33             
34         ]]>
35     </fx:Script>
36     <fx:Declarations>
37         <s:RemoteObject id="remoteObject" 
38                         destination="myService" 
39                         result="resultHandler(event);"
40                         fault="faultHandler(event);"/>
41         
42         
43     </fx:Declarations>
44     
45     <s:layout>
46         <s:VerticalLayout gap="0" horizontalAlign="center"  paddingTop="50"/>
47     </s:layout>
48     <s:Label text="测试spring集成remote"/>
49     <s:Button label="Send" click="dotest();"/>
50     
51      
52 </s:Application>

直接运行进行测试,弹出结果如下:

<list>
  <vo>
    <frstName>Hello</frstName>
    <lastName>World</lastName>
    <emailAddress>[email protected]</emailAddress>
  </vo>
  <vo>
    <frstName>你好</frstName>
    <lastName>Space</lastName>
    <emailAddress>[email protected]</emailAddress>
  </vo>
 </list>

下一篇我将介绍一下,push推送技术,如何利用spring和blazeds实现。

 

 

你可能感兴趣的:(blazeds)