什么是Spring JMS中的JmsTemplate回调? [英] What is a JmsTemplate callback in Spring JMS?

查看:496
本文介绍了什么是Spring JMS中的JmsTemplate回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 Spring JMS (以及一般的JMS),我对 JmsTemplate 回调的概念有些怀疑。

this is my first time with Spring JMS (and with JMS in general) and I have some doubts related the concept of the JmsTemplate callback.

我知道 JmsTemplate 是一个从Spring提供的类:

I know that the JmsTemplate is a class provided from Spring to:


  • 减少样板代码。

  • 透明地管理资源。

  • 将已检查的异常转换为运行时等效项。

  • 提供便利方法和回调。

  • Reduces boilerplate code.
  • Manages resources transparently.
  • Converts checked exceptions to runtime equivalents.
  • Provides convenience methods and callbacks.

并且它用于消息生成和同步消息接收。它简化了JMS的使用,因为它在发送或同步接收消息时处理资源的创建和释放。

and that it is used for message production and synchronous message reception. It simplifies the use of JMS since it handles the creation and release of resources when sending or synchronously receiving messages.

阅读Spring官方文档(此处:http://docs.spring.io/spring/docs/current/spring-framework-reference /html/jms.html )我发现:

Reading the Spring official documentation (here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html) I found:


使用JmsTemplate的代码只需要实现回调
接口为他们提供明确定义的高级合同。
MessageCreator回调接口创建一条消息,给出由JmsTemplate中的调用代码提供的Session

Code that uses the JmsTemplate only needs to implement callback interfaces giving them a clearly defined high level contract. The MessageCreator callback interface creates a message given a Session provided by the calling code in JmsTemplate.

这我不清楚。这些回调究竟是什么?

This is not clear for me. What exactly are these callback?

一开始我认为回调是从 JmsTemplate 提供的一种方法,但在这里阅读它看起来更类似于我的界面必须实施。它是如何工作的?

At the beginning I thought that a callback are a method provided from the JmsTemplate but reading here it seems something more similar to an interface that I have to implement. How it works?

我也找到了这个例子:

发送一个POJO通过JMS(使用JmsTemplate):

public class JmsOrderManager implements OrderManager {
    @Autowired JmsTemplate jmsTemplate;
    @Autowired Destination orderQueue;

    public void placeOrder(Order order) {
        String stringMessage = "New order " + order.getNumber();
        jmsTemplate.convertAndSend("messageQueue", stringMessage );
        // use destination resolver and message converter
        jmsTemplate.convertAndSend(orderQueue, order); // use message converter
        jmsTemplate.convertAndSend(order); // use converter and default destination
    }
}

我以为 convertAndSend()方法是JmsTemplate 回调,但可能此断言不正确。

I thought that the convertAndSend() method is a JmsTemplate callback but probably this assertion is not correct.

你能解释一下JmsTemplate回调到底是什么吗?

Can you explain me what exactly is a JmsTemplate callback?

推荐答案

Spring模板类常见的设计原则是提供
辅助方法来执行常见操作
,对于更复杂的用法,委托$ b $的本质b处理任务到用户实现的回调接口。

The design principle common to Spring template classes is to provide helper methods to perform common operations and for more sophisticated usage, delegate the essence of the processing task to user implemented callback interfaces.

在JMS Connection中将从工厂获得

In JMS Connection will be obtained from a factory

=>从该连接会话创建,一个seesion是一个工作单元,它还提供事务

=> From that connection session is created, a seesion is a unit of work, it also provides transaction

=>从会话中创建不同类型的JMS消息,如TextMessage,ObjectMessage,MapMessage,BytesMessage和StreamMessage以下列方式
session.createTextMessage(hello queue world);
session.createObjectMessage(someSerializedObject)等等

=> from session you create different types of JMS Message like TextMessage, ObjectMessage, MapMessage, BytesMessage and StreamMessage in following ways session.createTextMessage("hello queue world"); session.createObjectMessage(someSerializedObject) and so on

=>同一会话还负责创建MessageProducer session.createProducer(destination)和MessageConsumer会话的实例.createConsumer(destination)

=> The same session is also responsible for creating instance of MessageProducer session.createProducer(destination) and MessageConsumer session.createConsumer(destination)

您有以下convertAndSend可能性(重载方法):

You have following convertAndSend possibilities (Overloaded methods):

        jmsTemplate.convertAndSend(message)
        jmsTemplate.convertAndSend(destination, message)
        jmsTemplate.convertAndSend(message, postProcessor)
        jmsTemplate.convertAndSend(destinationName, message)
        jmsTemplate.convertAndSend(destination, message, postProcessor)
        jmsTemplate.convertAndSend(destinationName, message, postProcessor)

发生了什么?
回调的基本用法,如第3和第5签名,您可以在将对象转换为JMS消息后更改消息
通过CONFI消息MessageConverter。
你看到一个目的地由DestinationResolver解决,如果是6日,你没有传递它,它将从JNDI解析,如果它在那里注册。

What is happening? The basic use of callback like 3rd 5th and 6th signature is you can change the message after being converting the object to a JMS message through a configured MessageConverter. You are seeing an actual destination to be resolved by a DestinationResolver in case of 6th, you are not passing it, it will be resolved from JNDI, if it is registered there.

这是什么意思?

jmsTemplate.send(this.queue, new MessageCreator() {
public  Message createMessage(Session session) throws JMSException {
        return  session.createTextMessage("hello queue world");
    }
});

在这个例子中,您将看到使用JMS模板的send()方法提供匿名实现( callback)其中该方法允许您访问会话对象,并从该会话
您创建自定义的session.createTextMessage(hello queue world)消息。

Here in this example you are seeing with send() method of JMS template you are providing anonymous implemetation(callback) where the method gives you access to to Session Object and from that session you created customized session.createTextMessage("hello queue world") message.

convertAndSend中的相同方式(您可以访问要修改的后处理器)

Same way in convertAndSend(you can get access to postprocessors to modify)

public void ConvertSend() {
Map map = new HashMap();
map.put("Name", "Vimal");
map.put("Age", new Integer(45));
jmsTemplate.convertAndSend("jmsQueue", map, new MessagePostProcessor() {
    public Message postProcessMessage(Message message) throws JMSException {
        message.setIntProperty("ID", 9999);
        message.setJMSCorrelationID("123-99999");
        return message;
       }
   });
}

消息对象已创建,但您正在修改它(添加另外两个属性,在除名称和年龄外)。 MessagePostProcessor接口允许您在转换后但在发送之前访问该消息。

Message Object was created but you are modifying it(adding two more properties, in addition to name and age). The MessagePostProcessor interface gives you access to the message after it has been converted, but before it is sent.

换句话说!消息属性,标题和正文的设置不能封装在转换器类(SimpleMessageConverter)中,而是MessagePostProcessor界面使您可以在转换后但在发送之前访问该消息。

In other words! setting of message properties, headers and body can't be encapsulated inside a converter class(SimpleMessageConverter), but the MessagePostProcessor Interface gives you access to the message after it has been converted, but before sent.

这篇关于什么是Spring JMS中的JmsTemplate回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