关于服务端的发布很简单,按照官网上介绍的,将blazeds.war下下来后,按照示例建一个blazeds的web project
下面是我project目录的截图
其中可能需要改动的就是remoting-config.xml和web.xml两个文件,其他配置文件默认不需要改动
我的remoting-config.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="helloWorld">
<properties>
<source>svr.HelloWorld</source>
</properties>
</destination>
<destination id="bdserver">
<properties>
<source>svr.BDServer</source>
</properties>
</destination>
</service>
我的web.xml内容如下:
<?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</display-name>
<description>BlazeDS Application</description>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>
flex.messaging.MessageBrokerServlet
</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>servlets.InitServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<!-- RDSDispatchServlet相关配置 默认是注释掉的,如果需要Flash Builder通过data/service服务能连接的话,这边需要去掉注释 -->
<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>false</param-value><!-- 此处默认值是true,通过Flash Builder来生成相关服务时,需改为false,即不同相关用户名、密码来验证 -->
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id="RDS_DISPATCH_MAPPING">
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>
<!-- 以上是RDSDispatchServlet相关配置 -->
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!-- 1111 for WebSphere deployment, please uncomment -->
<!--
<resource-ref>
<description>Flex Messaging WorkManager</description>
<res-ref-name>wm/MessagingWorkManager</res-ref-name>
<res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
-->
</web-app>
至此发布,服务端就算ok了,
下面说下客户端如何访问,我用了两种方式访问,一个是flex的remoteobject 组件,另一种是通过blazeds的服务来生成相关代理文件,然后访问,在客户端调用服务前需要在服务端目录上放置crossdomain.xml文件,不然会报错,我用的是tomcat,放在了tomcat的webapps\root目录下,crossdomain.xml内容为:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all" />
<allow-access-from domain="*" />
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
remoteobject:
主要代码如下:
<s:ChannelSet id="channelSet">
<s:AMFChannel id="amfchannel" url="http://192.168.6.19:8008/blazeds/messagebroker/amf"/>
</s:ChannelSet>
<s:RemoteObject id="ro" destination="bdserver"
channelSet="{channelSet}" result="onResult(event)" fault="onFault(event)">
</s:RemoteObject>
这样通过ro这个对象就可以直接调用其发布的方法了
连接blazeds生成相关代理文件:
此处需要注意的是如果你的客户端的项目的context root和服务端的webcontext不一致的话就不能生成,会报错,拿我的这个做个列子,我服务端的webcontext是blazeds,而现在客户端的项目webcontext是JNBus,这样会报错,如果你客户端的目录名和服务器的一致就没问题了,但如果不一致的话咋办呢?其实可以临时调整下这个flex project 的context root,改完生成服务代理之后再调整过来,就ok了,改的地方如下图, root url和context root的JNBus换成blazeds
生成之后会自动生成如下的东西:
这样代理文件就算生成好了,下面就看怎么用了,下面是主要代码:
<fx:Declarations>
<s:ChannelSet id="channelSet">
<s:AMFChannel id="amfchannel" url="http://192.168.6.19:8008/blazeds/messagebroker/amf"/>
</s:ChannelSet>
<s:CallResponder id="getCarCountResult" result="getCarCountResult_resultHandler(event)" />
</fx:Declarations>
private function init(): void {
this.bdServer = new Bdserver();
this.bdServer.channelSet = this.channelSet;
this.getCarCountResult.token = this.bdServer.getCarCount();
}
这样总算可以了