📄 jmstemplate.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.core;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.Constants;
import org.springframework.jms.JmsException;
import org.springframework.jms.connection.ConnectionHolder;
import org.springframework.jms.support.JmsUtils;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.DynamicDestinationResolver;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* Helper class that simplifies JMS access code. This class requires a
* JMS 1.1 provider, because it builds on the new domain-independent API.
* <b>Use the {@link JmsTemplate102 JmsTemplate102} subclass for
* JMS 1.0.2 providers.</b>
*
* <p>If you want to use dynamic destination creation, you must specify
* the type of JMS destination to create, using the "pubSubDomain" property.
* For other operations, this is not necessary, in contrast to when working
* with JmsTemplate102. Point-to-Point (Queues) is the default domain.
*
* <p>Default settings for JMS sessions are not transacted and auto-acknowledge.
* As defined by the J2EE specification, the transaction and acknowledgement
* parameters are ignored when a JMS Session is created inside an active
* transaction, no matter if a JTA transaction or a Spring-managed transaction.
*
* <p>This template uses a DynamicDestinationResolver and a SimpleMessageConverter
* as default strategies for resolving a destination name respectively converting
* a message.
*
* @author Mark Pollack
* @author Juergen Hoeller
* @since 1.1
* @see #setConnectionFactory
* @see #setPubSubDomain
* @see JmsTemplate102
* @see #setDestinationResolver
* @see #setMessageConverter
* @see org.springframework.jms.support.destination.DynamicDestinationResolver
* @see org.springframework.jms.support.converter.SimpleMessageConverter
*/
public class JmsTemplate implements JmsOperations, InitializingBean {
/**
* Default timeout for receive operations:
* -1 indicates a blocking receive without timeout.
*/
public static final long DEFAULT_RECEIVE_TIMEOUT = -1;
/** Constants instance for javax.jms.Session */
private static final Constants constants = new Constants(Session.class);
protected final Log logger = LogFactory.getLog(getClass());
/**
* Used to obtain JMS connections.
*/
private ConnectionFactory connectionFactory;
/**
* By default uses the Point-to-Point domain.
*/
private boolean pubSubDomain = false;
/**
* Default transaction mode for a JMS Session.
*/
private boolean sessionTransacted = false;
/**
* Default ack mode for a JMS Session.
*/
private int sessionAcknowledgeMode = Session.AUTO_ACKNOWLEDGE;
/**
* The default destination to use on send operations that do not specify an explicit destination.
*/
private Destination defaultDestination;
/**
* Delegate management of JNDI lookups and dynamic destination creation
* to a DestinationResolver implementation.
*/
private DestinationResolver destinationResolver;
/**
* The messageConverter to use for send(object) methods.
*/
private MessageConverter messageConverter;
/**
* The timeout to use for receive operations.
*/
private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;
/**
* Use the default or explicit QOS parameters.
*/
private boolean explicitQosEnabled;
/**
* The delivery mode to use when sending a message. Only used if isExplicitQosEnabled = true.
*/
private int deliveryMode;
/**
* The priority of the message. Only used if isExplicitQosEnabled = true.
*/
private int priority;
/**
* The message's lifetime in milliseconds. Only used if isExplicitQosEnabled = true.
*/
private long timeToLive;
/**
* Create a new JmsTemplate for bean-style usage.
* <p>Note: The ConnectionFactory has to be set before using the instance.
* This constructor can be used to prepare a JmsTemplate via a BeanFactory,
* typically setting the ConnectionFactory via setConnectionFactory.
* @see #setConnectionFactory
*/
public JmsTemplate() {
initDefaultStrategies();
}
/**
* Create a new JmsTemplate, given a ConnectionFactory.
* @param connectionFactory the ConnectionFactory to obtain connections from
*/
public JmsTemplate(ConnectionFactory connectionFactory) {
this();
setConnectionFactory(connectionFactory);
afterPropertiesSet();
}
/**
* Initialize the default implementations for the template's strategies:
* DynamicDestinationResolver and SimpleMessageConverter.
* @see #setDestinationResolver
* @see #setMessageConverter
* @see org.springframework.jms.support.destination.DynamicDestinationResolver
* @see org.springframework.jms.support.converter.SimpleMessageConverter
*/
protected void initDefaultStrategies() {
setDestinationResolver(new DynamicDestinationResolver());
setMessageConverter(new SimpleMessageConverter());
}
/**
* Set the connection factory used for obtaining JMS connections.
*/
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
/**
* Return the connection factory used for obtaining JMS connections.
*/
public ConnectionFactory getConnectionFactory() {
return connectionFactory;
}
/**
* Configure the JmsTemplate with knowledge of the JMS domain used.
* Default is Point-to-Point (Queues).
* <p>For JmsTemplate102, this tells the JMS provider which class hierarchy to use
* in the implementation of the various execute methods. For JmsTemplate itself,
* it does not affect execute methods. In both implementations, it tells what type
* of destination to create if dynamic destinations are enabled.
* @param pubSubDomain true for Publish/Subscribe domain (Topics),
* false for Point-to-Point domain (Queues)
* @see #setDestinationResolver
*/
public void setPubSubDomain(boolean pubSubDomain) {
this.pubSubDomain = pubSubDomain;
}
/**
* Return whether the Publish/Subscribe domain (Topics) is used.
* Otherwise, the Point-to-Point domain (Queues) is used.
*/
public boolean isPubSubDomain() {
return pubSubDomain;
}
/**
* Set the transaction mode that is used when creating a JMS session to send a message.
* <p>Note that that within a JTA transaction, the parameters to
* create<Queue|Topic>Session(boolean transacted, int acknowledgeMode) method are not
* taken into account. Depending on the J2EE transaction context, the container
* makes its own decisions on these values. See section 17.3.5 of the EJB Spec.
* @param sessionTransacted the transaction mode
*/
public void setSessionTransacted(boolean sessionTransacted) {
this.sessionTransacted = sessionTransacted;
}
/**
* Return whether the JMS sessions used for sending a message are transacted.
*/
public boolean isSessionTransacted() {
return sessionTransacted;
}
/**
* Set the JMS acknowledgement mode by the name of the corresponding constant
* in the JMS Session interface, e.g. "CLIENT_ACKNOWLEDGE".
* @param constantName name of the constant
* @see javax.jms.Session#AUTO_ACKNOWLEDGE
* @see javax.jms.Session#CLIENT_ACKNOWLEDGE
* @see javax.jms.Session#DUPS_OK_ACKNOWLEDGE
*/
public void setSessionAcknowledgeModeName(String constantName) {
setSessionAcknowledgeMode(constants.asNumber(constantName).intValue());
}
/**
* Set the JMS acknowledgement mode that is used when creating a JMS session to send
* a message. Vendor extensions to the acknowledgment mode can be set here as well.
* <p>Note that that inside an EJB the parameters to
* create<Queue|Topic>Session(boolean transacted, int acknowledgeMode) method are not
* taken into account. Depending on the transaction context in the EJB, the container
* makes its own decisions on these values. See section 17.3.5 of the EJB Spec.
* @param sessionAcknowledgeMode the acknowledgement mode
*/
public void setSessionAcknowledgeMode(int sessionAcknowledgeMode) {
this.sessionAcknowledgeMode = sessionAcknowledgeMode;
}
/**
* Return the acknowledgement mode for JMS sessions.
*/
public int getSessionAcknowledgeMode() {
return sessionAcknowledgeMode;
}
/**
* Set the destination to be used on send operations that do not
* have a destination parameter.
* @see #send(MessageCreator)
* @see #convertAndSend(Object)
* @see #convertAndSend(Object, MessagePostProcessor)
*/
public void setDefaultDestination(Destination destination) {
this.defaultDestination = destination;
}
/**
* Return the destination to be used on send operations that do not
* have a destination parameter.
*/
public Destination getDefaultDestination() {
return defaultDestination;
}
/**
* Set the destination resolver for this template. Used to resolve
* destination names and to support dynamic destination functionality.
* <p>The default resolver is a DynamicDestinationResolver. Specify a
* JndiDestinationResolver for resolving destination names as JNDI locations.
* @see org.springframework.jms.support.destination.DynamicDestinationResolver
* @see org.springframework.jms.support.destination.JndiDestinationResolver
*/
public void setDestinationResolver(DestinationResolver destinationResolver) {
this.destinationResolver = destinationResolver;
}
/**
* Get the destination resolver for this template.
*/
public DestinationResolver getDestinationResolver() {
return destinationResolver;
}
/**
* Set the message converter for this template. Used to resolve
* Object parameters to convertAndSend methods and Object results
* from receiveAndConvert methods.
* <p>The default converter is a SimpleMessageConverter, which is able
* to handle BytesMessages, TextMessages and ObjectMessages.
* @see #convertAndSend
* @see #receiveAndConvert
* @see org.springframework.jms.support.converter.SimpleMessageConverter
*/
public void setMessageConverter(MessageConverter messageConverter) {
this.messageConverter = messageConverter;
}
/**
* Return the message converter for this template.
*/
public MessageConverter getMessageConverter() {
return messageConverter;
}
/**
* Set the timeout to use for receive calls.
* The default is -1, which means no timeout.
* @see javax.jms.MessageConsumer#receive(long)
* @see javax.jms.MessageConsumer#receive
*/
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
/**
* Return the timeout to use for receive calls.
*/
public long getReceiveTimeout() {
return receiveTimeout;
}
/**
* Set if the QOS values (deliveryMode, priority, timeToLive)
* should be used for sending a message.
*/
public void setExplicitQosEnabled(boolean explicitQosEnabled) {
this.explicitQosEnabled = explicitQosEnabled;
}
/**
* If true, then the values of deliveryMode, priority, and timeToLive
* will be used when sending a message. Otherwise, the default values,
* that may be set administratively, will be used.
* @return true if overriding default values of QOS parameters
* (deliveryMode, priority, and timeToLive)
* @see #setDeliveryMode
* @see #setPriority
* @see #setTimeToLive
*/
public boolean isExplicitQosEnabled() {
return explicitQosEnabled;
}
/**
* Set the delivery mode to use when sending a message. Since a default value may be
* defined administratively, it is only used when isExplicitQosEnabled equals true.
* @param deliveryMode the delivery mode to use
* @see #isExplicitQosEnabled
*/
public void setDeliveryMode(int deliveryMode) {
this.deliveryMode = deliveryMode;
}
/**
* Return the delivery mode to use when sending a message.
*/
public int getDeliveryMode() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -