jmstemplate.java
来自「spring framework 2.5.4源代码」· Java 代码 · 共 1,048 行 · 第 1/3 页
JAVA
1,048 行
/*
* Copyright 2002-2008 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.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import org.springframework.jms.JmsException;
import org.springframework.jms.connection.ConnectionFactoryUtils;
import org.springframework.jms.connection.JmsResourceHolder;
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.JmsDestinationAccessor;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
/**
* Helper class that simplifies synchronous JMS access code.
*
* <p><b>NOTE:</b> This class requires a JMS 1.1+ provider because it builds
* on the domain-independent API. <b>Use the {@link JmsTemplate102} subclass
* for a JMS 1.0.2 provider, e.g. when running on a J2EE 1.3 server.</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.
* To configure them for native JMS usage, specify appropriate values for
* the "sessionTransacted" and "sessionAcknowledgeMode" bean properties.
*
* <p>This template uses a
* {@link org.springframework.jms.support.destination.DynamicDestinationResolver}
* and a {@link org.springframework.jms.support.converter.SimpleMessageConverter}
* as default strategies for resolving a destination name or converting a message,
* respectively. These defaults can be overridden through the "destinationResolver"
* and "messageConverter" bean properties.
*
* <p><b>NOTE: The <code>ConnectionFactory</code> used with this template should
* return pooled Connections (or a single shared Connection) as well as pooled
* Sessions and MessageProducers. Otherwise, performance of ad-hoc JMS operations
* is going to suffer.</b> The simplest option is to use the Spring-provided
* {@link org.springframework.jms.connection.SingleConnectionFactory} as a
* decorator for your target <code>ConnectionFactory</code>, reusing a single
* JMS Connection in a thread-safe fashion; this is often good enough for the
* purpose of sending messages via this template. In a J2EE environment,
* make sure that the <code>ConnectionFactory</code> is obtained from the
* application's environment naming context via JNDI; application servers
* typically expose pooled, transaction-aware factories there.
*
* @author Mark Pollack
* @author Juergen Hoeller
* @since 1.1
* @see #setConnectionFactory
* @see #setPubSubDomain
* @see #setDestinationResolver
* @see #setMessageConverter
* @see JmsTemplate102
* @see javax.jms.MessageProducer
* @see javax.jms.MessageConsumer
*/
public class JmsTemplate extends JmsDestinationAccessor implements JmsOperations {
/**
* Timeout value indicating that a receive operation should
* check if a message is immediately available without blocking.
*/
public static final long RECEIVE_TIMEOUT_NO_WAIT = -1;
/**
* Timeout value indicating a blocking receive without timeout.
*/
public static final long RECEIVE_TIMEOUT_INDEFINITE_WAIT = 0;
/** Internal ResourceFactory adapter for interacting with ConnectionFactoryUtils */
private final JmsTemplateResourceFactory transactionalResourceFactory = new JmsTemplateResourceFactory();
private Object defaultDestination;
private MessageConverter messageConverter;
private boolean messageIdEnabled = true;
private boolean messageTimestampEnabled = true;
private boolean pubSubNoLocal = false;
private long receiveTimeout = RECEIVE_TIMEOUT_INDEFINITE_WAIT;
private boolean explicitQosEnabled = false;
private int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
private int priority = Message.DEFAULT_PRIORITY;
private long timeToLive = Message.DEFAULT_TIME_TO_LIVE;
/**
* 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() {
setMessageConverter(new SimpleMessageConverter());
}
/**
* Set the destination to be used on send/receive operations that do not
* have a destination parameter.
* <p>Alternatively, specify a "defaultDestinationName", to be
* dynamically resolved via the DestinationResolver.
* @see #send(MessageCreator)
* @see #convertAndSend(Object)
* @see #convertAndSend(Object, MessagePostProcessor)
* @see #setDefaultDestinationName(String)
*/
public void setDefaultDestination(Destination destination) {
this.defaultDestination = destination;
}
/**
* Return the destination to be used on send/receive operations that do not
* have a destination parameter.
*/
public Destination getDefaultDestination() {
return (this.defaultDestination instanceof Destination ? (Destination) this.defaultDestination : null);
}
/**
* Set the destination name to be used on send/receive operations that
* do not have a destination parameter. The specified name will be
* dynamically resolved via the DestinationResolver.
* <p>Alternatively, specify a JMS Destination object as "defaultDestination".
* @see #send(MessageCreator)
* @see #convertAndSend(Object)
* @see #convertAndSend(Object, MessagePostProcessor)
* @see #setDestinationResolver
* @see #setDefaultDestination(javax.jms.Destination)
*/
public void setDefaultDestinationName(String destinationName) {
this.defaultDestination = destinationName;
}
/**
* Return the destination name to be used on send/receive operations that
* do not have a destination parameter.
*/
public String getDefaultDestinationName() {
return (this.defaultDestination instanceof String ? (String) this.defaultDestination : null);
}
/**
* 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 this.messageConverter;
}
/**
* Set whether message IDs are enabled. Default is "true".
* <p>This is only a hint to the JMS producer.
* See the JMS javadocs for details.
* @see javax.jms.MessageProducer#setDisableMessageID
*/
public void setMessageIdEnabled(boolean messageIdEnabled) {
this.messageIdEnabled = messageIdEnabled;
}
/**
* Return whether message IDs are enabled.
*/
public boolean isMessageIdEnabled() {
return this.messageIdEnabled;
}
/**
* Set whether message timestamps are enabled. Default is "true".
* <p>This is only a hint to the JMS producer.
* See the JMS javadocs for details.
* @see javax.jms.MessageProducer#setDisableMessageTimestamp
*/
public void setMessageTimestampEnabled(boolean messageTimestampEnabled) {
this.messageTimestampEnabled = messageTimestampEnabled;
}
/**
* Return whether message timestamps are enabled.
*/
public boolean isMessageTimestampEnabled() {
return this.messageTimestampEnabled;
}
/**
* Set whether to inhibit the delivery of messages published by its own connection.
* Default is "false".
* @see javax.jms.TopicSession#createSubscriber(javax.jms.Topic, String, boolean)
*/
public void setPubSubNoLocal(boolean pubSubNoLocal) {
this.pubSubNoLocal = pubSubNoLocal;
}
/**
* Return whether to inhibit the delivery of messages published by its own connection.
*/
public boolean isPubSubNoLocal() {
return this.pubSubNoLocal;
}
/**
* Set the timeout to use for receive calls (in milliseconds).
* <p>The default is {@link #RECEIVE_TIMEOUT_INDEFINITE_WAIT}, which indicates
* a blocking receive without timeout.
* <p>Specify {@link #RECEIVE_TIMEOUT_NO_WAIT} to inidicate that a receive operation
* should check if a message is immediately available without blocking.
* @see javax.jms.MessageConsumer#receive(long)
* @see javax.jms.MessageConsumer#receive()
* @see javax.jms.MessageConsumer#receiveNoWait()
*/
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
/**
* Return the timeout to use for receive calls (in milliseconds).
*/
public long getReceiveTimeout() {
return this.receiveTimeout;
}
/**
* Set if the QOS values (deliveryMode, priority, timeToLive)
* should be used for sending a message.
* @see #setDeliveryMode
* @see #setPriority
* @see #setTimeToLive
*/
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 this.explicitQosEnabled;
}
/**
* Set whether message delivery should be persistent or non-persistent,
* specified as boolean value ("true" or "false"). This will set the delivery
* mode accordingly, to either "PERSISTENT" (1) or "NON_PERSISTENT" (2).
* <p>Default it "true" aka delivery mode "PERSISTENT".
* @see #setDeliveryMode(int)
* @see javax.jms.DeliveryMode#PERSISTENT
* @see javax.jms.DeliveryMode#NON_PERSISTENT
*/
public void setDeliveryPersistent(boolean deliveryPersistent) {
this.deliveryMode = (deliveryPersistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
}
/**
* Set the delivery mode to use when sending a message.
* Default is the Message default: "PERSISTENT".
* <p>Since a default value may be defined administratively,
* this is only used when "isExplicitQosEnabled" equals "true".
* @param deliveryMode the delivery mode to use
* @see #isExplicitQosEnabled
* @see javax.jms.DeliveryMode#PERSISTENT
* @see javax.jms.DeliveryMode#NON_PERSISTENT
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?