Add integration test for TTL, plus extra constructors for connection factories

This commit is contained in:
Dave Syer
2011-03-31 17:14:55 +01:00
parent 128a45de7f
commit 1a01f96a78
4 changed files with 114 additions and 0 deletions

View File

@@ -71,6 +71,24 @@ public class CachingConnectionFactory extends SingleConnectionFactory implements
super();
}
/**
* Create a new CachingConnectionFactory given a host name.
*
* @param hostName the host name to connect to
*/
public CachingConnectionFactory(String hostName, int port) {
super(hostName, port);
}
/**
* Create a new CachingConnectionFactory given a host name.
*
* @param hostName the host name to connect to
*/
public CachingConnectionFactory(int port) {
super(port);
}
/**
* Create a new CachingConnectionFactory given a host name.
*

View File

@@ -60,16 +60,34 @@ public class SingleConnectionFactory implements ConnectionFactory, DisposableBea
this((String) null);
}
/**
* Create a new SingleConnectionFactory given a host name.
* @param port the port to connect to
*/
public SingleConnectionFactory(int port) {
this(null, port);
}
/**
* Create a new SingleConnectionFactory given a host name.
* @param hostname the host name to connect to
*/
public SingleConnectionFactory(String hostname) {
this(hostname, com.rabbitmq.client.ConnectionFactory.DEFAULT_AMQP_PORT);
}
/**
* Create a new SingleConnectionFactory given a host name.
* @param hostname the host name to connect to
* @param port the port number to connect to
*/
public SingleConnectionFactory(String hostname, int port) {
if (!StringUtils.hasText(hostname)) {
hostname = getDefaultHostName();
}
this.rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory();
this.rabbitConnectionFactory.setHost(hostname);
this.rabbitConnectionFactory.setPort(port);
}
/**

View File

@@ -0,0 +1,63 @@
/*
* 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.amqp.rabbit.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.test.BrokerRunning;
import org.springframework.amqp.rabbit.test.BrokerTestUtils;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public final class QueueParserIntegrationTests {
@Rule
public BrokerRunning brokerIsRunning = BrokerRunning.isRunning();
private XmlBeanFactory beanFactory;
@Before
public void setUpDefaultBeanFactory() throws Exception {
beanFactory = new XmlBeanFactory(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass()));
}
@Test
public void testArgumentsQueue() throws Exception {
Queue queue = beanFactory.getBean("arguments", Queue.class);
assertNotNull(queue);
RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory(BrokerTestUtils.getPort()));
RabbitAdmin rabbitAdmin = new RabbitAdmin(template.getConnectionFactory());
rabbitAdmin.deleteQueue(queue.getName());
rabbitAdmin.declareQueue(queue);
assertEquals(100L, queue.getArguments().get("x-message-ttl"));
template.convertAndSend(queue.getName(), "message");
Thread.sleep(200);
String result = (String) template.receiveAndConvert(queue.getName());
assertEquals(null, result);
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:queue name="foo" />
<rabbit:queue name="arguments">
<rabbit:queue-arguments value-type="java.lang.Long">
<beans:entry key="x-message-ttl" value="100" />
</rabbit:queue-arguments>
</rabbit:queue>
</beans>