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.