📄 jmstemplate.java
字号:
}
else {
con = createConnection();
if (startConnection) {
con.start();
}
session = createSession(con);
conToUse = con;
sessionToUse = session;
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JMS Session [" + sessionToUse +
"] from connection [" + conToUse + "]");
}
return action.doInJms(sessionToUse);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
finally {
JmsUtils.closeSession(session);
JmsUtils.closeConnection(con);
}
}
public Object execute(SessionCallback action) throws JmsException {
return execute(action, false);
}
public Object execute(final ProducerCallback action) throws JmsException {
return execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
MessageProducer producer = createProducer(session, null);
try {
return action.doInJms(session, producer);
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}
}, false);
}
//-------------------------------------------------------------------------
// Convenience methods for sending messages
//-------------------------------------------------------------------------
public void send(MessageCreator messageCreator) throws JmsException {
checkDefaultDestination();
if (getDefaultDestination() != null) {
send(getDefaultDestination(), messageCreator);
}
else {
send(getDefaultDestinationName(), messageCreator);
}
}
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException {
execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
doSend(session, destination, messageCreator);
return null;
}
}, false);
}
public void send(final String destinationName, final MessageCreator messageCreator) throws JmsException {
execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
Destination destination = resolveDestinationName(session, destinationName);
doSend(session, destination, messageCreator);
return null;
}
}, false);
}
protected void doSend(Session session, Destination destination, MessageCreator messageCreator)
throws JMSException {
MessageProducer producer = createProducer(session, destination);
try {
Message message = messageCreator.createMessage(session);
if (logger.isDebugEnabled()) {
logger.debug("Sending created message [" + message + "]");
}
doSend(producer, message);
// Check commit - avoid commit call within a JTA transaction.
if (session.getTransacted() && isSessionTransacted() &&
!TransactionSynchronizationManager.hasResource(getConnectionFactory())) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (isExplicitQosEnabled()) {
producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
}
else {
producer.send(message);
}
}
//-------------------------------------------------------------------------
// Convenience methods for sending auto-converted messages
//-------------------------------------------------------------------------
public void convertAndSend(Object message) throws JmsException {
checkDefaultDestination();
if (getDefaultDestination() != null) {
convertAndSend(getDefaultDestination(), message);
}
else {
convertAndSend(getDefaultDestinationName(), message);
}
}
public void convertAndSend(Destination destination, final Object message) throws JmsException {
checkMessageConverter();
send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return getMessageConverter().toMessage(message, session);
}
});
}
public void convertAndSend(String destinationName, final Object message) throws JmsException {
checkMessageConverter();
send(destinationName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return getMessageConverter().toMessage(message, session);
}
});
}
public void convertAndSend(Object message, MessagePostProcessor postProcessor) throws JmsException {
checkDefaultDestination();
if (getDefaultDestination() != null) {
convertAndSend(getDefaultDestination(), message, postProcessor);
}
else {
convertAndSend(getDefaultDestinationName(), message, postProcessor);
}
}
public void convertAndSend(
Destination destination, final Object message, final MessagePostProcessor postProcessor)
throws JmsException {
checkMessageConverter();
send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message msg = getMessageConverter().toMessage(message, session);
return postProcessor.postProcessMessage(msg);
}
});
}
public void convertAndSend(
String destinationName, final Object message, final MessagePostProcessor postProcessor)
throws JmsException {
checkMessageConverter();
send(destinationName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message msg = getMessageConverter().toMessage(message, session);
return postProcessor.postProcessMessage(msg);
}
});
}
//-------------------------------------------------------------------------
// Convenience methods for receiving messages
//-------------------------------------------------------------------------
public Message receive() throws JmsException {
checkDefaultDestination();
if (getDefaultDestination() != null) {
return receive(getDefaultDestination());
}
else {
return receive(getDefaultDestinationName());
}
}
public Message receive(final Destination destination) throws JmsException {
return (Message) execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
return doReceive(session, destination, null);
}
}, true);
}
public Message receive(final String destinationName) throws JmsException {
return (Message) execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
Destination destination = resolveDestinationName(session, destinationName);
return doReceive(session, destination, null);
}
}, true);
}
public Message receiveSelected(String messageSelector) throws JmsException {
checkDefaultDestination();
if (getDefaultDestination() != null) {
return receiveSelected(getDefaultDestination(), messageSelector);
}
else {
return receiveSelected(getDefaultDestinationName(), messageSelector);
}
}
public Message receiveSelected(final Destination destination, final String messageSelector) throws JmsException {
return (Message) execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
return doReceive(session, destination, messageSelector);
}
}, true);
}
public Message receiveSelected(final String destinationName, final String messageSelector) throws JmsException {
return (Message) execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
Destination destination = resolveDestinationName(session, destinationName);
return doReceive(session, destination, messageSelector);
}
}, true);
}
protected Message doReceive(Session session, Destination destination, String messageSelector)
throws JMSException {
return doReceive(session, createConsumer(session, destination, messageSelector));
}
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
try {
// Use transaction timeout (if available).
long timeout = getReceiveTimeout();
ConnectionHolder conHolder =
(ConnectionHolder) TransactionSynchronizationManager.getResource(getConnectionFactory());
if (conHolder != null && conHolder.hasTimeout()) {
timeout = conHolder.getTimeToLiveInMillis();
}
Message message = (timeout >= 0) ?
consumer.receive(timeout) : consumer.receive();
if (session.getTransacted()) {
// Commit necessary - but avoid commit call within a JTA transaction.
if (isSessionTransacted() && conHolder == null) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
}
else if (isClientAcknowledge(session)) {
// Manually acknowledge message, if any.
if (message != null) {
message.acknowledge();
}
}
return message;
}
finally {
JmsUtils.closeMessageConsumer(consumer);
}
}
/**
* Return whether the Session is in client acknowledge mode.
* <p>This implementation uses JMS 1.1 API.
* @param session the JMS Session to check
* @throws JMSException if thrown by JMS API methods
*/
protected boolean isClientAcknowledge(Session session) throws JMSException {
return (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
}
//-------------------------------------------------------------------------
// Convenience methods for receiving auto-converted messages
//-------------------------------------------------------------------------
public Object receiveAndConvert() throws JmsException {
checkMessageConverter();
return doConvertFromMessage(receive());
}
public Object receiveAndConvert(Destination destination) throws JmsException {
checkMessageConverter();
return doConvertFromMessage(receive(destination));
}
public Object receiveAndConvert(String destinationName) throws JmsException {
checkMessageConverter();
return doConvertFromMessage(receive(destinationName));
}
public Object receiveSelectedAndConvert(String messageSelector) throws JmsException {
checkMessageConverter();
return doConvertFromMessage(receiveSelected(messageSelector));
}
public Object receiveSelectedAndConvert(Destination destination, String messageSelector) throws JmsException {
checkMessageConverter();
return doConvertFromMessage(receiveSelected(destination, messageSelector));
}
public Object receiveSelectedAndConvert(String destinationName, String messageSelector) throws JmsException {
checkMessageConverter();
return doConvertFromMessage(receiveSelected(destinationName, messageSelector));
}
protected Object doConvertFromMessage(Message message) {
if (message != null) {
try {
return getMessageConverter().fromMessage(message);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
}
return null;
}
//-------------------------------------------------------------------------
// JMS 1.1 factory methods, potentially overridden for JMS 1.0.2
//-------------------------------------------------------------------------
/**
* Create a JMS Connection via this template's ConnectionFactory.
* <p>This implementation uses JMS 1.1 API.
* @return the new JMS Connection
* @throws JMSException if thrown by JMS API methods
*/
protected Connection createConnection() throws JMSException {
return getConnectionFactory().createConnection();
}
/**
* Create a JMS Session for the given Connection.
* <p>This implementation uses JMS 1.1 API.
* @param con the JMS Connection to create a Session for
* @return the new JMS Session
* @throws JMSException if thrown by JMS API methods
*/
protected Session createSession(Connection con) throws JMSException {
return con.createSession(isSessionTransacted(), getSessionAcknowledgeMode());
}
/**
* Create a JMS MessageProducer for the given Session and Destination,
* configuring it to disable message ids and/or timestamps (if necessary).
* <p>Delegates to <code>doCreateProducer</code> for creation of the raw
* JMS MessageProducer, which needs to be specific to JMS 1.1 or 1.0.2.
* @param session the JMS Session to create a MessageProducer for
* @param destination the JMS Destination to create a MessageProducer for
* @return the new JMS MessageProducer
* @throws JMSException if thrown by JMS API methods
* @see #doCreateProducer
* @see #setMessageIdEnabled
* @see #setMessageTimestampEnabled
*/
protected MessageProducer createProducer(Session session, Destination destination) throws JMSException {
MessageProducer producer = doCreateProducer(session, destination);
if (!isMessageIdEnabled()) {
producer.setDisableMessageID(true);
}
if (!isMessageTimestampEnabled()) {
producer.setDisableMessageTimestamp(true);
}
return producer;
}
/**
* Create a raw JMS MessageProducer for the given Session and Destination.
* <p>This implementation uses JMS 1.1 API.
* @param session the JMS Session to create a MessageProducer for
* @param destination the JMS Destination to create a MessageProducer for
* @return the new JMS MessageProducer
* @throws JMSException if thrown by JMS API methods
*/
protected MessageProducer doCreateProducer(Session session, Destination destination) throws JMSException {
return session.createProducer(destination);
}
/**
* Create a JMS MessageConsumer for the given Session and Destination.
* <p>This implementation uses JMS 1.1 API.
* @param session the JMS Session to create a MessageConsumer for
* @param destination the JMS Destination to create a MessageConsumer for
* @param messageSelector the message selector for this consumer (can be <code>null</code>)
* @return the new JMS MessageConsumer
* @throws JMSException if thrown by JMS API methods
*/
protected MessageConsumer createConsumer(Session session, Destination destination, String messageSelector)
throws JMSException {
// Only pass in the NoLocal flag in case of a Topic:
// Some JMS providers, such as WebSphere MQ 6.0, throw IllegalStateException
// in case of the NoLocal flag being specified for a Queue.
if (destination instanceof Topic) {
return session.createConsumer(destination, messageSelector, isPubSubNoLocal());
}
else {
return session.createConsumer(destination, messageSelector);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -