@Bean @Scope(BeanDefinition.SCOPE_PROTOTYPE) public TestListener testListener(int count) { return new TestListener(count); } }
public Object getId() { if (this.message == null || this.getMessageProperties() == null) { throw new IllegalStateException("No MessageProperties received"); } return this.message.getMessageProperties().getMessageId(); }
@Test public void testAppender() throws InterruptedException { TestListener testListener = (TestListener) applicationContext.getBean("testListener", 4); listenerContainer.setMessageListener(testListener); listenerContainer.start(); log.debug("This is a DEBUG message"); log.info("This is an INFO message"); log.warn("This is a WARN message"); log.error("This is an ERROR message", new RuntimeException("Test exception")); assertTrue(testListener.getLatch().await(5, TimeUnit.SECONDS)); assertNotNull(testListener.getId()); }
@Test public void testCharset() throws InterruptedException { TestListener testListener = (TestListener) applicationContext.getBean("testListener", 1); listenerContainer.setMessageListener(testListener); listenerContainer.start(); String foo = "\u0fff"; // UTF-8 -> 0xe0bfbf log.info(foo); assertTrue(testListener.getLatch().await(5, TimeUnit.SECONDS)); byte[] body = testListener.getMessage().getBody(); int lineSeparatorExtraBytes = System.getProperty("line.separator").getBytes().length - 1; assertEquals(0xe0, body[body.length - 5 - lineSeparatorExtraBytes] & 0xff); assertEquals(0xbf, body[body.length - 4 - lineSeparatorExtraBytes] & 0xff); assertEquals(0xbf, body[body.length - 3 - lineSeparatorExtraBytes] & 0xff); }
@Test public void testAppenderWithProps() throws InterruptedException { TestListener testListener = (TestListener) applicationContext.getBean("testListener", 4); listenerContainer.setMessageListener(testListener); listenerContainer.start(); String propertyName = "someproperty"; String propertyValue = "property.value"; MDC.put(propertyName, propertyValue); log.debug("This is a DEBUG message with properties"); log.info("This is an INFO message with properties"); log.warn("This is a WARN message with properties"); log.error("This is an ERROR message with properties", new RuntimeException("Test exception")); MDC.remove(propertyName); assertTrue(testListener.getLatch().await(5, TimeUnit.SECONDS)); MessageProperties messageProperties = testListener.getMessageProperties(); assertNotNull(messageProperties); assertNotNull(messageProperties.getHeaders().get(propertyName)); assertEquals(propertyValue, messageProperties.getHeaders().get(propertyName)); Object location = messageProperties.getHeaders().get("location"); assertNotNull(location); assertThat(location, instanceOf(String.class)); assertThat((String) location, startsWith("org.springframework.amqp.rabbit.logback.AmqpAppenderIntegrationTests.testAppenderWithProps()")); Object threadName = messageProperties.getHeaders().get("thread"); assertNotNull(threadName); assertThat(threadName, instanceOf(String.class)); assertThat(threadName, is(Thread.currentThread().getName())); assertEquals("bar", messageProperties.getHeaders().get("foo")); }