Skip to content

fix: ActiveMQObjectMessage/ActiveMQMapMessage doGetBody() deserializes message body twice - #2561

Open
gt2001atgmail wants to merge 2 commits into
apache:mainfrom
gt2001atgmail:fix/2516-object-map-message-double-deserialize
Open

gt2001atgmail wants to merge 2 commits into
apache:mainfrom
gt2001atgmail:fix/2516-object-map-message-double-deserialize

Conversation

@gt2001atgmail

Copy link
Copy Markdown

doGetBody() in both classes re-deserializes the content directly instead of reusing the cache getObject()/getContentMap() already populate, so getBody() ends up deserializing twice. Fixed both to delegate to the cached getter, same as ActiveMQTextMessage already does. Added a test per class to catch the regression.

See: #2516

…s message body twice

doGetBody() in both classes re-deserializes the content directly instead of reusing the cache getObject()/getContentMap() already populate, so getBody() ends up deserializing twice. Fixed both to delegate to the cached getter, same as ActiveMQTextMessage already does. Added a test per class to catch the regression.

See: apache#2516
@lobtx

lobtx commented Nov 11, 2026

Copy link
Copy Markdown

Hi, I was about to open a PR for #2516 when I saw yours, so I reviewed and tested it instead.

I tested this PR merged with the current main (it merges cleanly) on JDK 21:

  • the new tests pass, as well as ActiveMQJMS2MessageTest, JMSMessageTest and ActiveMQJMS2ReceiveBodyTest (103 tests)
  • I also ran a test that counts the actual deserializations, using a Serializable with a readObject() that increments a counter, on a message that only has its serialized content (storeContentAndClear()): getBody() deserializes twice on main and once with this PR.

Two small remarks:

  1. ActiveMQObjectMessage.doGetBody() still calls storeContent() before getObject(). On the producer side, after setObject(), this serializes the object on every getBody() call although the result is not used anymore. It could be removed, unless there is a reason to keep it.
  2. Worth mentioning in the release notes maybe: getBody() now returns the same instance as getObject() / getContentMap() instead of a new copy. That looks fine to me since getObject() already returned the cached instance.

If it helps, the counting test (it checks the number of deserializations rather than the instance identity):

public void testGetBodyDeserializesOnce() throws Exception {
    ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
    msg.setObject(new CountingSerializable("test"));
    // as on the consumer side: only the serialized content is available
    msg.storeContentAndClear();

    CountingSerializable.READ_COUNT.set(0);
    CountingSerializable body = msg.getBody(CountingSerializable.class);
    assertEquals("test", body.value);
    assertEquals(1, CountingSerializable.READ_COUNT.get());
}

static class CountingSerializable implements Serializable {
    private static final long serialVersionUID = 1L;
    static final AtomicInteger READ_COUNT = new AtomicInteger();

    final String value;

    CountingSerializable(String value) {
        this.value = value;
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        READ_COUNT.incrementAndGet();
    }
}

LGTM otherwise.

getObject() already handles it. Also add a deserialization-count
test suggested in review.
@gt2001atgmail

Copy link
Copy Markdown
Author

Thanks for testing this so thoroughly!

  1. Good catch, removed. It only serialized once per setObject(), not on every call, but it was still unneeded.
  2. Agreed, worth a release note.

Added your counting test too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants