您的位置:首页 > 运维架构

Blazeds体系结构(二)

2011-11-03 18:19 162 查看
服务端FlexClient、MessageClient、FlexSession类的实例代表了Flex应用和服务端的连接。你可以使用这些对象管理FLEX应用程序和服务端的同步。
 
FlexClient, MessageClient, and FlexSession objects
 
FlexClient
 
每一个MXML或者AS的Flex应用都被编译进SWF文件。当SWF文件和Blazeds服务通信的时候,一个flex.message.client.FlexClient对象就被创建,并在服务端代表这个SWF文件。SWF文件和FlexClient实例具有一对一的映射关系。Blazeds服务端为每个FlexClient实例生成一个唯一的标识id。在客户端也有一个单例类mx.message.FlexClient,用于存储这个唯一的FlexClient Id。
 
MessageClient
 
如果某个Flex应用程序含有一个订阅组件(flex.message.Consumer),服务端就会创建一个相应的flex.messaging.MessageClient实例来代表这个订阅者的发布者。每个MessageClient都有一个唯一的clientId,可以自动由Blazeds服务端生成,也可以在调用Consumer.subscribe()方法之前指定这个Consumer.clientId属性。
 
FlexSession
 
FlexSession对象代表在Flex程序和Blazeds服务之间的连接。它的生命周期基于通道和端点使用的底层协议。如果是基于HTTP的通道,例如AMFChannel或者HTTPChannel,在Blazeds服务端就是浏览器范围。如果它连接的端点是一个基于servlet的端点,那么这个HTTP session是基于J2EE HttpSession 对象。
 
三者之间的关系
 
一个FlexObject对象可以拥有多个FlexSession实例,这取决于Flex应用使用的通道数。例如,一个程序使用了一个HTTPChannel,那么在Blazeds服务端一个FlexSession代表这个HTTP session就会被创建。
 
一个FlexSession也可以拥有一个或多个FlexClient和它关联。例如,a SWF file that uses an
HTTPChannel is opened in two tabs,在BlazeDS服务端2个FlexClient实例被创建(一个SWF文件一个),但是只有一个FlexSession,因为两个tab共享同一个HTTP session。
 
 
每个订阅组件都会创建MessageClient。每一个MessageClient都会和一个FlexClient以及一个FlexClient关联。
 
 
关于三者的监听器
 
BlazeDS服务端提供了下列监听器,来让你根据自己的逻辑来创建、销毁、以及改变三者的状态。
 
FlexClientListener
 
FlexClientAttributeListener
 
FlexClientBindingListener
 
FlexSessionListener
 
FlexSessionAttributeListener
 
FlexSessionBindingListener
 
MessageClientListener
 
 
 
数据序列化
 
       AS对象转换为Java对象


 

 

 

 
Java对象转换为AS对象
 
 


 

 


 



 

 
 
 
// Product.as
package samples.externalizable {
 
import flash.utils.IExternalizable;
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
[RemoteClass(alias="samples.externalizable.Product")]
public class Product implements IExternalizable {
 
public function Product(name:String=null) {
this.name = name;
}
 
public var id:int;
public var name:String;
public var properties:Object;
public var price:Number;
 
public function readExternal(input:IDataInput):void {
name = input.readObject() as String;
properties = input.readObject();
price = input.readFloat();
}
 
public function writeExternal(output:IDataOutput):void {
output.writeObject(name);
output.writeObject(properties);
output.writeFloat(price);
}
}
}
 
// Product.java
package samples.externalizable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
 
public class Product implements Externalizable {
 
private String inventoryId;
public String name;
public Map properties;
public float price;
 
public Product(){
}
 
public String getInventoryId() {
return inventoryId;
}
 
public void setInventoryId(String inventoryId) {
if (inventoryId != null && inventoryId.startsWith("X")){
this.inventoryId = inventoryId;
}else{
throw new IllegalArgumentException("3rd party product
inventory identities must start with 'X'");
}
}
 
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
name = (String)in.readObject();
properties = (Map)in.readObject();
price = in.readFloat();
setInventoryId(lookupInventoryId(name, price));
}
 
public void writeExternal(ObjectOutput out) throws IOException {
// Write out the client properties from the server representation
out.writeObject(name);
out.writeObject(properties);
out.writeFloat(price);
}
 
private static String lookupInventoryId(String name, float price) {
String inventoryId = "X" + name + Math.rint(price);
return inventoryId;
}
}
 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息