INT-1583 if received event has a Message as its source, it will be passed as-is
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.context.ApplicationListener;
|
|||||||
import org.springframework.context.event.ApplicationContextEvent;
|
import org.springframework.context.event.ApplicationContextEvent;
|
||||||
import org.springframework.expression.Expression;
|
import org.springframework.expression.Expression;
|
||||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||||
|
import org.springframework.integration.Message;
|
||||||
import org.springframework.integration.endpoint.MessageProducerSupport;
|
import org.springframework.integration.endpoint.MessageProducerSupport;
|
||||||
import org.springframework.integration.support.MessageBuilder;
|
import org.springframework.integration.support.MessageBuilder;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
@@ -105,8 +106,13 @@ public class ApplicationEventListeningMessageProducer extends MessageProducerSup
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendEventAsMessage(ApplicationEvent event) {
|
private void sendEventAsMessage(ApplicationEvent event) {
|
||||||
Object payload = (this.payloadExpression != null) ? this.payloadExpression.getValue(event) : event;
|
if (event.getSource() instanceof Message<?>) {
|
||||||
this.sendMessage(MessageBuilder.withPayload(payload).build());
|
this.sendMessage((Message<?>) event.getSource());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Object payload = (this.payloadExpression != null) ? this.payloadExpression.getValue(event) : event;
|
||||||
|
this.sendMessage(MessageBuilder.withPayload(payload).build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|||||||
import org.springframework.integration.Message;
|
import org.springframework.integration.Message;
|
||||||
import org.springframework.integration.channel.QueueChannel;
|
import org.springframework.integration.channel.QueueChannel;
|
||||||
import org.springframework.integration.core.PollableChannel;
|
import org.springframework.integration.core.PollableChannel;
|
||||||
import org.springframework.integration.event.inbound.ApplicationEventListeningMessageProducer;
|
import org.springframework.integration.event.core.MessagingEvent;
|
||||||
|
import org.springframework.integration.message.GenericMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
@@ -116,6 +117,34 @@ public class ApplicationEventListeningMessageProducerTests {
|
|||||||
assertEquals("received: event2", message3.getPayload());
|
assertEquals("received: event2", message3.getPayload());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void messagingEventReceived() {
|
||||||
|
QueueChannel channel = new QueueChannel();
|
||||||
|
ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer();
|
||||||
|
adapter.setOutputChannel(channel);
|
||||||
|
adapter.start();
|
||||||
|
Message<?> message1 = channel.receive(0);
|
||||||
|
assertNull(message1);
|
||||||
|
adapter.onApplicationEvent(new MessagingEvent(new GenericMessage<String>("test")));
|
||||||
|
Message<?> message2 = channel.receive(20);
|
||||||
|
assertNotNull(message2);
|
||||||
|
assertEquals("test", message2.getPayload());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void messageAsSourceOrCustomEventType() {
|
||||||
|
QueueChannel channel = new QueueChannel();
|
||||||
|
ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer();
|
||||||
|
adapter.setOutputChannel(channel);
|
||||||
|
adapter.start();
|
||||||
|
Message<?> message1 = channel.receive(0);
|
||||||
|
assertNull(message1);
|
||||||
|
adapter.onApplicationEvent(new TestMessagingEvent(new GenericMessage<String>("test")));
|
||||||
|
Message<?> message2 = channel.receive(20);
|
||||||
|
assertNotNull(message2);
|
||||||
|
assertEquals("test", message2.getPayload());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
private static class TestApplicationEvent1 extends ApplicationEvent {
|
private static class TestApplicationEvent1 extends ApplicationEvent {
|
||||||
@@ -134,4 +163,13 @@ public class ApplicationEventListeningMessageProducerTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
private static class TestMessagingEvent extends ApplicationEvent {
|
||||||
|
|
||||||
|
public TestMessagingEvent(Message<?> message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user