http://www.cnblogs.com/noam/archive/2010/08/05/1793504.html

blazeds, spring3整合实现RPC服务和消息服务

环境:

  MyEclipse 7.5

  Flash Builder 4 plugin for eclipse

  Tomcat 6

  BlazeDS 4.0.0

  springframework 3.0.3

  Spring-flex整合 org.springframework.flex-1.0.3.RELEASE.jar (适用于spring2.5.6+,blazeds3.2+)

步骤:

  1. 创建带有Flex支持的Web工程,见通过J2EE Web工程添加Flex项目,进行BlazeDS开发

  2. 导入spring包和spring-flex集成包。

  3. 配置web.xml,配置DispatcherServlet,使用spring进行管理,并将请求映射到MessageBroker。

  4. 创建web-application-config.xml,配置flex服务,主要是在spring中配置MessageBroker。

  5. 创建remoting-destination和 message-destination,修改channel属性。

  6. 创建mxml文件,定义channelSet,注册remote-object和定义Productor, Consumer.

注意:

  当不使用spring直接配置blazeDS实现RPC和消息服务时,仅需要在remoting-config.xml和messaging-config.xml中配置destination,只要在这些文件中配置了默认通道,mxml文件中不需要再定义channelSet即可执行。而使用spirng时,仅当将default-channels定义在services-config.xml的services标签中才有效,若在services-config.xml的services标签中加载其他配置文件,在这些文件中配置各自的default-channels,会报如下错误:[MessagingError message='×××'artgallerydataservice' either does not exist or the destination has no channels defined (and the application does not define any default channels.)'],原因是没有找到通道。如果仅将通道定义在<flex:message-destination>或<flex:remoting-destination>中,也会出现同样的情况。

  对于以上的两个问题,我觉得几乎不可理解,解决方案是在mxml文件中定义channelSet以找到amf通道。

