ConnectionFactory factory = getTemplateConnectionFactory();
// I whish the spring templates had built in support for preserving the
// message
// qos when doing a send. :(
JmsTemplate template = useVersion102 ? new JmsTemplate102(factory, pubSubDomain) {
/**
* Override so we can support preserving the Qos settings that have
* been set on the message.
*/
@Override
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (preserveMessageQos) {
long ttl = message.getJMSExpiration();
if (ttl != 0) {
ttl = ttl - System.currentTimeMillis();
// Message had expired.. so set the ttl as small as
// possible
if (ttl <= 0) {
ttl = 1;
}
}
if (isPubSubDomain()) {
((TopicPublisher)producer).publish(message, message.getJMSDeliveryMode(), message
.getJMSPriority(), ttl);
} else {
((QueueSender)producer).send(message, message.getJMSDeliveryMode(), message
.getJMSPriority(), ttl);
}
} else {
super.doSend(producer, message);
}
}
} : new JmsTemplate(factory) {
/**
* Override so we can support preserving the Qos settings that have
* been set on the message.
*/
@Override
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (preserveMessageQos) {
long ttl = message.getJMSExpiration();
if (ttl != 0) {
ttl = ttl - System.currentTimeMillis();
// Message had expired.. so set the ttl as small as
// possible
if (ttl <= 0) {
ttl = 1;
}
}
producer.send(message, message.getJMSDeliveryMode(), message.getJMSPriority(), ttl);
} else {
super.doSend(producer, message);
}
}
};
template.setPubSubDomain(pubSubDomain);
if (destinationResolver != null) {
template.setDestinationResolver(destinationResolver);
}
template.setDefaultDestinationName(destination);
template.setExplicitQosEnabled(explicitQosEnabled);
template.setDeliveryPersistent(deliveryPersistent);
if (messageConverter != null) {
template.setMessageConverter(messageConverter);
}
template.setMessageIdEnabled(messageIdEnabled);
template.setMessageTimestampEnabled(messageTimestampEnabled);
if (priority >= 0) {
template.setPriority(priority);
}
template.setPubSubNoLocal(pubSubNoLocal);
if (receiveTimeout >= 0) {
template.setReceiveTimeout(receiveTimeout);
}
if (timeToLive >= 0) {
template.setTimeToLive(timeToLive);
}
template.setSessionTransacted(transacted);
if (transacted) {
template.setSessionAcknowledgeMode(Session.SESSION_TRANSACTED);
} else {
// This is here for completeness, but the template should not get
// used
// for receiving messages.
if (acknowledgementMode >= 0) {
template.setSessionAcknowledgeMode(acknowledgementMode);
} else if (acknowledgementModeName != null) {
template.setSessionAcknowledgeModeName(acknowledgementModeName);
}
}
return template;
}