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

前端之家收集整理的这篇文章主要介绍了通过BlazeDS从Java到Flex的自定义编组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的团队正在使用BlazeDS在基于 Spring的服务器上放置一个概念验证Flex应用程序.

我们做了很多日期计算,因此我们在整个代码和域模型中广泛使用Joda Time.

我们现在正试图弄清楚我们如何继续在我们的DTO中使用Joda Time,这些DTO通过BlazeDS与Flex前端来回发送.

我们的目标是在Flex端使用Actionscript 3数据类型Date,并将该映射用于我们在Java端使用Joda time的DateTime,LocalDate和LocalTime类型.

我们可以解决在使用插入BlazeDS的自定义类型marshaller调用Java时转换Actionscript 3的Date类型的问题,但这似乎只针对Flex-> Java / BlazeDS方向调用,而不是针对Java / BlazeDS->调用.弯曲方向.

我现在正在研究BlazeDS的自定义PropertyProxy实现,但这看起来也不正确.

另一个想法是在我们的Java DTO上实现Externalizable,但这看起来似乎太多了,特别是当我看到BlazeDS的竞争对手GraniteDS时,它显示了在他们的文档中使用简单的类型转换器插入Joda Time支持

任何想法都赞赏.

解决方法

好的 – 我自己找到了答案.这涉及编写我自己的AMF端点类相关的序列化类.我必须说,7000以上的人一直是黑客攻击BlazeDS的灵感源泉.

这段代码应该真正包含在BlazeDS本身或一些开源扩展项目中 – 它是如此基础.

渠道定义

  1. <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
  2. <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="ch.hedgesphere.core.blazeds.endpoint.AMFEndpoint"/>
  3.  
  4. <properties>
  5. <serialization>
  6. <type-marshaller>ch.hedgesphere.core.blazeds.translator.HedgesphereASTranslator</type-marshaller>
  7. </serialization>
  8. </properties>
  9.  
  10. </channel-definition>

自定义AMF端点

  1. package ch.hedgesphere.core.blazeds.endpoint;
  2.  
  3. import ch.hedgesphere.core.blazeds.serialization.Serializer;
  4.  
  5. public class AMFEndpoint extends flex.messaging.endpoints.AMFEndpoint {
  6.  
  7. @Override
  8. protected String getSerializerClassName() {
  9. return Serializer.class.getName();
  10. }
  11.  
  12. }

自定义序列化器

  1. package ch.hedgesphere.core.blazeds.serialization;
  2.  
  3. import java.io.OutputStream;
  4.  
  5. import flex.messaging.io.MessageIOConstants;
  6. import flex.messaging.io.SerializationContext;
  7. import flex.messaging.io.amf.AmfMessageSerializer;
  8. import flex.messaging.io.amf.AmfTrace;
  9.  
  10. public class Serializer extends AmfMessageSerializer {
  11.  
  12. @Override
  13. public void initialize(SerializationContext context,OutputStream out,AmfTrace trace)
  14. {
  15. amfOut = new AMF0Output(context);
  16. amfOut.setOutputStream(out);
  17. amfOut.setAvmPlus(version >= MessageIOConstants.AMF3);
  18.  
  19. debugTrace = trace;
  20. isDebug = trace != null;
  21. amfOut.setDebugTrace(debugTrace);
  22. }
  23. }

自定义AMF 0处理

  1. package ch.hedgesphere.core.blazeds.serialization;
  2.  
  3. import flex.messaging.io.SerializationContext;
  4.  
  5. public class AMF0Output extends flex.messaging.io.amf.Amf0Output {
  6.  
  7. public AMF0Output(SerializationContext context) {
  8. super(context);
  9. }
  10.  
  11. @Override
  12. protected void createAMF3Output()
  13. {
  14. avmPlusOutput = new AMF3Output(context);
  15. avmPlusOutput.setOutputStream(out);
  16. avmPlusOutput.setDebugTrace(trace);
  17. }
  18. }

自定义AMF 3处理

  1. package ch.hedgesphere.core.blazeds.serialization;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.joda.time.DateTime;
  6. import org.joda.time.LocalDate;
  7. import org.joda.time.LocalTime;
  8.  
  9. import flex.messaging.io.SerializationContext;
  10.  
  11. public class AMF3Output extends flex.messaging.io.amf.Amf3Output {
  12.  
  13. public AMF3Output(SerializationContext context) {
  14. super(context);
  15. }
  16.  
  17. @Override
  18. public void writeObject(Object value) throws IOException {
  19. if(value instanceof DateTime) {
  20. value = convertToDate((DateTime)value);
  21. }
  22. if(value instanceof LocalDate) {
  23. value = convertToDate((LocalDate)value);
  24. }
  25. if(value instanceof LocalTime) {
  26. value = convertToDate((LocalTime)value);
  27. }
  28. super.writeObject(value);
  29. }
  30.  
  31. private Object convertToDate(LocalTime time) {
  32. return time.toDateTimeToday().toDate();
  33. }
  34.  
  35. private Object convertToDate(LocalDate date) {
  36. return date.toDateMidnight().toDate();
  37. }
  38.  
  39. private Object convertToDate(DateTime dateTime) {
  40. return dateTime.toDate();
  41. }
  42. }

自定义Marshaller for Flex-> Java Calling

  1. package ch.hedgesphere.core.blazeds.translator;
  2.  
  3. import org.joda.time.DateTime;
  4. import org.joda.time.LocalDate;
  5. import org.joda.time.LocalTime;
  6.  
  7. import flex.messaging.io.amf.translator.ASTranslator;
  8.  
  9.  
  10. public class HedgesphereASTranslator extends ASTranslator {
  11.  
  12. @SuppressWarnings({"rawtypes"})
  13. @Override
  14. public Object convert(Object originalValue,Class type) {
  15. if( type.equals(DateTime.class)) {
  16. return convertToDateTime(originalValue);
  17. }
  18. if( type.equals(LocalDate.class)) {
  19. return convertToLocalDate(originalValue);
  20. }
  21. if( type.equals(LocalTime.class)) {
  22. return convertToLocalTime(originalValue);
  23. }
  24.  
  25. return super.convert(originalValue,type);
  26. }
  27.  
  28. private Object convertToLocalTime(Object originalValue) {
  29. return originalValue == null ? null : new LocalTime(originalValue);
  30. }
  31.  
  32. private Object convertToLocalDate(Object originalValue) {
  33. return originalValue == null ? null : new LocalDate(originalValue);
  34. }
  35.  
  36. private Object convertToDateTime(Object originalValue) {
  37. return originalValue == null ? null : new DateTime(originalValue);
  38. }
  39.  
  40. @SuppressWarnings({"rawtypes"})
  41. @Override
  42. public Object createInstance(Object source,Class type) {
  43. return super.createInstance(source,type);
  44. }
  45. }

猜你在找的Flex相关文章