Reuse QosSettings in JmsTemplate

See SPR-15408
This commit is contained in:
Stephane Nicoll 2017-04-18 13:32:53 +02:00
parent a2acbee004
commit 4ffdb50681
2 changed files with 25 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@ -34,6 +34,7 @@ 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.QosSettings;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.JmsDestinationAccessor;
@ -364,6 +365,23 @@ public class JmsTemplate extends JmsDestinationAccessor implements JmsOperations
return this.explicitQosEnabled;
}
/**
* Set the {@link QosSettings} to use when sending a message.
* @param settings the deliveryMode, priority, and timeToLive settings to use
* @see #setExplicitQosEnabled(boolean)
* @see #setDeliveryMode(int)
* @see #setPriority(int)
* @see #setTimeToLive(long)
* @since 5.0
*/
public void setQosSettings(QosSettings settings) {
Assert.notNull(settings, "Settings must not be null");
setExplicitQosEnabled(true);
setDeliveryMode(settings.getDeliveryMode());
setPriority(settings.getPriority());
setTimeToLive(settings.getTimeToLive());
}
/**
* Set whether message delivery should be persistent or non-persistent,
* specified as boolean value ("true" or "false"). This will set the delivery

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 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.
@ -53,6 +53,7 @@ import org.springframework.jms.connection.ConnectionFactoryUtils;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy;
import org.springframework.jms.support.JmsUtils;
import org.springframework.jms.support.QosSettings;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.JndiDestinationResolver;
import org.springframework.jndi.JndiTemplate;
@ -81,12 +82,7 @@ public class JmsTemplateTests {
private Destination queue;
private int deliveryMode = DeliveryMode.PERSISTENT;
private int priority = 9;
private int timeToLive = 10000;
private QosSettings qosSettings = new QosSettings(DeliveryMode.PERSISTENT, 9, 10000);
/**
* Create the mock objects for testing.
@ -373,10 +369,7 @@ public class JmsTemplateTests {
given(session.createTextMessage("just testing")).willReturn(textMessage);
if (!ignoreQOS) {
template.setExplicitQosEnabled(true);
template.setDeliveryMode(deliveryMode);
template.setPriority(priority);
template.setTimeToLive(timeToLive);
template.setQosSettings(qosSettings);
}
if (useDefaultDestination) {
@ -419,7 +412,8 @@ public class JmsTemplateTests {
verify(messageProducer).send(textMessage);
}
else {
verify(messageProducer).send(textMessage, deliveryMode, priority, timeToLive);
verify(messageProducer).send(textMessage, qosSettings.getDeliveryMode(),
qosSettings.getPriority(), qosSettings.getTimeToLive());
}
verify(messageProducer).close();
verify(session).close();