Renamed ApplicationEventSource and ApplicationEventTarget. The new names are ApplicationEventInboundChannelAdapter and ApplicationEventOutboundChannelAdapter respectively.
This commit is contained in:
@@ -16,36 +16,25 @@
|
||||
|
||||
package org.springframework.integration.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.MessageChannelTemplate;
|
||||
import org.springframework.integration.endpoint.AbstractProducerEndpoint;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* A message source for passing Spring
|
||||
* An inbound Channel Adapter that passes Spring
|
||||
* {@link ApplicationEvent ApplicationEvents} within messages.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ApplicationEventSource implements ApplicationListener {
|
||||
public class ApplicationEventInboundChannelAdapter extends AbstractProducerEndpoint implements ApplicationListener {
|
||||
|
||||
private final MessageChannel channel;
|
||||
|
||||
private List<Class<? extends ApplicationEvent>> eventTypes = new ArrayList<Class<? extends ApplicationEvent>>();
|
||||
|
||||
private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate();
|
||||
|
||||
|
||||
public ApplicationEventSource(MessageChannel channel) {
|
||||
Assert.notNull(channel, "channel must not be null");
|
||||
this.channel = channel;
|
||||
}
|
||||
private final List<Class<? extends ApplicationEvent>> eventTypes = new CopyOnWriteArrayList<Class<? extends ApplicationEvent>>();
|
||||
|
||||
|
||||
/**
|
||||
@@ -55,25 +44,27 @@ public class ApplicationEventSource implements ApplicationListener {
|
||||
*/
|
||||
public void setEventTypes(List<Class<? extends ApplicationEvent>> eventTypes) {
|
||||
Assert.notEmpty(eventTypes, "at least one event type is required");
|
||||
this.eventTypes = eventTypes;
|
||||
synchronized (this.eventTypes) {
|
||||
this.eventTypes.clear();
|
||||
this.eventTypes.addAll(eventTypes);
|
||||
}
|
||||
}
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
if (CollectionUtils.isEmpty(this.eventTypes)) {
|
||||
this.sendMessage(event);
|
||||
this.sendEventAsMessage(event);
|
||||
return;
|
||||
}
|
||||
for (Class<? extends ApplicationEvent> eventType : this.eventTypes) {
|
||||
if (eventType.isAssignableFrom(event.getClass())) {
|
||||
this.sendMessage(event);
|
||||
this.sendEventAsMessage(event);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean sendMessage(ApplicationEvent event) {
|
||||
return this.channelTemplate.send(
|
||||
new GenericMessage<ApplicationEvent>(event), this.channel);
|
||||
private boolean sendEventAsMessage(ApplicationEvent event) {
|
||||
return this.sendMessage(MessageBuilder.fromPayload(event).build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,17 +19,18 @@ package org.springframework.integration.event;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.integration.endpoint.AbstractMessageConsumingEndpoint;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageTarget;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A message target for publishing {@link MessagingEvent MessagingEvents}. The
|
||||
* {@link MessagingEvent} is a subclass of Spring's {@link ApplicationEvent}
|
||||
* used by this adapter to wrap any {@link Message} sent to this target.
|
||||
* An outbound Channel Adapter that publishes each {@link Message} it receives
|
||||
* as a {@link MessagingEvent}. The {@link MessagingEvent} is a subclass of Spring's
|
||||
* {@link ApplicationEvent} used by this adapter to simply wrap the {@link Message}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ApplicationEventTarget<T> implements MessageTarget, ApplicationEventPublisherAware {
|
||||
public class ApplicationEventOutboundChannelAdapter<T> extends AbstractMessageConsumingEndpoint implements ApplicationEventPublisherAware {
|
||||
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
@@ -38,10 +39,10 @@ public class ApplicationEventTarget<T> implements MessageTarget, ApplicationEven
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
public boolean send(Message<?> message) {
|
||||
this.applicationEventPublisher.publishEvent(
|
||||
new MessagingEvent((Message<?>) message));
|
||||
return true;
|
||||
@Override
|
||||
protected void processMessage(Message<?> message) {
|
||||
Assert.notNull(this.applicationEventPublisher, "applicationEventPublisher is required");
|
||||
this.applicationEventPublisher.publishEvent(new MessagingEvent((Message<?>) message));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,8 +20,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -38,12 +37,13 @@ import org.springframework.integration.message.Message;
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ApplicationEventSourceTests {
|
||||
public class ApplicationEventInboundChannelAdapterTests {
|
||||
|
||||
@Test
|
||||
public void testAnyApplicationEventSentByDefault() {
|
||||
QueueChannel channel = new QueueChannel();
|
||||
ApplicationEventSource adapter = new ApplicationEventSource(channel);
|
||||
ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter();
|
||||
adapter.setOutputChannel(channel);
|
||||
Message<?> message1 = channel.receive(0);
|
||||
assertNull(message1);
|
||||
adapter.onApplicationEvent(new TestApplicationEvent1());
|
||||
@@ -59,10 +59,9 @@ public class ApplicationEventSourceTests {
|
||||
@Test
|
||||
public void testOnlyConfiguredEventTypesAreSent() {
|
||||
QueueChannel channel = new QueueChannel();
|
||||
ApplicationEventSource adapter = new ApplicationEventSource(channel);
|
||||
List<Class<? extends ApplicationEvent>> eventTypes = new ArrayList<Class<? extends ApplicationEvent>>();
|
||||
eventTypes.add(TestApplicationEvent1.class);
|
||||
adapter.setEventTypes(eventTypes);
|
||||
ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter();
|
||||
adapter.setOutputChannel(channel);
|
||||
adapter.setEventTypes(Collections.<Class<? extends ApplicationEvent>>singletonList(TestApplicationEvent1.class));
|
||||
Message<?> message1 = channel.receive(0);
|
||||
assertNull(message1);
|
||||
adapter.onApplicationEvent(new TestApplicationEvent1());
|
||||
@@ -76,7 +75,8 @@ public class ApplicationEventSourceTests {
|
||||
|
||||
@Test
|
||||
public void testApplicationContextEvents() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationEventSourceTests.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"applicationEventInboundChannelAdapterTests.xml", this.getClass());
|
||||
PollableChannel channel = (PollableChannel) context.getBean("channel");
|
||||
Message<?> refreshedEventMessage = channel.receive(0);
|
||||
assertNotNull(refreshedEventMessage);
|
||||
@@ -96,6 +96,7 @@ public class ApplicationEventSourceTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestApplicationEvent1 extends ApplicationEvent {
|
||||
|
||||
public TestApplicationEvent1() {
|
||||
@@ -104,6 +105,7 @@ public class ApplicationEventSourceTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestApplicationEvent2 extends ApplicationEvent {
|
||||
|
||||
public TestApplicationEvent2() {
|
||||
@@ -29,17 +29,17 @@ import org.springframework.integration.message.StringMessage;
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ApplicationEventTargetTests {
|
||||
public class ApplicationEventOutboundChannelAdapterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testSendingEvent() throws InterruptedException {
|
||||
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
|
||||
ApplicationEventTarget adapter = new ApplicationEventTarget();
|
||||
ApplicationEventOutboundChannelAdapter adapter = new ApplicationEventOutboundChannelAdapter();
|
||||
adapter.setApplicationEventPublisher(publisher);
|
||||
assertNull(publisher.getLastEvent());
|
||||
Message<?> message = new StringMessage("testing");
|
||||
adapter.send(message);
|
||||
adapter.onMessage(message);
|
||||
ApplicationEvent event = publisher.getLastEvent();
|
||||
assertEquals(MessagingEvent.class, event.getClass());
|
||||
assertEquals(message, ((MessagingEvent) event).getMessage());
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
<bean id="channel" class="org.springframework.integration.channel.QueueChannel"/>
|
||||
|
||||
<bean id="source" class="org.springframework.integration.event.ApplicationEventSource">
|
||||
<constructor-arg ref="channel"/>
|
||||
<bean id="adapter" class="org.springframework.integration.event.ApplicationEventInboundChannelAdapter">
|
||||
<property name="outputChannel" ref="channel"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user