我想将创建/解析目的地的细节封装到某种 DestinationResolver 实现中。
详细信息包括创建连接、创建会话、创建/解析目标。
这样的实现会是一个糟糕的方法吗?
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);
}
}
更新
将特定任务包装在执行操作中是否有更好的方法?
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jms/core/JmsTemplate.html#execute(org.springframework.jms.core.会话回调)
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?
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jms/core/JmsTemplate.html#execute(org.springframework.jms.core.SessionCallback)
发布评论
评论(1)
不确定我是否完全理解你,JmsTemplate 已经为从 JMS 提供者发送/接收消息提供了非常好的抽象 - 您不必处理原始 JMS ConnectionFactory 或 Session。除非您对会话有特定需求 - 比如说在同一会话中实现队列浏览器或请求/响应等,如果是这种情况,则像您提到的那样使用 SessionCallback 和 JmsTemplate 的执行方法的方法是一个很好的方法想法而不是需要编写自己的抽象。
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.