diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j/AmqpAppender.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j/AmqpAppender.java index a8eb1b4f..006a1167 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j/AmqpAppender.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j/AmqpAppender.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). - * + * Copyright (c) 2011-2012 by the original author(s). + * * 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. @@ -17,6 +17,7 @@ import java.util.Calendar; import java.util.Map; import java.util.Timer; import java.util.TimerTask; +import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; @@ -86,6 +87,7 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; * #------------------------------- * log4j.appender.amqp.contentType=text/plain * log4j.appender.amqp.contentEncoding=null + * log4j.appender.amqp.generateId=false * #------------------------------- * ## Sender configuration * #------------------------------- @@ -101,6 +103,7 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; * * * @author Jon Brisbin + * @author Gary Russell */ public class AmqpAppender extends AppenderSkeleton { @@ -208,6 +211,11 @@ public class AmqpAppender extends AppenderSkeleton { private boolean autoDelete = false; + /** + * Used to determine whether {@link MessageProperties#setMessageId(String)} is set. + */ + private boolean generateId = false; + public AmqpAppender() { } @@ -340,6 +348,14 @@ public class AmqpAppender extends AppenderSkeleton { this.autoDelete = autoDelete; } + public boolean isGenerateId() { + return generateId; + } + + public void setGenerateId(boolean generateId) { + this.generateId = generateId; + } + /** * Submit the required number of senders into the pool. */ @@ -427,6 +443,9 @@ public class AmqpAppender extends AppenderSkeleton { } amqpProps.setHeader(CATEGORY_NAME, name); amqpProps.setHeader(CATEGORY_LEVEL, level.toString()); + if (generateId) { + amqpProps.setMessageId(UUID.randomUUID().toString()); + } // Set applicationId, if we're using one if (null != applicationId) { diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/AmqpAppenderIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/AmqpAppenderIntegrationTests.java index abbc2ea4..35ee3b13 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/AmqpAppenderIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/AmqpAppenderIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). - * + * Copyright (c) 2011-2012 by the original author(s). + * * 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. @@ -13,6 +13,8 @@ package org.springframework.amqp.rabbit.log4j; +import static org.junit.Assert.assertNotNull; + import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; @@ -32,6 +34,7 @@ import org.springframework.util.Log4jConfigurer; /** * @author Jon Brisbin + * @author Gary Russell */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "org.springframework.amqp.rabbit.log4j" }, loader = AnnotationConfigContextLoader.class) @@ -73,6 +76,7 @@ public class AmqpAppenderIntegrationTests { log.error("This is an ERROR message", new RuntimeException("Test exception")); testListener.getLatch().await(5, TimeUnit.SECONDS); + assertNotNull(testListener.getId()); } @Test diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/TestListener.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/TestListener.java index ead5664e..a4087554 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/TestListener.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j/TestListener.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2012 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,11 +23,14 @@ import org.springframework.amqp.core.MessageListener; /** * @author Jon Brisbin + * @author Gary Russell */ public class TestListener implements MessageListener { private CountDownLatch latch; + private Object id; + public TestListener(int count) { latch = new CountDownLatch(count); } @@ -36,9 +39,14 @@ public class TestListener implements MessageListener { return latch; } - public void onMessage(Message message) { + public Object getId() { + return id; +} + +public void onMessage(Message message) { System.out.println("MESSAGE: " + message); System.out.println("BODY: " + new String(message.getBody())); + this.id = message.getMessageProperties().getMessageId(); latch.countDown(); } diff --git a/spring-rabbit/src/test/resources/log4j-amqp.properties b/spring-rabbit/src/test/resources/log4j-amqp.properties index 4a5f7f0b..13a55440 100644 --- a/spring-rabbit/src/test/resources/log4j-amqp.properties +++ b/spring-rabbit/src/test/resources/log4j-amqp.properties @@ -9,6 +9,7 @@ log4j.appender.amqp.applicationId=AmqpAppenderTest log4j.appender.amqp.routingKeyPattern=%X{applicationId}.%c.%p log4j.appender.amqp.layout=org.apache.log4j.PatternLayout log4j.appender.amqp.layout.ConversionPattern=%d %p %t [%c] - <%m>%n +log4j.appender.amqp.generateId=true log4j.category.org.springframework.amqp.rabbit.log4j=DEBUG, amqp