RabbitMQ学习之messageconver插件实现(Gson)

RabbitMQ已经实现了Jackson的消息转换(Jackson2JsonMessageConverter),由于考虑到效率,如下使用Gson实现消息转换。

如下消息的转换类的接口MessageConverter,Jackson2JsonMessageConverter的父类AbstractJsonMessageConverter针对json转换的基类。


我们实现Gson2JsonMessageConverter转换类也继承AbstractJsonMessageConverter。

引入Gson的pom

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.             <groupId>com.google.code.gson</groupId>  
  3.             <artifactId>gson</artifactId>  
  4.             <version>2.3</version>  
  5.         </dependency>  
转换类实现如下:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.slimsmart.rabbitmq.demo.spring.tag;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5.   
  6. import org.apache.commons.logging.Log;  
  7. import org.apache.commons.logging.LogFactory;  
  8. import org.springframework.amqp.core.Message;  
  9. import org.springframework.amqp.core.MessageProperties;  
  10. import org.springframework.amqp.support.converter.AbstractJsonMessageConverter;  
  11. import org.springframework.amqp.support.converter.ClassMapper;  
  12. import org.springframework.amqp.support.converter.DefaultClassMapper;  
  13. import org.springframework.amqp.support.converter.MessageConversionException;  
  14.   
  15. import com.google.gson.Gson;  
  16.   
  17. public class Gson2JsonMessageConverter extends AbstractJsonMessageConverter {  
  18.       
  19.     private static Log log = LogFactory.getLog(Gson2JsonMessageConverter.class);  
  20.       
  21.     private static  ClassMapper classMapper =  new DefaultClassMapper();  
  22.   
  23.     private static Gson gson = new Gson();  
  24.   
  25.     public Gson2JsonMessageConverter() {  
  26.         super();  
  27.     }  
  28.   
  29.     @Override  
  30.     protected Message createMessage(Object object,  
  31.             MessageProperties messageProperties) {  
  32.         byte[] bytes = null;  
  33.         try {  
  34.             String jsonString = gson.toJson(object);  
  35.             bytes = jsonString.getBytes(getDefaultCharset());  
  36.         }  
  37.         catch (IOException e) {  
  38.             throw new MessageConversionException(  
  39.                     "Failed to convert Message content", e);  
  40.         }  
  41.         messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);  
  42.         messageProperties.setContentEncoding(getDefaultCharset());  
  43.         if (bytes != null) {  
  44.             messageProperties.setContentLength(bytes.length);  
  45.         }  
  46.         classMapper.fromClass(object.getClass(),messageProperties);  
  47.         return new Message(bytes, messageProperties);  
  48.     }  
  49.   
  50.     @Override  
  51.     public Object fromMessage(Message message)  
  52.             throws MessageConversionException {  
  53.         Object content = null;  
  54.         MessageProperties properties = message.getMessageProperties();  
  55.         if (properties != null) {  
  56.             String contentType = properties.getContentType();  
  57.             if (contentType != null && contentType.contains("json")) {  
  58.                 String encoding = properties.getContentEncoding();  
  59.                 if (encoding == null) {  
  60.                     encoding = getDefaultCharset();  
  61.                 }  
  62.                 try {  
  63.                         Class<?> targetClass = getClassMapper().toClass(  
  64.                                 message.getMessageProperties());  
  65.                         content = convertBytesToObject(message.getBody(),  
  66.                                 encoding, targetClass);  
  67.                 }  
  68.                 catch (IOException e) {  
  69.                     throw new MessageConversionException(  
  70.                             "Failed to convert Message content", e);  
  71.                 }  
  72.             }  
  73.             else {  
  74.                 log.warn("Could not convert incoming message with content-type ["  
  75.                         + contentType + "]");  
  76.             }  
  77.         }  
  78.         if (content == null) {  
  79.             content = message.getBody();  
  80.         }  
  81.         return content;  
  82.     }  
  83.   
  84.     private Object convertBytesToObject(byte[] body, String encoding,  
  85.             Class<?> clazz) throws UnsupportedEncodingException {  
  86.         String contentAsString = new String(body, encoding);  
  87.         return gson.fromJson(contentAsString, clazz);  
  88.     }  
  89. }  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值