欢迎交流转载,请注明出处:http://www.cnblogs.com/shizhongtao/p/3487128.html
1.配置及说明
- jar包说明
从官方上下的Blazeds中,默认的配置有四个文件,都在WEB-INF/Flex目录下:services-config.xml、remoting-config.xml、proxy-config.xml、messaging-config.xml。要使用他,首先把lib目录下的jar包拷贝到你自己创建的web项目中去,当然你可以用maven来管理jar包,这样就会方便很多。这里作为demo,我把目录的截图展示一下:
其中xalan.jar在本例中并没有用到,它是处理xml文档的一个jar包。
- web.xml配置文件
在web.xml中加入监听(就是个设计模式中的观察者模式),来将HttpFlexSession类作为监听器注册到web.xml中,这样当J2EE HttpSession属性和代理属性的变化时候,就会通知FlexSession属性和目前绑定的listener做相应处理。
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
加入blazeds提供的servlet,这样当URL请求符合/messagebroker/*(这个和后面展示的channel中的那个路径对应)模式时候,它会在Servlet容器启动时启动,并在启动时读取配置文件/WEB-INF/flex/services-config.xml.
<!-- 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-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
- 服务配置文件flex/services-config.xml
<?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> <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>
<!--Channel的作用就是帮助我们传输消息.Channel使用URL与Endpoint通信.Endpoint就是消息传输的接收器,它并不真正处理消息,它将处理过程委托给service -->
<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-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>
<!-- 输出到控制台 ,若要使用Servlet日志文件作为目标,将class属性改为"flex.messaging.log.ServletLogTarget".-->
<!-- 中level="Error"为日志的记录级别.有None,Error,Warn,Info,Debug,All六种选择 -->
<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>
配置文件就简单说明到这,有兴趣自己googling。。。。。
2.创建简单remote例子。
这里面我就用flexbuilder4.6和myeclipse两个分开来创建项目。我觉得这样舒服,你要是不爽,你也可以装插件,都在eclipse中编译。
- 后代java代码
package com.bing.test; public class HelloWorld { public String getHelloWorld(String name){
System.out.println("hello "+name);
return "this is a test for " +name;
}
}
- 远程服务配置文件
<?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为客户端组件RemoteObject的destination -->
<destination id="helloWorld" channels="my-amf">
<properties>
<source>com.bing.test.HelloWorld</source>
</properties>
</destination>
</service>
- 前台flexbuilder中配置及代码
新建flex项目dstest,在服务器设置这一步,选择如下,当然你也可以创建好项目后在设置。
创建好之后,编写flex代码
<?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.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent; // Send the message in response to a Button click.
private function dotest():void {
var text:String = ti.text;
remoteObject.getHelloWorld(text);
} // Handle the recevied message.
private function resultHandler(event:ResultEvent):void {
ta.text += "服务器返回: "+ event.result + "\n";
} // Handle a message fault.
private function faultHandler(event:FaultEvent):void {
ta.text += "Received fault: " + event.fault + "\n";
}
]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="remoteObject"
destination="helloWorld"
result="resultHandler(event);"
fault="faultHandler(event);"/>
</fx:Declarations> <s:layout>
<s:VerticalLayout gap="10" />
</s:layout>
<s:Label text="输入名字"/>
<s:TextInput id="ti" text="Hello World!"/>
<s:Button label="Send" click="dotest();"/>
<s:TextArea id="ta" width="100%" height="100%"/>
</s:Application>
编译之后,在myeclipse的项目目录下会出现一个debug目录,如下:
修改index.jsp页面,加入超链接
<html>
<body>
<h2>Hello World!</h2>
<a href="dstest-debug/dstest.html">ddd</a>
</body>
</html>
好了,这样就可以启动访问了。