The expirationDate header is now stored as a Long instead of a Date.

This commit is contained in:
Mark Fisher
2008-08-25 13:44:25 +00:00
parent a162156b2a
commit 50a227aaa1
7 changed files with 102 additions and 81 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.message;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -55,14 +56,14 @@ public class GenericMessage<T> implements Message<T>, Serializable {
*/
public GenericMessage(T payload, Map<String, Object> headers) {
Assert.notNull(payload, "payload must not be null");
this.payload = payload;
if (headers == null) {
headers = new HashMap<String, Object>();
}
else if (headers instanceof MessageHeaders) {
else {
headers = new HashMap<String, Object>(headers);
}
this.headers = new MessageHeaders(headers);
this.payload = payload;
}

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import java.util.Set;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author Arjen Poutsma
@@ -45,10 +46,10 @@ public final class MessageBuilder<T> {
/**
* Create a builder for a new {@link Message} instance pre-populated with
* all of the headers copied from the provided message. The payload will
* also be taken from the provided message.
* all of the headers copied from the provided message. The payload of the
* provided Message will also be used as the payload for the new message.
*
* @param messageToCopy the Message from which the paylaod and all headers
* @param messageToCopy the Message from which the payload and all headers
* will be copied
*/
public static <T> MessageBuilder<T> fromMessage(Message<T> message) {
@@ -73,7 +74,7 @@ public final class MessageBuilder<T> {
* <code>null</code>, the header will be removed.
*/
public MessageBuilder<T> setHeader(String headerName, Object headerValue) {
if (headerName != null) {
if (StringUtils.hasLength(headerName)) {
if (headerValue == null) {
this.headers.remove(headerName);
}
@@ -128,10 +129,19 @@ public final class MessageBuilder<T> {
return this;
}
public MessageBuilder<T> setExpirationDate(Date expirationDate) {
public MessageBuilder<T> setExpirationDate(Long expirationDate) {
return this.setHeader(MessageHeaders.EXPIRATION_DATE, expirationDate);
}
public MessageBuilder<T> setExpirationDate(Date expirationDate) {
if (expirationDate != null) {
return this.setHeader(MessageHeaders.EXPIRATION_DATE, expirationDate.getTime());
}
else {
return this.setHeader(MessageHeaders.EXPIRATION_DATE, null);
}
}
public MessageBuilder<T> setCorrelationId(Object correlationId) {
return this.setHeader(MessageHeaders.CORRELATION_ID, correlationId);
}

View File

@@ -19,13 +19,10 @@ package org.springframework.integration.message;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.integration.util.IdGenerator;
import org.springframework.integration.util.RandomUuidGenerator;
import java.util.UUID;
/**
* The headers for a {@link Message}.
@@ -56,12 +53,11 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
private final Map<String, Object> headers;
private transient final IdGenerator idGenerator = new RandomUuidGenerator();
public MessageHeaders(Map<String, Object> headers) {
this.headers = (headers != null ? headers : new HashMap<String, Object>());
this.headers.put(ID, this.idGenerator.generateId());
this.headers = (headers != null ? headers
: new HashMap<String, Object>());
this.headers.put(ID, UUID.randomUUID());
this.headers.put(TIMESTAMP, new Long(System.currentTimeMillis()));
}
@@ -74,8 +70,8 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
return this.get(TIMESTAMP, Long.class);
}
public Date getExpirationDate() {
return this.get(EXPIRATION_DATE, Date.class);
public Long getExpirationDate() {
return this.get(EXPIRATION_DATE, Long.class);
}
public Object getCorrelationId() {
@@ -104,22 +100,6 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
return this.get(PRIORITY, MessagePriority.class);
}
public void clear() {
this.headers.clear();
}
public boolean containsKey(Object key) {
return this.headers.containsKey(key);
}
public boolean containsValue(Object value) {
return this.headers.containsValue(value);
}
public Set<Map.Entry<String, Object>> entrySet() {
return Collections.unmodifiableSet(this.headers.entrySet());
}
@SuppressWarnings("unchecked")
public <T> T get(Object key, Class<T> type) {
Object value = this.headers.get(key);
@@ -127,44 +107,14 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
return null;
}
if (!type.isAssignableFrom(value.getClass())) {
throw new IllegalArgumentException("Incorrect type specified for header '" + key
+ "'. Expected [" + type + "] but actual type is [" + value.getClass() + "]");
throw new IllegalArgumentException(
"Incorrect type specified for header '" + key
+ "'. Expected [" + type + "] but actual type is ["
+ value.getClass() + "]");
}
return (T) value;
}
public Object get(Object key) {
return this.headers.get(key);
}
public boolean isEmpty() {
return this.headers.isEmpty();
}
public Set<String> keySet() {
return Collections.unmodifiableSet(this.headers.keySet());
}
public Object put(String key, Object value) {
throw new UnsupportedOperationException("MessageHeaders is immutable.");
}
public void putAll(Map<? extends String, ? extends Object> t) {
throw new UnsupportedOperationException("MessageHeaders is immutable.");
}
public Object remove(Object key) {
throw new UnsupportedOperationException("MessageHeaders is immutable.");
}
public int size() {
return this.headers.size();
}
public Collection<Object> values() {
return Collections.unmodifiableCollection(this.headers.values());
}
public int hashCode() {
return headers.hashCode();
}
@@ -184,4 +134,60 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
return this.headers.toString();
}
/*
* Map implementation
*/
public void clear() {
this.headers.clear();
}
public boolean containsKey(Object key) {
return this.headers.containsKey(key);
}
public boolean containsValue(Object value) {
return this.headers.containsValue(value);
}
public Set<Map.Entry<String, Object>> entrySet() {
return Collections.unmodifiableSet(this.headers.entrySet());
}
public Object get(Object key) {
return this.headers.get(key);
}
public boolean isEmpty() {
return this.headers.isEmpty();
}
public Set<String> keySet() {
return Collections.unmodifiableSet(this.headers.keySet());
}
public int size() {
return this.headers.size();
}
public Collection<Object> values() {
return Collections.unmodifiableCollection(this.headers.values());
}
/*
* Unsupported operations
*/
public Object put(String key, Object value) {
throw new UnsupportedOperationException("MessageHeaders is immutable.");
}
public void putAll(Map<? extends String, ? extends Object> t) {
throw new UnsupportedOperationException("MessageHeaders is immutable.");
}
public Object remove(Object key) {
throw new UnsupportedOperationException("MessageHeaders is immutable.");
}
}

View File

@@ -16,24 +16,23 @@
package org.springframework.integration.message.selector;
import java.util.Date;
import org.springframework.integration.message.Message;
/**
* A {@link MessageSelector} that accepts {@link Message Messages} that are
* <em>not</em> expired.
* <em>not</em> yet expired. If a Message's expiration date header is
* <code>null</code>, that Message <em>never</em> expires.
*
* @author Mark Fisher
*/
public class UnexpiredMessageSelector implements MessageSelector {
public boolean accept(Message<?> message) {
Date expirationDate = message.getHeaders().getExpirationDate();
Long expirationDate = message.getHeaders().getExpirationDate();
if (expirationDate == null) {
return true;
}
return expirationDate.getTime() > System.currentTimeMillis();
return expirationDate > System.currentTimeMillis();
}
}

View File

@@ -21,7 +21,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
@@ -224,8 +223,8 @@ public class QueueChannelTests {
QueueChannel channel = new QueueChannel(2);
long minute = 60 * 1000;
long time = System.currentTimeMillis();
Date past = new Date(time - minute);
Date future = new Date(time + minute);
long past = time - minute;
long future = time + minute;
Message<String> expiredMessage = MessageBuilder.fromPayload("test1")
.setExpirationDate(past).build();
Message<String> unexpiredMessage = MessageBuilder.fromPayload("test2")

View File

@@ -101,11 +101,19 @@ public class MessageBuilderTests {
}
@Test
public void testExpirationDate() {
Date past = new Date(System.currentTimeMillis() - (60 * 1000));
public void testExpirationDateSetAsLong() {
Long past = System.currentTimeMillis() - (60 * 1000);
Message<Integer> expiredMessage = MessageBuilder.fromPayload(1)
.setExpirationDate(past).build();
assertEquals(past, expiredMessage.getHeaders().getExpirationDate());
}
@Test
public void testExpirationDateSetAsDate() {
Long past = System.currentTimeMillis() - (60 * 1000);
Message<Integer> expiredMessage = MessageBuilder.fromPayload(1)
.setExpirationDate(new Date(past)).build();
assertEquals(past, expiredMessage.getHeaders().getExpirationDate());
}
}

View File

@@ -19,8 +19,6 @@ package org.springframework.integration.message.selector;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Date;
import org.junit.Test;
import org.springframework.integration.message.Message;
@@ -35,7 +33,7 @@ public class UnexpiredMessageSelectorTests {
public void testExpiredMessageRejected() {
long past = System.currentTimeMillis() - 60000;
Message<String> message = MessageBuilder.fromPayload("expired")
.setExpirationDate(new Date(past)).build();
.setExpirationDate(past).build();
UnexpiredMessageSelector selector = new UnexpiredMessageSelector();
assertFalse(selector.accept(message));
}
@@ -44,7 +42,7 @@ public class UnexpiredMessageSelectorTests {
public void testUnexpiredMessageAccepted() {
long future = System.currentTimeMillis() + 60000;
Message<String> message = MessageBuilder.fromPayload("unexpired")
.setExpirationDate(new Date(future)).build();
.setExpirationDate(future).build();
UnexpiredMessageSelector selector = new UnexpiredMessageSelector();
assertTrue(selector.accept(message));
}