MessageHeader is now an interface. The DefaultMessageHeader class is the implementation used by GenericMessage (INT-288).

This commit is contained in:
Mark Fisher
2008-07-06 22:34:51 +00:00
parent 9c49063a3c
commit 99af66481d
5 changed files with 195 additions and 121 deletions

View File

@@ -0,0 +1,162 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* 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.
*/
package org.springframework.integration.message;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* The default implementation of the {@link MessageHeader} interface.
*
* @author Mark Fisher
*/
public class DefaultMessageHeader implements MessageHeader, Serializable {
private final Date timestamp = new Date();
private volatile Date expiration;
private volatile Object correlationId;
private transient volatile Object returnAddress;
private volatile int sequenceNumber = 1;
private volatile int sequenceSize = 1;
private volatile MessagePriority priority = MessagePriority.NORMAL;
private final Properties properties = new Properties();
private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>();
/**
* Return the creation time of this message.
*/
public Date getTimestamp() {
return this.timestamp;
}
/**
* Return the expiration date for this message or <code>null</code> to
* indicate 'never expire'.
*/
public Date getExpiration() {
return this.expiration;
}
/**
* Set the expiration date for this message or <code>null</code> to
* indicate 'never expire'. The default is <code>null</code>.
*/
public void setExpiration(Date expiration) {
this.expiration = expiration;
}
public Object getCorrelationId() {
return this.correlationId;
}
public void setCorrelationId(Object correlationId) {
this.correlationId = correlationId;
}
public Object getReturnAddress() {
return this.returnAddress;
}
public void setReturnAddress(Object returnAddress) {
this.returnAddress = returnAddress;
}
public int getSequenceNumber() {
return this.sequenceNumber;
}
public void setSequenceNumber(int sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
public int getSequenceSize() {
return this.sequenceSize;
}
public void setSequenceSize(int sequenceSize) {
this.sequenceSize = sequenceSize;
}
public MessagePriority getPriority() {
return this.priority;
}
public void setPriority(MessagePriority priority) {
this.priority = priority;
}
public String getProperty(String key) {
return this.properties.getProperty(key);
}
public String setProperty(String key, String value) {
return (String) this.properties.setProperty(key, value);
}
public String removeProperty(String key) {
return (String) this.properties.remove(key);
}
public Set<String> getPropertyNames() {
Set<String> propertyNames = new HashSet<String>();
for (Object key : this.properties.keySet()) {
propertyNames.add((String) key);
}
return propertyNames;
}
public Object getAttribute(String key) {
return this.attributes.get(key);
}
public Object setAttribute(String key, Object value) {
return this.attributes.put(key, value);
}
public Object setAttributeIfAbsent(String key, Object value) {
return this.attributes.putIfAbsent(key, value);
}
public Object removeAttribute(String key) {
return this.attributes.remove(key);
}
public Set<String> getAttributeNames() {
return this.attributes.keySet();
}
public String toString() {
return "[CorrelationID=" + this.correlationId + "][Properties=" + this.properties + "][Attributes=" + this.attributes +
"][Timestamp=" + this.timestamp + "][Expiration=" + this.expiration + "][Priority=" + this.priority +
"][Sequence #" + this.sequenceNumber + " (of " + this.sequenceSize + ")]";
}
}

View File

@@ -32,7 +32,7 @@ public class GenericMessage<T> implements Message<T> {
private final Object id;
private final MessageHeader header = new MessageHeader();
private final MessageHeader header = new DefaultMessageHeader();
private volatile T payload;

View File

@@ -16,13 +16,8 @@
package org.springframework.integration.message;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* A holder for Message metadata. This includes information that may be used by
@@ -33,134 +28,50 @@ import java.util.concurrent.ConcurrentMap;
*
* @author Mark Fisher
*/
public class MessageHeader implements Serializable {
public interface MessageHeader {
private final Date timestamp = new Date();
Date getTimestamp();
private volatile Date expiration;
Date getExpiration();
private volatile Object correlationId;
void setExpiration(Date expiration);
private transient volatile Object returnAddress;
Object getCorrelationId();
private volatile int sequenceNumber = 1;
void setCorrelationId(Object correlationId);
private volatile int sequenceSize = 1;
Object getReturnAddress();
private volatile MessagePriority priority = MessagePriority.NORMAL;
void setReturnAddress(Object returnAddress);
private final Properties properties = new Properties();
int getSequenceNumber();
private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>();
void setSequenceNumber(int sequenceNumber);
int getSequenceSize();
/**
* Return the creation time of this message.
*/
public Date getTimestamp() {
return this.timestamp;
}
void setSequenceSize(int sequenceSize);
/**
* Return the expiration date for this message or <code>null</code> to
* indicate 'never expire'.
*/
public Date getExpiration() {
return this.expiration;
}
MessagePriority getPriority();
/**
* Set the expiration date for this message or <code>null</code> to
* indicate 'never expire'. The default is <code>null</code>.
*/
public void setExpiration(Date expiration) {
this.expiration = expiration;
}
void setPriority(MessagePriority priority);
public Object getCorrelationId() {
return this.correlationId;
}
String getProperty(String key);
public void setCorrelationId(Object correlationId) {
this.correlationId = correlationId;
}
String setProperty(String key, String value);
public Object getReturnAddress() {
return this.returnAddress;
}
String removeProperty(String key);
public void setReturnAddress(Object returnAddress) {
this.returnAddress = returnAddress;
}
Set<String> getPropertyNames();
public int getSequenceNumber() {
return this.sequenceNumber;
}
Object getAttribute(String key);
public void setSequenceNumber(int sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
Object setAttribute(String key, Object value);
public int getSequenceSize() {
return this.sequenceSize;
}
Object setAttributeIfAbsent(String key, Object value);
public void setSequenceSize(int sequenceSize) {
this.sequenceSize = sequenceSize;
}
Object removeAttribute(String key);
public MessagePriority getPriority() {
return this.priority;
}
public void setPriority(MessagePriority priority) {
this.priority = priority;
}
public String getProperty(String key) {
return this.properties.getProperty(key);
}
public String setProperty(String key, String value) {
return (String) this.properties.setProperty(key, value);
}
public String removeProperty(String key) {
return (String) this.properties.remove(key);
}
public Set<String> getPropertyNames() {
Set<String> propertyNames = new HashSet<String>();
for (Object key : this.properties.keySet()) {
propertyNames.add((String) key);
}
return propertyNames;
}
public Object getAttribute(String key) {
return this.attributes.get(key);
}
public Object setAttribute(String key, Object value) {
return this.attributes.put(key, value);
}
public Object setAttributeIfAbsent(String key, Object value) {
return this.attributes.putIfAbsent(key, value);
}
public Object removeAttribute(String key) {
return this.attributes.remove(key);
}
public Set<String> getAttributeNames() {
return this.attributes.keySet();
}
public String toString() {
return "[CorrelationID=" + this.correlationId + "][Properties=" + this.properties + "][Attributes=" + this.attributes +
"][Timestamp=" + this.timestamp + "][Expiration=" + this.expiration + "][Priority=" + this.priority +
"][Sequence #" + this.sequenceNumber + " (of " + this.sequenceSize + ")]";
}
Set<String> getAttributeNames();
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration;
package org.springframework.integration.message;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -26,22 +26,23 @@ import java.util.Set;
import org.junit.Test;
import org.springframework.integration.message.DefaultMessageHeader;
import org.springframework.integration.message.MessageHeader;
/**
* @author Mark Fisher
*/
public class MessageHeaderTests {
public class DefaultMessageHeaderTests {
@Test
public void testTimestamp() {
MessageHeader header = new MessageHeader();
MessageHeader header = new DefaultMessageHeader();
assertNotNull(header.getTimestamp());
}
@Test
public void testAttributes() {
MessageHeader header = new MessageHeader();
MessageHeader header = new DefaultMessageHeader();
Integer value = new Integer(123);
Object previousValue = header.setAttribute("test", value);
assertNull(previousValue);
@@ -58,7 +59,7 @@ public class MessageHeaderTests {
@Test
public void testProperties() {
MessageHeader header = new MessageHeader();
MessageHeader header = new DefaultMessageHeader();
String previousValue = header.setProperty("foo", "bar");
assertNull(previousValue);
assertEquals("bar", header.getProperty("foo"));
@@ -73,7 +74,7 @@ public class MessageHeaderTests {
@Test
public void testSetAttributeIfAbsent() {
MessageHeader header = new MessageHeader();
MessageHeader header = new DefaultMessageHeader();
Integer integer = new Integer(123);
assertNull(header.getAttribute("test"));
assertFalse(header.getAttributeNames().contains("test"));
@@ -85,7 +86,7 @@ public class MessageHeaderTests {
@Test
public void testSetAttributeIfAbsentDoesNotOverride() {
MessageHeader header = new MessageHeader();
MessageHeader header = new DefaultMessageHeader();
Integer originalValue = new Integer(123);
header.setAttributeIfAbsent("test", originalValue);
Object existingValue = header.setAttributeIfAbsent("test", new Integer(456));

View File

@@ -58,7 +58,7 @@ public class GenericMessageTests {
@Test
public void testMessageHeaderCopied() {
MessageHeader header = new MessageHeader();
MessageHeader header = new DefaultMessageHeader();
header.setAttribute("testAttribute", new Integer(123));
header.setProperty("testProperty", "foo");
header.setSequenceSize(42);