`
丁树同
  • 浏览: 9664 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Using MessageConverter

 
阅读更多

In order to facilitate the sending of domain model objects, the JmsTemplate has various send methods that take a Java object as an argument for a message's data content. The overloaded methods convertAndSend and receiveAndConvert in JmsTemplate delegate the conversion process to an instance of the MessageConverter interface. This interface defines a simple contract to convert between Java objects and JMS messages. The default implementation SimpleMessageConverter supports conversion between String and TextMessage, byte[] and BytesMesssage, and java.util.Map and MapMessage. By using the converter, you and your application code can focus on the business object that is being sent or received via JMS and not be concerned with the details of how it is represented as a JMS message.

The sandbox currently includes a MapMessageConverter which uses reflection to convert between a JavaBean and a MapMessage. Other popular implementations choices you might implement yourself are Converters that use an existing XML marshalling package, such as JAXB, Castor, XMLBeans, or XStream, to create a TextMessage representing the object.

To accommodate the setting of a message's properties, headers, and body that can not be generically encapsulated inside a converter class, the MessagePostProcessor interface gives you access to the message after it has been converted, but before it is sent. The example below demonstrates how to modify a message header and a property after a java.util.Map is converted to a message.

public void sendWithConversion() {
    Map map = new HashMap();
    map.put("Name", "Mark");
    map.put("Age", new Integer(47));
    jmsTemplate.convertAndSend("testQueue", map, new MessagePostProcessor() {
        public Message postProcessMessage(Message message) throws JMSException {
            message.setIntProperty("AccountID", 1234);
            message.setJMSCorrelationID("123-00001");
            return message;
        }
    });
}

 This results in a message of the form:

MapMessage={
    Header={
        ... standard headers ...
        CorrelationID={123-00001}
    }
    Properties={
        AccountID={Integer:1234}
    }
    Fields={
        Name={String:Mark}
        Age={Integer:47}
    } 
}

 

From: http://docs.spring.io/spring/docs/3.0.0.RC2/reference/html/ch21s03.html

分享到:
评论

相关推荐

    Spring整合JMS(三)——MessageConverter介绍

    本文将深入探讨Spring与JMS的整合,特别是MessageConverter的概念及其作用。 一、Spring JMS概述 Spring提供了对JMS的全面支持,包括连接工厂配置、消息生产者、消费者以及消息转换器等。JMS允许应用程序通过消息...

    springMVC rest风格视图解析

    在实际开发中,我们可能还需要自定义MessageConverter,或者调整内置MessageConverter的优先级,以满足特定的项目需求。这可以通过在配置文件中配置`<mvc:message-converters>`来实现。 总的来说,Spring MVC通过其...

    基于springboot集成mybatis、durid和自定义消息转换项目

    在本项目中,我们主要探讨的是如何将Spring Boot与MyBatis、Druid以及自定义消息转换器(MessageConverter)进行集成,以构建一个高效、可扩展的应用程序。Spring Boot以其简洁的配置和快速的开发体验深受开发者喜爱...

    ActiveMQ与spring集成实例之使用消息转换器

    <bean id="jsonMessageConverter" class="org.springframework.jms.support.converter.MappingJackson2MessageConverter"> <bean id="messageConverter" class="org.springframework.jms.config....

    SpringMVC使用@ResponseBody.rar

    例如,如果客户端请求JSON,而服务器端返回一个XML对象,`@ResponseBody`会尝试找到适当的`MessageConverter`来完成转换。 4. **异常处理** 当`@ResponseBody`方法抛出异常时,Spring MVC会自动捕获并处理这些异常...

    spring-amqp 1.5.3源码

    4. **MessageConverter**:Spring AMQP提供了一系列的MessageConverter实现,如Jackson2JsonMessageConverter,用于在消息对象与AMQP Message之间进行转换,确保数据在传输过程中的正确性。 在源码中,我们可以看到...

    springMVC的消息转换器(Message Converter) 1

    MessageConverter的作用就是将这些格式化的数据转化为Java对象,反之亦然。 请求体通常包含JSON、XML或其他格式的数据,而查询参数和表单参数则以不同的方式存在。Content-Type头部用来指示请求体或响应体的数据...

    springmvc log4j2 logback 注解 jackson 日志脱敏实现源码

    在IT行业中,日志记录是系统监控和故障排查的关键环节,但同时也涉及到用户隐私保护的问题。日志脱敏就是一种确保敏感数据在日志中不被泄露的技术手段。本资源包含的是关于`SpringMVC`、`Log4j2`、`Logback`以及`...

    lucene-test

    相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean, 配置一些messageconverter。即解决了@Controller注解的使用前提配置。

    Spring JMS

    同时,Spring JMS 也提供了 MessageConverter 抽象,以在 Java 对象和 JMS 消息之间进行转换。 在 Spring JMS 中,还提供了管理 JMS 目标的不同策略,比如针对 JNDI 中保存的目标的服务定位器。同时,Spring JMS 也...

    SpringMVC源码总结(四)由StringHttpMessageConverter引出的客户端服务器端之间的乱码过程分析

    Spring MVC在接收到请求时,会通过MessageConverter链来处理请求体。StringHttpMessageConverter是这个链上的一员,它会检查请求头中的Content-Type字段来确定数据的字符编码。如果找不到合适的编码,Spring MVC会...

    JMS-Spring

    3. **MessageConverter**: 当需要在发送和接收消息时进行类型转换时,`MessageConverter`接口就派上用场了。例如,我们可以使用`SimpleMessageConverter`,它默认支持字符串和byte数组的转换。 在实际应用中,我们...

    springboot集成rabbitMQ

    public Jackson2JsonMessageConverter producerJackson2MessageConverter() { return new Jackson2JsonMessageConverter(); } } ``` ##### 2. 生产者的实现 - **创建消息生产者** (`MessegeProducer.java`):...

    spring-boot集成RabbitMQ

    如果需要自定义消息的序列化和反序列化,可以实现 `MessageConverter` 接口,比如使用 Jackson JSON 支持: ```java @Component public class JsonMessageConverter extends AbstractMessageConverter { ...

    springboot-源码解析-个人分析

    `MessageConverter`是另一个扩展点,例如`SimpleRabbitListenerContainerFactory`的子类可以重写`setMessageConverter`方法来设置自定义的转换器,如`Jackson2XmlMessageConverter`、`Jackson2JsonMessageConverter`...

    Springboot-rabbitmq.zip

    例如,可以创建一个支持JSON的`Jackson2JsonMessageConverter`,或者根据业务需求实现自定义的转换逻辑。 此外,项目可能还涉及到`AmqpTemplate`的扩展,以实现更高级的功能,如延迟消息、优先级消息或者死信交换等...

    jackson-core-2.5.0.jar

    通过自定义`MessageConverter`或者调整Spring的配置,我们可以定制Jackson的行为,比如改变默认的日期格式,或者添加自定义的序列化模块。 在实际开发中,`jackson-core-2.5.0.jar`的使用往往结合`jackson-databind...

    springMVC 源码

    8. **MessageConverter**:在处理HTTP请求和响应时,MessageConverter负责将Java对象序列化和反序列化为HTTP消息体,支持JSON、XML等多种格式。 9. **Exception Handler**:Spring MVC允许我们定义异常处理器,统一...

    8583Server.zip

    通过`MessageConverter`,我们可以将8583报文的二进制数据转换为Java对象,方便业务逻辑处理。此外,Spring的`WebSocket`和`WebSocketContainer`组件也可用于更高级的WebSocket通信,但此处可能是基于简单TCP Socket...

    springmvc操作json数据所需的jar包

    为了在Spring MVC中处理JSON,我们需要在配置文件中启用`@EnableWebMvc`或`<mvc:annotation-driven>`,这会自动配置`MessageConverter`,包括处理JSON的`MappingJackson2HttpMessageConverter`(如果已经添加了...

Global site tag (gtag.js) - Google Analytics