项目代码:

  目录结构:

   

  Web.xml:

  1.  
  2. web.xml
  3.  
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <web-app version="2.4" 
  6.     xmlns="http://java.sun.com/xml/ns/j2ee" 
  7.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  8.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  9.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  10.     
  11.     <servlet>
  12.       <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  13.       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  14.       <init-param>
  15.         <param-name>contextConfigLocation</param-name>
  16.         <param-value>/WEB-INF/web-application-config.xml</param-value>
  17.       </init-param>
  18.       <load-on-startup>1</load-on-startup>
  19.     </servlet>
  20.     
  21.     <servlet-mapping>
  22.       <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  23.       <url-pattern>/messagebroker/*</url-pattern>
  24.     </servlet-mapping>
  25.     
  26.   <welcome-file-list>
  27.     <welcome-file>index.jsp</welcome-file>
  28.   </welcome-file-list>
  29. </web-app>

  web-application-config.xml:

    <flex:message-broker/>是非常重要的,它还有其它的几种表示方法,但以这种最为简单,它通过MessageBrokerHandlerAdapter和HandlerMapping将请求发送给spring管理的MessageBroker。

    注意这里一定要导入spring-flex集成的schemaLocation,remoting-destination和 message-destination也在这里定义。

web-application-config.xml

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:flex="http://www.springframework.org/schema/flex"
  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-2.5.xsd
  9.         http://www.springframework.org/schema/flex
  10.         http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
  11.  
  12.     <flex:message-broker/>
  13.  
  14.     <flex:message-destination id="msg_dest"/>
  15.     <bean id="flex_test" class="test.Test">
  16.         <flex:remoting-destination/>
  17.     </bean>
  18. </beans>

  services-config.xml:

    这里最重要的是channels的定义和配置。

services-config.xml

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <services-config>
  4.  
  5.     <services>
  6.       <default-channels>
  7.         <channel ref="my-amf"/>
  8.       </default-channels>
  9.     </services>
  10.  
  11.     <security>
  12.         <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
  13.         <!-- Uncomment the correct app server
  14.         <login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
  15.         <login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>        
  16.         <login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
  17.         <login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
  18.         -->
  19.  
  20.         <!-- 
  21.         <security-constraint id="basic-read-access">
  22.             <auth-method>Basic</auth-method>
  23.             <roles>
  24.                 <role>guests</role>
  25.                 <role>accountants</role>
  26.                 <role>employees</role>
  27.                 <role>managers</role>
  28.             </roles>
  29.         </security-constraint>
  30.          -->
  31.     </security>
  32.  
  33.     <channels>
  34.  
  35.         <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
  36.             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
  37.         </channel-definition>
  38.  
  39.         <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
  40.             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
  41.             <properties>
  42.                 <add-no-cache-headers>false</add-no-cache-headers>
  43.             </properties>
  44.         </channel-definition>
  45.  
  46.         <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
  47.             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
  48.             <properties>
  49.                 <polling-enabled>true</polling-enabled>
  50.                 <polling-interval-seconds>1</polling-interval-seconds>
  51.             </properties>
  52.         </channel-definition>
  53.  
  54.         <!--
  55.         <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
  56.             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
  57.         </channel-definition>
  58.  
  59.         <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
  60.             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
  61.             <properties>
  62.                 <add-no-cache-headers>false</add-no-cache-headers>
  63.             </properties>
  64.         </channel-definition>
  65.         -->
  66.     </channels>
  67.  
  68.     <logging>
  69.         <target class="flex.messaging.log.ConsoleTarget" level="Error">
  70.             <properties>
  71.                 <prefix>[BlazeDS] </prefix>
  72.                 <includeDate>false</includeDate>
  73.                 <includeTime>false</includeTime>
  74.                 <includeLevel>false</includeLevel>
  75.                 <includeCategory>false</includeCategory>
  76.             </properties>
  77.             <filters>
  78.                 <pattern>Endpoint.*</pattern>
  79.                 <pattern>Service.*</pattern>
  80.                 <pattern>Configuration</pattern>
  81.             </filters>
  82.         </target>
  83.     </logging>
  84.  
  85.     <system>
  86.         <redeploy>
  87.             <enabled>false</enabled>
  88.             <!-- 
  89.             <watch-interval>20</watch-interval>
  90.             <watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
  91.             <watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
  92.             <watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
  93.             <watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
  94.             <watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
  95.             <touch-file>{context.root}/WEB-INF/web.xml</touch-file>
  96.              -->
  97.         </redeploy>
  98.     </system>
  99.  
  100. </services-config>

  messaging-config.xml, proxy-config.xml, remoting-config.xml:

    略。和blazeDS提供的几乎一样。

  test.Test.java:

    这里只定义了一个方法,用以测试远程方法调用。

  1.  
  2. package test;
  3.  
  4. public class Test {
  5.  
  6.     public void done(){
  7.         System.out.println("OK.");
  8.     }
  9. }

  flex_test.mxml:

    这个不做过多解释。 这里定义了AMFChannel和ChannelSet,避免找不到通道的问题。Productor和Consumer是实现BlazeDS消息服务的两个组件,AsyncMessage是用来发送消息的。

  1.  
  2. flex_test.mxml
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
  6.                xmlns:s="library://ns.adobe.com/flex/spark" 
  7.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
  8.                creationComplete="consumer.subscribe();">
  9.     
  10.     <fx:Script>
  11.         <![CDATA[
  12.             import mx.controls.Alert;
  13.             import mx.messaging.messages.*;
  14.             import mx.messaging.events.*;
  15.             import mx.rpc.events.FaultEvent;
  16.             import mx.rpc.events.ResultEvent;
  17.             
  18.             // Write received message to TextArea control.
  19.             private function messageHandler(event: MessageEvent):void {
  20.             ta.text += event.message.body + "\n";
  21.             }
  22.             
  23.             // Compose the message as an instance of AsyncMessage, 
  24.             // then use the Producer.send() method to send it.
  25.             private function sendMessage():void {
  26.                 var message: AsyncMessage = new AsyncMessage();
  27.                 message.body = userName.text + ": " + msg.text;
  28.                 producer.send(message);
  29.                 msg.text = "";
  30.             }
  31.             
  32.             protected function button_clickHandler(event:MouseEvent):void
  33.             {
  34.                 // TODO Auto-generated method stub
  35.                 object.done();
  36.             }
  37.             
  38.             protected function resulth(event:ResultEvent):void
  39.             {
  40.                 label.text="succeed!";
  41.             }
  42.             
  43.             protected function faulth(event:FaultEvent):void
  44.             {
  45.                 label.text="failed!";
  46.                 Alert.show("远程对象调用失败:\n"+event.fault);
  47.             }
  48.             
  49.         ]]>
  50.     </fx:Script>
  51.     
  52.     <fx:Declarations>
  53.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  54.         <mx:AMFChannel id="polling_amf" url="../messagebroker/amfpolling" />
  55.         <mx:ChannelSet id="polling_channel" channels="{[polling_amf]}" />
  56.         <mx:AMFChannel id="amf" url="../messagebroker/amf" />
  57.         <mx:ChannelSet id="amf_channel" channels="{[amf]}" />
  58.         
  59.         <mx:Producer id="producer" destination="msg_dest" channelSet="{polling_channel}"/>
  60.         <mx:Consumer id="consumer" destination="msg_dest" channelSet="{polling_channel}"
  61.                      message="messageHandler(event)"/>
  62.         
  63.         <mx:RemoteObject id="object" destination="flex_test" 
  64.                          channelSet="{amf_channel}" 
  65.                          result="resulth(event);" fault="faulth(event);" />
  66.         
  67.     </fx:Declarations>
  68.     <s:TextArea x="42" y="56" width="289" height="163" id="ta"/>
  69.     <s:Label x="42" y="244" text="User Name"/>
  70.     <s:Label x="43" y="272" text="Message"/>
  71.     <s:TextInput x="120" y="237" width="128" id="userName"/>
  72.     <s:TextInput x="120" y="267" id="msg"/>
  73.     <s:Button x="261" y="237" label="Send" height="52" click="sendMessage();"/>
  74.     <s:Label x="42" y="26" text="Message Content"/>
  75.     <mx:VRule x="411" y="26" height="263"/>
  76.     <s:Button x="468" y="54" label="RPC Service Test" width="148" height="39" id="button" click="button_clickHandler(event)"/>
  77.     <s:Label y="140" text="Guess the Result!" verticalAlign="middle" textAlign="center" width="177" height="79" x="456" id="label"/>
  78. </s:Application>

  执行结果:

  

flex+java+blazeds 多通道好文的更多相关文章

  1. Flex+Java+Blazeds

    1.环境:jdk1.6,Flex4.6 2.工具:MyEclipse10 3.server:Tomcat7 4.连接方式:Blazeds 5.项目类型:Flex项目 6.步骤 (1)新建Flex项目一 ...

  2. Flex使用Blazeds与Java交互及自定义对象转换详解-DATAGRID读取ORACLE数据

    http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html 一.建立Flex与Java交互的工程. 本文中讲到的交互是利用Blazeds的,因为这 ...

  3. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  4. Java 后台创建word 文档

    ---恢复内容开始--- Java 后台创建 word 文档 自己总结  网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...

  5. Java解析word,获取文档中图片位置

    前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...

  6. 《Java开发学习大纲文档》V7.0

    <Java开发学习大纲文档>V7.0简介: 本文档是根据企业开发所需要掌握的知识点大纲进行总结汇编,是Java开发工程师必备知识体系,系统化学习针对性非常强,逻辑分析能力非常清晰;技术方面 ...

  7. 《Java开发学习大纲文档》V6.0(已经不公布了,请查看第七版)

    <Java开发大纲学习文档第六版>简介: 有需要的私聊作者QQ:253173641.

  8. 001-Java®语言规范、Java平台标准版文档、JVM概述

    一.概述 相关api地址:JDK10   JDK 9   JDK 8   JDK 7   JDK 6 Java语言和虚拟机规范: https://docs.oracle.com/javase/spec ...

  9. java实现简单回文算法

    算法要求 编写一个程序,判断一个字符串是否为"回文".回文串:字符串字符从前往后与从后往前一致(中心对称). 算法思路 首先将字符串等分左右两块,然后依次对称比较每一对字符是否相同 ...

随机推荐

  1. ivy 配置 maven代理

    Ivy 是一个依赖管理工具,直观感受是其跟maven 的作用差不多:但这两个其实是不同的工具: maven 是面向整个项目的工程管理及构建工具:ivy 仅作为依赖管理工具,与ant 高度集成. 需要了 ...

  2. C#调试心经续(转)

    断点篇 命中次数(Hit Counts) 右击断点,可以设置Hit Counts(命中次数),会弹出如下的对话框 当条件满足的时候断点会被命中(即即将被执行),这个命中次数是断点被命中的次数.默认是始 ...

  3. 线程让出实验【RT-Thread学习笔记 4】

    API: rt_thread_yield 线程函数中调用,本线程释放MCU.如果此时有别的相同优先级的任务整处于等待状态,将获得MCU使用权. 线程让出就是给OS增加一个任务调度的机会. 创建两个线程 ...

  4. Easyui 小脚本

    function addTab(subtitle, url, icon) { if (!$('#tabs').tabs('exists', subtitle)) { $('#tabs').tabs(' ...

  5. OAF_开发系列23_实现OAF数据格式CSS和CSS库(案例)

    20150716 Created By BaoXinjian

  6. 没想到cnblog也有月经贴,其实C#值不值钱不重要。

    呵呵,就不倚老卖老了,从basic走过来,一路经历vb,vf,delphi,C#,php,asp,html,js,css,太多太多的开发语言,包括面向对象编程思想,语义化页面结构等等,除了高级的编程技 ...

  7. 如何解決 Homebrew Update 失敗?

    相信許多用 MAC 系統的程式設計師.工程師們都有用 Homebrew 這個超好用的 Open Source 套件管理程式吧? 如果沒有的話,你可以透過以下的指令安裝: ruby -e "$ ...

  8. ASP.NET 服务器控件的生命周期

    服务器控件生命周期简介 服务器控件的生命周期是创建服务器控件最重要的概念.作为开发人员,必须对服务器控件生命周期深刻理解.当然,这不是一朝一夕就可以做到的.对于学习控件开发技术的初学者,可以不必掌握得 ...

  9. Metro Win8风格的按钮(Filp翻转)

    原地址->http://www.cnblogs.com/yk250/p/5661093.html 介绍:简约而不简单....颜色可随意调制,最好用Blend工具. 效果图如下:话说这个图会不会太 ...

  10. 关于ckeditor ajax提交到后台 问题

    ckeditor 提交时 如果有带有html时是提交不了的 解决办法就是 你在提交的时候 将ckeditor获取的只编码(encodeURI) 然后在传到后台提交的时候 在解码 就ok了 ckname ...