blazeds使用remote访问

 欢迎交流转载,请注明出处: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,我把目录的截图展示一下:

blazeds使用remote访问_第1张图片

其中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
  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 
 10     <security>
 11         <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
 12         <!-- Uncomment the correct app server
 13         <login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
 14         <login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>        
 15         <login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
 16         <login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
 17         -->
 18 
 19         <!-- 
 20         <security-constraint id="basic-read-access">
 21             <auth-method>Basic</auth-method>
 22             <roles>
 23                 <role>guests</role>
 24                 <role>accountants</role>
 25                 <role>employees</role>
 26                 <role>managers</role>
 27             </roles>
 28         </security-constraint>
 29          -->
 30     </security>
 31     <!--Channel的作用就是帮助我们传输消息.Channel使用URL与Endpoint通信.Endpoint就是消息传输的接收器,它并不真正处理消息,它将处理过程委托给service  -->
 32     <channels>
 33 
 34         <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
 35             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
 36         </channel-definition>
 37 
 38         <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
 39             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
 40             <properties>
 41                 <add-no-cache-headers>false</add-no-cache-headers>
 42             </properties>
 43         </channel-definition>
 44 
 45         <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
 46             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
 47             <properties>
 48                 <polling-enabled>true</polling-enabled>
 49                 <polling-interval-seconds>4</polling-interval-seconds>
 50             </properties>
 51         </channel-definition>
 52 
 53         <!--
 54         <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
 55             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
 56         </channel-definition>
 57 
 58         <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
 59             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
 60             <properties>
 61                 <add-no-cache-headers>false</add-no-cache-headers>
 62             </properties>
 63         </channel-definition>
 64         -->
 65     </channels>
 66 
 67     <logging>
 68     <!-- 输出到控制台 ,若要使用Servlet日志文件作为目标,将class属性改为"flex.messaging.log.ServletLogTarget".-->
 69       <!-- 中level="Error"为日志的记录级别.有None,Error,Warn,Info,Debug,All六种选择 -->
 70         <target class="flex.messaging.log.ConsoleTarget" level="Error">
 71             <properties>
 72                 <prefix>[BlazeDS] </prefix>
 73                 <includeDate>false</includeDate>
 74                 <includeTime>false</includeTime>
 75                 <includeLevel>false</includeLevel>
 76                 <includeCategory>false</includeCategory>
 77             </properties>
 78             <!-- 信息的过滤条件,只有匹配的类别才会被记录到指定的目标中 -->
 79             <filters>
 80                 <pattern>Endpoint.*</pattern>
 81                 <pattern>Service.*</pattern>
 82                 <pattern>Configuration</pattern>
 83             </filters>
 84         </target>
 85     </logging>
 86 
 87     <system>
 88         <redeploy>
 89             <enabled>false</enabled>
 90             <!-- 
 91             <watch-interval>20</watch-interval>
 92             <watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
 93             <watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
 94             <watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
 95             <watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
 96             <watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
 97             <touch-file>{context.root}/WEB-INF/web.xml</touch-file>
 98              -->
 99         </redeploy>
100     </system>
101 
102 </services-config>
View Code

配置文件就简单说明到这,有兴趣自己googling。。。。。

2.创建简单remote例子。

这里面我就用flexbuilder4.6和myeclipse两个分开来创建项目。我觉得这样舒服,你要是不爽,你也可以装插件,都在eclipse中编译。

  •  后代java代码
1 package com.bing.test;
2 
3 public class HelloWorld {
4 
5     public String getHelloWorld(String name){
6         System.out.println("hello "+name);
7         return "this is a test for " +name;
8     }
9 }
  • 远程服务配置文件 
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <service id="remoting-service" class="flex.messaging.services.RemotingService">
 3 
 4     <adapters>
 5         <adapter-definition id="java-object"
 6             class="flex.messaging.services.remoting.adapters.JavaAdapter"
 7             default="true" />
 8     </adapters>
 9 
10     <default-channels>
11         <channel ref="my-amf" />
12     </default-channels>
13     <!-- destination设置服务终端的目的地,属性id为客户端组件RemoteObject的destination -->
14     <destination id="helloWorld" channels="my-amf">
15         <properties>
16             <source>com.bing.test.HelloWorld</source>
17         </properties>
18     </destination>
19 </service>

 

  • 前台flexbuilder中配置及代码

 新建flex项目dstest,在服务器设置这一步,选择如下,当然你也可以创建好项目后在设置。

blazeds使用remote访问_第2张图片

 创建好之后,编写flex代码

 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.rpc.events.FaultEvent;
 8             import mx.rpc.events.ResultEvent;
 9             
10             // Send the message in response to a Button click.
11             private function dotest():void {
12                 var text:String = ti.text;
13                 remoteObject.getHelloWorld(text);
14             }
15             
16             // Handle the recevied message.
17             private function resultHandler(event:ResultEvent):void {
18                 ta.text += "服务器返回: "+ event.result + "\n";
19             }
20             
21             // Handle a message fault.
22             private function faultHandler(event:FaultEvent):void {
23                 ta.text += "Received fault: " + event.fault + "\n";
24             }
25         ]]>
26     </fx:Script>
27     <fx:Declarations>
28         <s:RemoteObject id="remoteObject" 
29                         destination="helloWorld"
30                         result="resultHandler(event);"
31                         fault="faultHandler(event);"/>
32     </fx:Declarations>
33     
34     <s:layout>
35         <s:VerticalLayout gap="10" />
36     </s:layout>
37     <s:Label text="输入名字"/>
38     <s:TextInput id="ti" text="Hello World!"/>
39     <s:Button label="Send" click="dotest();"/>
40     <s:TextArea id="ta" width="100%" height="100%"/>
41 </s:Application>

编译之后,在myeclipse的项目目录下会出现一个debug目录,如下:

blazeds使用remote访问_第3张图片

 

 修改index.jsp页面,加入超链接

<html>
<body>
<h2>Hello World!</h2>
<a href="dstest-debug/dstest.html">ddd</a>
</body>
</html>

好了,这样就可以启动访问了。

 

你可能感兴趣的:(blazeds)