AMQP-253 Add Id Generation Option to AMQP Appender

log4j.appender.amqp.generateId=true

Turns on id generation - useful when configuring a
retry interceptor on the consuming side.

AMQP-253 Polishing

Fix default value; move to Message Properties.
This commit is contained in:
Gary Russell
2012-07-17 15:37:27 -04:00
committed by Oleg Zhurakousky
parent d11991838d
commit 22c0a59c7f
4 changed files with 42 additions and 10 deletions

View File

@@ -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;
* </pre>
*
* @author Jon Brisbin <jbrisbin@vmware.com>
* @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) {

View File

@@ -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 <jbrisbin@vmware.com>
* @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

View File

@@ -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 <jbrisbin@vmware.com>
* @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();
}

View File

@@ -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