What is the JmsTemplate callback in Spring JMS?

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

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

  • Reduces boilerplate code.
  • Resource management is transparent.
  • Converts checked exceptions to runtime equivalents.
  • Provides convenient methods and callbacks.

and that it is used to create messages and receive messages synchronously. This simplifies the use of JMS because it handles the creation and release of resources when sending or receiving messages synchronously.

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

Code that uses JmsTemplate should only call back interfaces that give them a well-defined high-level contract. The MessageCreator callback interface creates a session-specific message provided by the calling code in the JmsTemplate.

This is not clear to me. What exactly are these callbacks?

At the beginning, I thought that the callback is a method provided with JmsTemplate , but here it seems to be more like an interface that I have to implement. How it works?

I also found this example:

SENDING PLEASE JMS (using 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 } } 

I thought the convertAndSend () method is a JMSTemplate callback but, probably, this statement is incorrect.

Can you explain to me what a JmsTemplate callback is?

+5
source share
2 answers

The design principle common to Spring template classes is to provide helper methods for performing common operations and for more complex use, delegate the nature of the processing task to the callback user interfaces.

In JMS Connection will be obtained from factory

=> From this connection session, visibility is created - a unit of work, it also provides a transaction

=> From a session, you create various types of JMS messages, such as TextMessage, ObjectMessage, MapMessage, BytesMessage and StreamMessage in the following ways session.createTextMessage ("hello queue world"); session.createObjectMessage (someSerializedObject) etc.

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

You have the following convertAndSend features (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) 

What's happening? The main use of a callback, for example, the 3rd and 6th signature, you can change the message after converting the object to a JMS message through the configured MessageConverter. You see that the actual destination must be allowed by the DestinationResolver in case of the 6th, you do not pass it, it will be allowed from the JNDI if it is registered there.

What does it mean?

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

Here, in this example, you see the send () method of the JMS template that you provide an anonymous implementation (callback), where the method gives you access to the session object and from this session you created the customized message session.createTextMessage ("hello queue world" )

Same thing in convertAndSend (you can access postprocessors for change)

 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; } }); } 

A message object was created, but you are modifying it (adding two more properties besides the name and age). The MessagePostProcessor interface gives you access to the message after converting it, but before sending it.

In other words! setting message properties, headers and body cannot be encapsulated inside the converter class (SimpleMessageConverter), but the MessagePostProcessor interface gives you access to the message after it is converted, but before sending.

+6
source

This allows you to get low-level access to JMS objects (for example, Session in SessionCallback using execute() ), while still reliably freeing resources when the operation is completed.

+3
source

All Articles