栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过BlazeDS从Java到Flex的自定义编组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过BlazeDS从Java到Flex的自定义编组

好的-
我自己找到了答案。这涉及编写我自己的AMF端点类和相关的序列化类。我要说的是,http://flexblog.faratasystems.com上的家伙一直是黑客BlazeDS灵感的重要来源。

该代码实际上应该合并到BlazeDS本身或某个开放源代码扩展项目中-它是如此基础。

频道定义

    <channel-definition id="my-amf" >        <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" />         <properties> <serialization>     <type-marshaller>ch.hedgesphere.core.blazeds.translator.HedgesphereASTranslator</type-marshaller> </serialization>        </properties>    </channel-definition>

自定义AMF端点

package ch.hedgesphere.core.blazeds.endpoint;import ch.hedgesphere.core.blazeds.serialization.Serializer;    public class AMFEndpoint extends flex.messaging.endpoints.AMFEndpoint {    @Override    protected String getSerializerClassName() {        return Serializer.class.getName();        }    }

自定义序列化器

package ch.hedgesphere.core.blazeds.serialization;import java.io.OutputStream;import flex.messaging.io.MessageIOConstants;import flex.messaging.io.SerializationContext;import flex.messaging.io.amf.AmfMessageSerializer;import flex.messaging.io.amf.AmfTrace;public class Serializer extends AmfMessageSerializer {    @Override    public void initialize(SerializationContext context, OutputStream out, AmfTrace trace)    {        amfOut = new AMF0Output(context);        amfOut.setOutputStream(out);        amfOut.setAvmPlus(version >= MessageIOConstants.AMF3);        debugTrace = trace;        isDebug = trace != null;        amfOut.setDebugTrace(debugTrace);    }}

自定义AMF 0处理

package ch.hedgesphere.core.blazeds.serialization;import flex.messaging.io.SerializationContext;public class AMF0Output extends flex.messaging.io.amf.Amf0Output {public AMF0Output(SerializationContext context) {    super(context);}@Override    protected void createAMF3Output()    {        avmPlusOutput = new AMF3Output(context);        avmPlusOutput.setOutputStream(out);        avmPlusOutput.setDebugTrace(trace);    }}

自定义AMF 3处理

package ch.hedgesphere.core.blazeds.serialization;import java.io.IOException;import org.joda.time.DateTime;import org.joda.time.LocalDate;import org.joda.time.LocalTime;import flex.messaging.io.SerializationContext;public class AMF3Output extends flex.messaging.io.amf.Amf3Output {public AMF3Output(SerializationContext context) {    super(context);}@Overridepublic void writeObject(Object value) throws IOException {    if(value instanceof DateTime) {        value = convertToDate((DateTime)value);    }    if(value instanceof LocalDate) {        value = convertToDate((LocalDate)value);    }    if(value instanceof LocalTime) {    value = convertToDate((LocalTime)value);    }    super.writeObject(value);}private Object convertToDate(LocalTime time) {    return time.toDateTimeToday().toDate();}private Object convertToDate(LocalDate date) {    return date.toDateMidnight().toDate();}private Object convertToDate(DateTime dateTime) {    return dateTime.toDate();}   }

用于Flex-> Java调用的自定义Marshaller

package ch.hedgesphere.core.blazeds.translator;import org.joda.time.DateTime;import org.joda.time.LocalDate;import org.joda.time.LocalTime;import flex.messaging.io.amf.translator.ASTranslator;public class HedgesphereASTranslator extends ASTranslator {@SuppressWarnings({"rawtypes"})@Overridepublic Object convert(Object originalValue, Class type) {    if( type.equals(DateTime.class)) {        return convertToDateTime(originalValue);    }    if( type.equals(LocalDate.class)) {    return convertToLocalDate(originalValue);     }    if( type.equals(LocalTime.class)) {        return convertToLocalTime(originalValue);    }    return super.convert(originalValue, type);}private Object convertToLocalTime(Object originalValue) {    return originalValue == null ? null : new LocalTime(originalValue);}private Object convertToLocalDate(Object originalValue) {    return originalValue == null ? null : new LocalDate(originalValue); }private Object convertToDateTime(Object originalValue) {    return originalValue == null ? null : new DateTime(originalValue);}@SuppressWarnings({"rawtypes"})@Overridepublic Object createInstance(Object source, Class type) {    return super.createInstance(source, type);}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407320.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号