Spring Framework JMS Destination name resolution approach
-
19-04-2021 - |
Question
I wanted to encapsulate the details of creating / resolving a destination into some sort of DestinationResolver implementation.
The details being creating a connection, creating a session, creating / resolving a destination.
Would such an implementation be a bad approach?
public class SessionAwareDestinationResolver extends DynamicDestinationResolver {
private Session session;
@Inject
public SessionAwareDestinationResolver(ConnectionFactory connectionFactory) {
try {
Connection connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException ex) {
throw JmsUtils.convertJmsAccessException(ex);
}
}
public Destination resolveDestinationName(String destinationName,
boolean pubSubDomain) throws JMSException {
return super.resolveDestinationName(session, destinationName,
pubSubDomain);
}
}
UPDATE
Would a better approach just to wrap the specific task in an execute action?
Solution
Not sure if I completely understood you, JmsTemplate already provides a very good abstraction to sending/receiving message from JMS providers - you do not have to deal with raw JMS ConnectionFactory or Session. Unless you have a specific need for a Session - say to implement a queue browser or a request/response in the same session etc, if that is the case the approach of using a SessionCallback with execute method of JmsTemplate like you have mentioned is a good idea rather than needing to write your own abstraction.