Flex和BlazeDS用了很久,一直会出现各种稀奇古怪的问题,是时候要把源码看看了,先从BlazeDS开始。第一步从git上下载源码,搭建运行环境。然后写一个简单的测试类:
public class Hello {
public String sayHello(String name) {
System.out.println("hello " + name);
return "hello " + name;
}
}
再将这个类配置起来,确保能被外部访问,有两种配置方式,第一种方式
<destination id="testService" channels="my-amf"> <properties> <source>com.lang.remote.Hello</source> </properties> </destination>
第二种方式:删除线
<bean id="testService" class="com.lang.remote.Hello"> <flex:remoting-destination/> </bean>
目前看来这两种方式是等价的,我本人比较喜欢第二种方式。messagebroker的配置
<flex:message-broker> <flex:mapping pattern="messagebroker/*"/> <flex:message-service default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf" /> </flex:message-broker>
配置完成,启动项目,再写一个测试类
@Test
public void test() {
AMFConnection conn = new AMFConnection();
try {
conn.connect("http://localhost:9900/remote/messagebroker/amf");
//call里面的第一个参数是刚刚配置好的remotedistination的id,"点"后面是方法名,再后面的是参数
conn.call("testService.sayHello", "Lang");
} catch (Exception e) {
fail(e.getMessage());
}
}
测试环境搭建完成,接下来看看请求是怎样转发到messagebroker的