Skip to content

spring-integration-event minor changes #9953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -30,7 +30,8 @@
* Spring's {@link ApplicationEvent} used by this adapter to simply wrap the
* {@link Message}.
* <p>
* If the {@link #publishPayload} flag is specified to {@code true}, the {@code payload}
* However, if the {@code payload} is an instance of {@link ApplicationEvent}, or
* if the {@link #publishPayload} flag is specified to {@code true}, the {@code payload}
* will be published as is without wrapping to any {@link ApplicationEvent}.
*
* @author Mark Fisher
Expand Down Expand Up @@ -63,8 +64,8 @@ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEv
@Override
protected void handleMessageInternal(Message<?> message) {
Assert.notNull(this.applicationEventPublisher, "applicationEventPublisher is required");
if (message.getPayload() instanceof ApplicationEvent) {
this.applicationEventPublisher.publishEvent((ApplicationEvent) message.getPayload());
if (message.getPayload() instanceof ApplicationEvent applicationEvent) {
this.applicationEventPublisher.publishEvent(applicationEvent);
}
else if (this.publishPayload) {
this.applicationEventPublisher.publishEvent(message.getPayload());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -29,6 +29,7 @@
/**
* @author Mark Fisher
* @author Artem Bilan
* @author Ma Jiandong
*/
public class ApplicationEventPublishingMessageHandlerTests {

Expand All @@ -40,7 +41,7 @@ public void messagingEvent() {
assertThat(publisher.getLastEvent()).isNull();
Message<?> message = new GenericMessage<>("testing");
handler.handleMessage(message);
ApplicationEvent event = publisher.getLastEvent();
Object event = publisher.getLastEvent();
assertThat(event.getClass()).isEqualTo(MessagingEvent.class);
assertThat(((MessagingEvent) event).getMessage()).isEqualTo(message);
}
Expand All @@ -53,27 +54,36 @@ public void payloadAsEvent() {
assertThat(publisher.getLastEvent()).isNull();
Message<?> message = new GenericMessage<>(new TestEvent("foo"));
handler.handleMessage(message);
ApplicationEvent event = publisher.getLastEvent();
Object event = publisher.getLastEvent();
assertThat(event.getClass()).isEqualTo(TestEvent.class);
assertThat((event).getSource()).isEqualTo("foo");
assertThat(((ApplicationEvent) event).getSource()).isEqualTo("foo");
}

private static class TestApplicationEventPublisher implements ApplicationEventPublisher {
@Test
public void payloadAsIs() {
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
handler.setApplicationEventPublisher(publisher);
handler.setPublishPayload(true);
assertThat(publisher.getLastEvent()).isNull();
Message<?> message = new GenericMessage<>("testing");
handler.handleMessage(message);
Object event = publisher.getLastEvent();
assertThat(event.getClass()).isEqualTo(String.class);
assertThat(((String) event)).isEqualTo("testing");
}

private volatile ApplicationEvent lastEvent;
private static class TestApplicationEventPublisher implements ApplicationEventPublisher {

public ApplicationEvent getLastEvent() {
return this.lastEvent;
}
private volatile Object lastEvent;

@Override
public void publishEvent(ApplicationEvent event) {
this.lastEvent = event;
public Object getLastEvent() {
return lastEvent;
}

@Override
public void publishEvent(Object event) {

this.lastEvent = event;
}

}
Expand Down