Spring WebSphere Application Server 上的 JmsTemplate 和连接 jndi 查找
Spring JmsTemplate and connection jndi lookup on WebSphere Application Server
我正在使用 JmsTemplate 发送和接收消息 to/from IBM MQ 队列。
我的应用程序安装在 WebSphere 应用程序服务器 8.5 上,为了检索连接,我使用了 jndi 查找。
我的Spring豆子:
<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jndiTest" />
<property name="lookupOnStartup" value="false" />
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.jms.QueueConnectionFactory" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="receiveTimeout" value="10000" />
<property name="sessionAcknowledgeMode" value="1" />
</bean>
<bean id="mqServerDao" class="MqServerDao" >
<constructor-arg name="jmsTemplate" ref="jmsTemplate" />
</bean>
我的Javaclass:
public class MqServerDao {
private JmsTemplate jmsTemplate;
public MqServerDao(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public String write(byte[] request, final String correlationId)
throws Exception {
MQQueue mqQueue = new MQQueue(MQ_INPUT_QUEUE);
mqQueue.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
MqRequestMessageCreator messageCreator = new MqRequestMessageCreator(
request, correlationId);
jmsTemplate.send(mqQueue, messageCreator);
return messageCreator.getMessageId();
}
public byte[] read(String messageId, String correlationId) throws Exception {
MQQueue mqQueue = new MQQueue(MQ_OUTPUT_QUEUE);
mqQueue.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
String messageSelector = "JMSCorrelationID = 'ID:" + correlationId
+ "' AND JMSMessageID = '" + messageId + "'";
TextMessage receiveMessage = (TextMessage) jmsTemplate.receiveSelected(
mqQueue, messageSelector);
return receiveMessage.getText().getBytes();
}
}
我想知道这样做是否正确,我有一些问题:
- 是否建议添加 CachingConnectionFactory 或管理 che 连接的应用程序服务器本身?
- 这是使用JmsTemplate的正确方法吗?同时调用两次MqServerDao的"write"方法安全吗?或者我会在 "write" 和 "read" 方法中创建一个新的 JmsTemplate 实例吗?
- WebSphere 将为您管理连接。来自 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html:
In a Java EE environment, make sure that the ConnectionFactory is
obtained from the application's environment naming context via JNDI;
application servers typically expose pooled, transaction-aware
factories there.
- Spring 模板 类,包括
JmsTemplate
,旨在成为可重用的线程安全单例。你绝对不想继续创建新的。
我找了这方面的权威参考资料,但没有找到。 (您可能认为它会在上面的 JavaDoc link 中。IMO,Spring 的文档通常有一些不足之处。)到目前为止我能找到的最好的是 this description of RestTemplate,其中说:
Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and
the various other templates found in the Spring Framework and other
portfolio projects. This means, for instance, that the RestTemplate is
thread-safe once constructed, and that you can use callbacks to
customize its operations.
我正在使用 JmsTemplate 发送和接收消息 to/from IBM MQ 队列。 我的应用程序安装在 WebSphere 应用程序服务器 8.5 上,为了检索连接,我使用了 jndi 查找。
我的Spring豆子:
<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jndiTest" />
<property name="lookupOnStartup" value="false" />
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.jms.QueueConnectionFactory" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="receiveTimeout" value="10000" />
<property name="sessionAcknowledgeMode" value="1" />
</bean>
<bean id="mqServerDao" class="MqServerDao" >
<constructor-arg name="jmsTemplate" ref="jmsTemplate" />
</bean>
我的Javaclass:
public class MqServerDao {
private JmsTemplate jmsTemplate;
public MqServerDao(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public String write(byte[] request, final String correlationId)
throws Exception {
MQQueue mqQueue = new MQQueue(MQ_INPUT_QUEUE);
mqQueue.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
MqRequestMessageCreator messageCreator = new MqRequestMessageCreator(
request, correlationId);
jmsTemplate.send(mqQueue, messageCreator);
return messageCreator.getMessageId();
}
public byte[] read(String messageId, String correlationId) throws Exception {
MQQueue mqQueue = new MQQueue(MQ_OUTPUT_QUEUE);
mqQueue.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
String messageSelector = "JMSCorrelationID = 'ID:" + correlationId
+ "' AND JMSMessageID = '" + messageId + "'";
TextMessage receiveMessage = (TextMessage) jmsTemplate.receiveSelected(
mqQueue, messageSelector);
return receiveMessage.getText().getBytes();
}
}
我想知道这样做是否正确,我有一些问题:
- 是否建议添加 CachingConnectionFactory 或管理 che 连接的应用程序服务器本身?
- 这是使用JmsTemplate的正确方法吗?同时调用两次MqServerDao的"write"方法安全吗?或者我会在 "write" 和 "read" 方法中创建一个新的 JmsTemplate 实例吗?
- WebSphere 将为您管理连接。来自 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html:
In a Java EE environment, make sure that the ConnectionFactory is obtained from the application's environment naming context via JNDI; application servers typically expose pooled, transaction-aware factories there.
- Spring 模板 类,包括
JmsTemplate
,旨在成为可重用的线程安全单例。你绝对不想继续创建新的。
我找了这方面的权威参考资料,但没有找到。 (您可能认为它会在上面的 JavaDoc link 中。IMO,Spring 的文档通常有一些不足之处。)到目前为止我能找到的最好的是 this description of RestTemplate,其中说:
Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and the various other templates found in the Spring Framework and other portfolio projects. This means, for instance, that the RestTemplate is thread-safe once constructed, and that you can use callbacks to customize its operations.