Skip to content

Commit 1949453

Browse files
authored
spring-integration-event minor changes
1. Fix Javadoc & use pattern matching for instanceof 2. Polish unit tests Signed-off-by: Ma,Jiandong <[email protected]>
1 parent 4dd4a83 commit 1949453

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

Diff for: spring-integration-event/src/main/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandler.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,8 @@
3030
* Spring's {@link ApplicationEvent} used by this adapter to simply wrap the
3131
* {@link Message}.
3232
* <p>
33-
* If the {@link #publishPayload} flag is specified to {@code true}, the {@code payload}
33+
* However, if the {@code payload} is an instance of {@link ApplicationEvent}, or
34+
* if the {@link #publishPayload} flag is specified to {@code true}, the {@code payload}
3435
* will be published as is without wrapping to any {@link ApplicationEvent}.
3536
*
3637
* @author Mark Fisher
@@ -63,8 +64,8 @@ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEv
6364
@Override
6465
protected void handleMessageInternal(Message<?> message) {
6566
Assert.notNull(this.applicationEventPublisher, "applicationEventPublisher is required");
66-
if (message.getPayload() instanceof ApplicationEvent) {
67-
this.applicationEventPublisher.publishEvent((ApplicationEvent) message.getPayload());
67+
if (message.getPayload() instanceof ApplicationEvent applicationEvent) {
68+
this.applicationEventPublisher.publishEvent(applicationEvent);
6869
}
6970
else if (this.publishPayload) {
7071
this.applicationEventPublisher.publishEvent(message.getPayload());

Diff for: spring-integration-event/src/test/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandlerTests.java

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
/**
3030
* @author Mark Fisher
3131
* @author Artem Bilan
32+
* @author Ma Jiandong
3233
*/
3334
public class ApplicationEventPublishingMessageHandlerTests {
3435

@@ -40,7 +41,7 @@ public void messagingEvent() {
4041
assertThat(publisher.getLastEvent()).isNull();
4142
Message<?> message = new GenericMessage<>("testing");
4243
handler.handleMessage(message);
43-
ApplicationEvent event = publisher.getLastEvent();
44+
Object event = publisher.getLastEvent();
4445
assertThat(event.getClass()).isEqualTo(MessagingEvent.class);
4546
assertThat(((MessagingEvent) event).getMessage()).isEqualTo(message);
4647
}
@@ -53,27 +54,36 @@ public void payloadAsEvent() {
5354
assertThat(publisher.getLastEvent()).isNull();
5455
Message<?> message = new GenericMessage<>(new TestEvent("foo"));
5556
handler.handleMessage(message);
56-
ApplicationEvent event = publisher.getLastEvent();
57+
Object event = publisher.getLastEvent();
5758
assertThat(event.getClass()).isEqualTo(TestEvent.class);
58-
assertThat((event).getSource()).isEqualTo("foo");
59+
assertThat(((ApplicationEvent) event).getSource()).isEqualTo("foo");
5960
}
6061

61-
private static class TestApplicationEventPublisher implements ApplicationEventPublisher {
62+
@Test
63+
public void payloadAsIs() {
64+
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
65+
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
66+
handler.setApplicationEventPublisher(publisher);
67+
handler.setPublishPayload(true);
68+
assertThat(publisher.getLastEvent()).isNull();
69+
Message<?> message = new GenericMessage<>("testing");
70+
handler.handleMessage(message);
71+
Object event = publisher.getLastEvent();
72+
assertThat(event.getClass()).isEqualTo(String.class);
73+
assertThat(((String) event)).isEqualTo("testing");
74+
}
6275

63-
private volatile ApplicationEvent lastEvent;
76+
private static class TestApplicationEventPublisher implements ApplicationEventPublisher {
6477

65-
public ApplicationEvent getLastEvent() {
66-
return this.lastEvent;
67-
}
78+
private volatile Object lastEvent;
6879

69-
@Override
70-
public void publishEvent(ApplicationEvent event) {
71-
this.lastEvent = event;
80+
public Object getLastEvent() {
81+
return lastEvent;
7282
}
7383

7484
@Override
7585
public void publishEvent(Object event) {
76-
86+
this.lastEvent = event;
7787
}
7888

7989
}

0 commit comments

Comments
 (0)