Spring JMS integration module that simplifies JMS development by providing template-based messaging, message-driven POJOs, declarative transaction management, and comprehensive exception handling
—
Central messaging operations using the template pattern for synchronous JMS operations. The JmsTemplate provides a consistent programming model for sending, receiving, and browsing messages with comprehensive configuration options and automatic resource management.
The central class for synchronous JMS operations, providing template-based access to JMS destinations with automatic resource management and exception translation.
/**
* Central class for synchronous JMS operations with template pattern.
* Handles connection and session management automatically.
*/
public class JmsTemplate implements JmsOperations {
// Constructors
public JmsTemplate();
public JmsTemplate(ConnectionFactory connectionFactory);
// Connection configuration
public void setConnectionFactory(ConnectionFactory connectionFactory);
public ConnectionFactory getConnectionFactory();
// Default destination configuration
public void setDefaultDestinationName(String defaultDestinationName);
public void setDefaultDestination(Destination defaultDestination);
// Message conversion configuration
public void setMessageConverter(MessageConverter messageConverter);
public MessageConverter getMessageConverter();
// Timeout configuration
public void setReceiveTimeout(long receiveTimeout);
public long getReceiveTimeout();
public void setDeliveryDelay(long deliveryDelay);
// QoS configuration
public void setQosSettings(QosSettings qosSettings);
public void setDeliveryPersistent(boolean deliveryPersistent);
public void setPriority(int priority);
public void setTimeToLive(long timeToLive);
// Pub/Sub configuration
public void setPubSubDomain(boolean pubSubDomain);
public boolean isPubSubDomain();
// Session configuration
public void setSessionAcknowledgeMode(int sessionAcknowledgeMode);
public void setSessionTransacted(boolean sessionTransacted);
}Execute custom operations within JMS Session and MessageProducer contexts with automatic resource management.
/**
* Execute the action specified by the given action object within a JMS Session
* @param action callback object that exposes the session
* @return the result object from working with the session
* @throws JmsException in case of JMS errors
*/
public <T> T execute(SessionCallback<T> action) throws JmsException;
/**
* Execute the action specified by the given action object within a JMS Session and MessageProducer
* @param action callback object that exposes the session and producer
* @return the result object from working with the session and producer
* @throws JmsException in case of JMS errors
*/
public <T> T execute(ProducerCallback<T> action) throws JmsException;
/**
* Execute the action with a MessageProducer for the default destination
* @param action callback object that exposes the session and producer
* @return the result object from working with the session and producer
* @throws JmsException in case of JMS errors
*/
public <T> T execute(String destinationName, ProducerCallback<T> action) throws JmsException;Send messages to destinations with various configuration options and message creation patterns.
/**
* Send a message using a MessageCreator callback to create the message
* @param destinationName the name of the destination to send to
* @param messageCreator callback to create the message
* @throws JmsException in case of JMS errors
*/
public void send(String destinationName, MessageCreator messageCreator) throws JmsException;
/**
* Send a message to the specified destination
* @param destination the destination to send to
* @param messageCreator callback to create the message
* @throws JmsException in case of JMS errors
*/
public void send(Destination destination, MessageCreator messageCreator) throws JmsException;
/**
* Send a message to the default destination
* @param messageCreator callback to create the message
* @throws JmsException in case of JMS errors
*/
public void send(MessageCreator messageCreator) throws JmsException;
/**
* Convert an object to a message and send it to the specified destination
* @param destinationName the name of the destination to send to
* @param message the object to convert and send
* @throws JmsException in case of JMS errors
*/
public void convertAndSend(String destinationName, Object message) throws JmsException;
/**
* Convert an object to a message and send it to the specified destination with post-processing
* @param destinationName the name of the destination to send to
* @param message the object to convert and send
* @param postProcessor callback to post-process the message
* @throws JmsException in case of JMS errors
*/
public void convertAndSend(String destinationName, Object message, MessagePostProcessor postProcessor)
throws JmsException;Receive messages from destinations synchronously with timeout support and automatic message conversion.
/**
* Receive a message from the specified destination
* @param destinationName the name of the destination to receive from
* @return the received message, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Message receive(String destinationName) throws JmsException;
/**
* Receive a message from the specified destination
* @param destination the destination to receive from
* @return the received message, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Message receive(Destination destination) throws JmsException;
/**
* Receive a message from the default destination
* @return the received message, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Message receive() throws JmsException;
/**
* Receive a message and convert it to an object
* @param destinationName the name of the destination to receive from
* @return the converted object, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Object receiveAndConvert(String destinationName) throws JmsException;
/**
* Receive a message and convert it to the specified type
* @param destinationName the name of the destination to receive from
* @param targetClass the target class for conversion
* @return the converted object, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public <T> T receiveAndConvert(String destinationName, Class<T> targetClass) throws JmsException;Send messages and synchronously receive replies, useful for request-reply messaging patterns.
/**
* Send a message and receive the reply from a temporary queue
* @param destinationName the name of the destination to send to
* @param messageCreator callback to create the message
* @return the reply message, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Message sendAndReceive(String destinationName, MessageCreator messageCreator) throws JmsException;
/**
* Send a message and receive the reply from a temporary queue
* @param destination the destination to send to
* @param messageCreator callback to create the message
* @return the reply message, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Message sendAndReceive(Destination destination, MessageCreator messageCreator) throws JmsException;
/**
* Send a message to the default destination and receive the reply
* @param messageCreator callback to create the message
* @return the reply message, or null if timeout occurred
* @throws JmsException in case of JMS errors
*/
public Message sendAndReceive(MessageCreator messageCreator) throws JmsException;Browse messages in queues without consuming them, useful for monitoring and debugging message queues.
/**
* Browse messages in a queue using a callback
* @param destinationName the name of the queue to browse
* @param action callback for processing each message
* @return the result from the callback
* @throws JmsException in case of JMS errors
*/
public <T> T browse(String destinationName, BrowserCallback<T> action) throws JmsException;
/**
* Browse messages in a queue with a message selector
* @param destinationName the name of the queue to browse
* @param messageSelector JMS message selector expression
* @param action callback for processing each message
* @return the result from the callback
* @throws JmsException in case of JMS errors
*/
public <T> T browseSelected(String destinationName, String messageSelector, BrowserCallback<T> action)
throws JmsException;Callback interfaces for advanced JMS operations providing access to JMS Session and Producer objects.
/**
* Callback interface for creating JMS messages
*/
public interface MessageCreator {
/**
* Create a JMS message
* @param session the JMS session to use
* @return the created message
* @throws JMSException in case of JMS errors
*/
Message createMessage(Session session) throws JMSException;
}
/**
* Callback interface for post-processing messages after conversion
*/
public interface MessagePostProcessor {
/**
* Post-process the given message
* @param message the message to post-process
* @return the post-processed message
* @throws JMSException in case of JMS errors
*/
Message postProcessMessage(Message message) throws JMSException;
}
/**
* Callback interface for working directly with JMS Session
*/
public interface SessionCallback<T> {
/**
* Execute operations within a JMS session
* @param session the JMS session
* @return the result object
* @throws JMSException in case of JMS errors
*/
T doInJms(Session session) throws JMSException;
}
/**
* Callback interface for working with MessageProducer
*/
public interface ProducerCallback<T> {
/**
* Execute operations with a MessageProducer
* @param session the JMS session
* @param producer the MessageProducer
* @return the result object
* @throws JMSException in case of JMS errors
*/
T doInJms(Session session, MessageProducer producer) throws JMSException;
}
/**
* Callback interface for browsing queue messages
*/
public interface BrowserCallback<T> {
/**
* Process messages during queue browsing
* @param session the JMS session
* @param browser the QueueBrowser
* @return the result object
* @throws JMSException in case of JMS errors
*/
T doInJms(Session session, QueueBrowser browser) throws JMSException;
}Usage Examples:
import org.springframework.jms.core.*;
import javax.jms.*;
// Basic message sending
@Service
public class OrderService {
@Autowired
private JmsTemplate jmsTemplate;
public void sendOrder(Order order) {
jmsTemplate.convertAndSend("order.queue", order);
}
// Custom message creation
public void sendOrderWithHeaders(Order order) {
jmsTemplate.send("order.queue", session -> {
ObjectMessage message = session.createObjectMessage(order);
message.setStringProperty("orderType", order.getType());
message.setIntProperty("priority", order.getPriority());
return message;
});
}
// Receive messages synchronously
public Order receiveOrder() {
return (Order) jmsTemplate.receiveAndConvert("order.queue");
}
// Browse messages without consuming
public List<String> browseOrderIds() {
return jmsTemplate.browse("order.queue", (session, browser) -> {
List<String> orderIds = new ArrayList<>();
Enumeration<?> messages = browser.getEnumeration();
while (messages.hasMoreElements()) {
Message message = (Message) messages.nextElement();
orderIds.add(message.getJMSMessageID());
}
return orderIds;
});
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-springframework--spring-jms