@Test public void registerContainerWithoutFactory() throws Exception { SimpleRabbitListenerEndpoint endpoint = new SimpleRabbitListenerEndpoint(); endpoint.setId("myEndpoint"); registrar.setContainerFactory(containerFactory); registrar.registerEndpoint(endpoint); registrar.afterPropertiesSet(); assertNotNull("Container not created", registry.getListenerContainer("myEndpoint")); assertEquals("myEndpoint", registry.getListenerContainerIds().iterator().next()); assertEquals(1, registry.getListenerContainers().size()); }
@Test public void testDifferentTypes() throws InterruptedException { Foo1 foo = new Foo1(); foo.setBar("bar"); this.jsonRabbitTemplate.convertAndSend("differentTypes", foo); assertTrue(this.service.latch.await(10, TimeUnit.SECONDS)); assertThat(this.service.foos.get(0), instanceOf(Foo2.class)); assertEquals("bar", ((Foo2) this.service.foos.get(0)).getBar()); assertThat(TestUtils.getPropertyValue(this.registry.getListenerContainer("different"), "concurrentConsumers"), equalTo(2)); }
@Test public void registerNullContainerFactoryIsAllowed() throws Exception { SimpleRabbitListenerEndpoint endpoint = new SimpleRabbitListenerEndpoint(); endpoint.setId("some id"); registrar.setContainerFactory(containerFactory); registrar.registerEndpoint(endpoint, null); registrar.afterPropertiesSet(); assertNotNull("Container not created", registry.getListenerContainer("some id")); assertEquals("some id", registry.getListenerContainerIds().iterator().next()); assertEquals(1, registry.getListenerContainers().size()); }
@Test public void simpleDirectEndpointWithConcurrency() { String reply = (String) rabbitTemplate.convertSendAndReceive("test.simple.direct2", "foo"); assertThat(reply, startsWith("FOOfoo")); assertThat(reply, containsString("rabbitClientThread-")); // container runs on client thread assertThat(TestUtils.getPropertyValue(this.registry.getListenerContainer("directWithConcurrency"), "consumersPerQueue"), equalTo(3)); }
/** * Test for {@link CustomBean} and an manually endpoint registered * with "myCustomEndpointId". The custom endpoint does not provide * any factory so it's registered with the default one */ public void testCustomConfiguration(ApplicationContext context) { RabbitListenerContainerTestFactory defaultFactory = context.getBean("rabbitListenerContainerFactory", RabbitListenerContainerTestFactory.class); RabbitListenerContainerTestFactory customFactory = context.getBean("customFactory", RabbitListenerContainerTestFactory.class); assertEquals(1, defaultFactory.getListenerContainers().size()); assertEquals(1, customFactory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint(); assertEquals("Wrong endpoint type", SimpleRabbitListenerEndpoint.class, endpoint.getClass()); assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"), ((SimpleRabbitListenerEndpoint) endpoint).getMessageListener()); RabbitListenerEndpointRegistry customRegistry = context.getBean("customRegistry", RabbitListenerEndpointRegistry.class); assertEquals("Wrong number of containers in the registry", 2, customRegistry.getListenerContainerIds().size()); assertEquals("Wrong number of containers in the registry", 2, customRegistry.getListenerContainers().size()); assertNotNull("Container with custom id on the annotation should be found", customRegistry.getListenerContainer("listenerId")); assertNotNull("Container created with custom id should be found", customRegistry.getListenerContainer("myCustomEndpointId")); }
@Test public void testDifferentTypesWithVariableConcurrency() throws InterruptedException { Foo1 foo = new Foo1(); foo.setBar("bar"); this.jsonRabbitTemplate.convertAndSend("differentTypes", foo); assertTrue(this.service.latch.await(10, TimeUnit.SECONDS)); assertThat(this.service.foos.get(0), instanceOf(Foo2.class)); assertEquals("bar", ((Foo2) this.service.foos.get(0)).getBar()); MessageListenerContainer container = this.registry.getListenerContainer("differentWithVariableConcurrency"); assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers"), equalTo(3)); assertThat(TestUtils.getPropertyValue(container, "maxConcurrentConsumers"), equalTo(4)); }
@Test public void testDifferentTypesWithConcurrency() throws InterruptedException { Foo1 foo = new Foo1(); foo.setBar("bar"); this.jsonRabbitTemplate.convertAndSend("differentTypes", foo); assertTrue(this.service.latch.await(10, TimeUnit.SECONDS)); assertThat(this.service.foos.get(0), instanceOf(Foo2.class)); assertEquals("bar", ((Foo2) this.service.foos.get(0)).getBar()); MessageListenerContainer container = this.registry.getListenerContainer("differentWithConcurrency"); assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers"), equalTo(3)); assertNull(TestUtils.getPropertyValue(container, "maxConcurrentConsumers")); }
@Test public void simpleDirectEndpoint() { MessageListenerContainer container = this.registry.getListenerContainer("direct"); assertFalse(container.isRunning()); container.start(); String reply = (String) rabbitTemplate.convertSendAndReceive("test.simple.direct", "foo"); assertThat(reply, startsWith("FOOfoo")); assertThat(reply, containsString("rabbitClientThread-")); // container runs on client thread assertThat(TestUtils.getPropertyValue(container, "consumersPerQueue"), equalTo(2)); }
@Test public void autoSimpleDeclareAnonymousQueue() { final SimpleMessageListenerContainer container = (SimpleMessageListenerContainer) registry .getListenerContainer("anonymousQueue575"); assertThat(container.getQueueNames(), arrayWithSize(1)); assertEquals("viaAnonymous:foo", rabbitTemplate.convertSendAndReceive(container.getQueueNames()[0], "foo")); Object messageListener = container.getMessageListener(); assertThat(TestUtils.getPropertyValue(messageListener, "retryTemplate"), notNullValue()); assertThat(TestUtils.getPropertyValue(messageListener, "recoveryCallback"), notNullValue()); }
@Test public void autoStart() { MessageListenerContainer listenerContainer = this.registry.getListenerContainer("notStarted"); assertNotNull(listenerContainer); assertFalse(listenerContainer.isRunning()); this.registry.start(); assertTrue(listenerContainer.isRunning()); listenerContainer.stop(); }
@Test public void multiListenerJson() { Bar bar = new Bar(); bar.field = "bar"; String exchange = "multi.json.exch"; String routingKey = "multi.json.rk"; assertEquals("BAR: barMultiListenerJsonBean", this.jsonRabbitTemplate.convertSendAndReceive(exchange, routingKey, bar)); Baz baz = new Baz(); baz.field = "baz"; assertEquals("BAZ: baz", this.jsonRabbitTemplate.convertSendAndReceive(exchange, routingKey, baz)); Qux qux = new Qux(); qux.field = "qux"; assertEquals("QUX: qux: multi.json.rk", this.jsonRabbitTemplate.convertSendAndReceive(exchange, routingKey, qux)); // SpEL replyTo this.jsonRabbitTemplate.convertAndSend(exchange, routingKey, bar); this.jsonRabbitTemplate.setReceiveTimeout(10000); assertEquals("BAR: barMultiListenerJsonBean", this.jsonRabbitTemplate.receiveAndConvert("sendTo.replies.spel")); MessageListenerContainer container = this.registry.getListenerContainer("multi"); assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers"), equalTo(1)); assertThat(TestUtils.getPropertyValue(container, "messageListener.errorHandler"), notNullValue()); assertTrue(TestUtils.getPropertyValue(container, "messageListener.returnExceptions", Boolean.class)); }
@Test public void testIdle() throws Exception { assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue.getName(), "foo")); assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue.getName(), "foo")); assertTrue(this.listener.latch.await(10, TimeUnit.SECONDS)); assertEquals("foo", this.listener.event.getListenerId()); assertEquals(this.queue.getName(), this.listener.event.getQueueNames()[0]); assertEquals("BAR", this.rabbitTemplate.convertSendAndReceive(this.queue.getName(), "bar")); assertEquals("BAR", this.rabbitTemplate.convertSendAndReceive(this.queue.getName(), "bar")); assertFalse(this.listener.barEventReceived); MessageListenerContainer listenerContainer = registry.getListenerContainer("foo"); assertTrue(TestUtils.getPropertyValue(listenerContainer, "mismatchedQueuesFatal", Boolean.class)); }