Skip to content

GenericMessage unnecessarily copies message headers before passing to MessageHeaders [SPR-11268] #15893

@spring-projects-issues

Description

@spring-projects-issues

Luke Jackson opened SPR-11268 and commented

In the following GenericMessage constructor:

public GenericMessage(T payload, Map<String, Object> headers) {
     Assert.notNull(payload, "payload must not be null");
     if (headers == null) {
          headers = new HashMap<String, Object>();
     }
     else {
          headers = new HashMap<String, Object>(headers);
     }
     this.headers = new MessageHeaders(headers);
     this.payload = payload;
}

The headers are being copied (presumably to ensure immutability).

However MessageHeaders itself copies the header map provided to it, which results in an unnecessary double copying of the headers map:

	public MessageHeaders(Map<String, Object> headers) {
		this.headers = (headers != null) ? new HashMap<String, Object>(headers) : new HashMap<String, Object>();
		IdGenerator generatorToUse = (idGenerator != null) ? idGenerator : defaultIdGenerator;
		this.headers.put(ID, generatorToUse.generateId());

		this.headers.put(TIMESTAMP, new Long(System.currentTimeMillis()));
	}

Affects: 4.0 GA

Referenced from: commits 3b14e97

Metadata

Metadata

Assignees

Labels

in: messagingIssues in messaging modules (jms, messaging)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions