Update MessageHeaders constructor
Following discussion with Gary. Issue: SPR-11468
This commit is contained in:
parent
1eee339c15
commit
ddf8750511
|
|
@ -34,6 +34,7 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.util.AlternativeJdkIdGenerator;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.IdGenerator;
|
||||
|
||||
/**
|
||||
|
|
@ -96,24 +97,36 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
|
|||
|
||||
private final Map<String, Object> headers;
|
||||
|
||||
/**
|
||||
* Constructs a minimal {@link MessageHeaders} with zero headers.
|
||||
*/
|
||||
protected MessageHeaders() {
|
||||
this.headers = new HashMap<String, Object>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Consructs a {@link MessageHeaders} from the headers map; adding (or
|
||||
* overwriting) the {@link #ID} and {@link #TIMESTAMP} headers.
|
||||
* @param headers The map.
|
||||
* @param headers a map with headers to add
|
||||
*/
|
||||
public MessageHeaders(Map<String, Object> headers) {
|
||||
this.headers = (headers != null) ? new HashMap<String, Object>(headers) : new HashMap<String, Object>();
|
||||
this.headers.put(ID, ((idGenerator != null) ? idGenerator : defaultIdGenerator).generateId());
|
||||
this.headers.put(TIMESTAMP, System.currentTimeMillis());
|
||||
this(headers, ((idGenerator != null) ? idGenerator : defaultIdGenerator).generateId(),
|
||||
System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor allowing a sub-class to access the (mutable) header map as well
|
||||
* to provide the ID and TIMESTAMP header values.
|
||||
*
|
||||
* @param headers a map with headers to add
|
||||
* @param id the value for the {@link #ID} header, never {@code null}
|
||||
* @param timestamp the value for the {@link #TIMESTAMP} header,
|
||||
* or {@code null} meaning no timestamp header
|
||||
*/
|
||||
protected MessageHeaders(Map<String, Object> headers, UUID id, Long timestamp) {
|
||||
Assert.notNull(id, "'id' is required");
|
||||
this.headers = (headers != null) ? new HashMap<String, Object>(headers) : new HashMap<String, Object>();
|
||||
this.headers.put(ID, id);
|
||||
if (timestamp != null) {
|
||||
this.headers.put(TIMESTAMP, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected Map<String, Object> getRawHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -142,13 +142,13 @@ public class MessageHeadersTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void subclass() {
|
||||
public void subClassWithCustomIdAndNoTimestamp() {
|
||||
final AtomicLong id = new AtomicLong();
|
||||
@SuppressWarnings("serial")
|
||||
class MyMH extends MessageHeaders {
|
||||
|
||||
public MyMH() {
|
||||
this.getRawHeaders().put(ID, new UUID(0, id.incrementAndGet()));
|
||||
super(null, new UUID(0, id.incrementAndGet()), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue