AMQP-268 Fix Appender MDC Properties

MDC Properties were being stored under key
Entry.toString() instead of Entry.getKey().toString().
This commit is contained in:
Gary Russell
2012-08-30 12:03:43 -04:00
parent 98bc284432
commit c309741003
3 changed files with 31 additions and 10 deletions

View File

@@ -15,6 +15,8 @@ package org.springframework.amqp.rabbit.log4j;
import java.util.Calendar;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
@@ -491,8 +493,10 @@ public class AmqpAppender extends AppenderSkeleton {
// Copy properties in from MDC
@SuppressWarnings("rawtypes")
Map props = event.getProperties();
for (Object key : event.getProperties().entrySet()) {
amqpProps.setHeader(key.toString(), props.get(key));
@SuppressWarnings("unchecked")
Set<Entry<?,?>> entrySet = props.entrySet();
for (Entry<?, ?> entry : entrySet) {
amqpProps.setHeader(entry.getKey().toString(), entry.getValue());
}
LocationInfo locInfo = logEvent.getLocationInformation();
if (!"?".equals(locInfo.getClassName())) {

View File

@@ -13,7 +13,9 @@
package org.springframework.amqp.rabbit.log4j;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
@@ -24,6 +26,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.test.BrokerRunning;
import org.springframework.beans.factory.annotation.Autowired;
@@ -75,7 +78,7 @@ public class AmqpAppenderIntegrationTests {
log.warn("This is a WARN message");
log.error("This is an ERROR message", new RuntimeException("Test exception"));
testListener.getLatch().await(5, TimeUnit.SECONDS);
assertTrue(testListener.getLatch().await(5, TimeUnit.SECONDS));
assertNotNull(testListener.getId());
}
@@ -85,14 +88,20 @@ public class AmqpAppenderIntegrationTests {
listenerContainer.setMessageListener(testListener);
listenerContainer.start();
MDC.put("someproperty", "property.value");
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("someproperty");
MDC.remove(propertyName);
testListener.getLatch().await(5, TimeUnit.SECONDS);
assertTrue(testListener.getLatch().await(5, TimeUnit.SECONDS));
MessageProperties messageProperties = testListener.getMessageProperties();
assertNotNull(messageProperties);
assertNotNull(messageProperties.getHeaders().get(propertyName));
assertEquals(propertyValue, messageProperties.getHeaders().get(propertyName));
}
}

View File

@@ -20,6 +20,7 @@ import java.util.concurrent.CountDownLatch;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.core.MessageProperties;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
@@ -29,7 +30,7 @@ public class TestListener implements MessageListener {
private CountDownLatch latch;
private Object id;
private MessageProperties messageProperties;
public TestListener(int count) {
latch = new CountDownLatch(count);
@@ -40,13 +41,20 @@ public class TestListener implements MessageListener {
}
public Object getId() {
return id;
}
if (this.messageProperties == null) {
throw new IllegalStateException("No MessageProperties received");
}
return this.messageProperties.getMessageId();
}
public MessageProperties getMessageProperties() {
return this.messageProperties;
}
public void onMessage(Message message) {
System.out.println("MESSAGE: " + message);
System.out.println("BODY: " + new String(message.getBody()));
this.id = message.getMessageProperties().getMessageId();
this.messageProperties = message.getMessageProperties();
latch.countDown();
}