diff --git a/docs/src/reference/docbook/event.xml b/docs/src/reference/docbook/event.xml index 3a9546edbf..cc6336d97a 100644 --- a/docs/src/reference/docbook/event.xml +++ b/docs/src/reference/docbook/event.xml @@ -13,21 +13,20 @@ Receiving Spring ApplicationEvents To receive events and send them to a channel, simply define an instance of Spring Integration's - ApplicationEventListeningChannelAdapter. This class is an implementation of + ApplicationEventListeningMessageProducer. This class is an implementation of Spring's ApplicationListener interface. By default it will pass all received events as Spring Integration Messages. To limit based on the type of event, configure the list of event types that you want to receive with the 'eventTypes' property. - For convenience namespace support was provided to configureĀ ApplicationEventListeningChannelAdapter via inbound-channel-adapter - + For convenience namespace support was provided to configureĀ ApplicationEventListeningMessageProducer via inbound-channel-adapter + -]]> -In the above sample, all Application Context events that are of type specified by the 'event-types' (optional) attribute will be -delivered as Spring Integration Messages to 'sampleEventChannel'. +]]> +In the above example, all Application Context events that match one of the types specified by the 'event-types' (optional) attribute will be +delivered as Spring Integration Messages to 'eventChannel'. -
@@ -41,21 +40,21 @@ delivered as Spring Integration Messages to 'sampleEventChannel'. For convenience namespace support was provided to configureĀ ApplicationEventPublishingMessageHandler via outbound-channel-adapter element - + -]]> -If you are using PollableChannel (e.g., Queue), you can also provide poller as sub-element of outbound-channel-adapter, optionally providing task-executor - +]]> +If you are using a PollableChannel (e.g., Queue), you can also provide poller as a sub-element of the outbound-channel-adapter element. You can also optionally provide a task-executor reference for that poller. + - + ]]> -In the above sample, all messages sent to an 'input' channel will be published as ApplicationEvents to Spring Application sContext +In the above example, all messages sent to the 'eventChannel' channel will be published as ApplicationEvents to any relevant ApplicationListeners within the Spring ApplicationContext. If the payload of the Message is an ApplicationEvent, it will be passed as-is. Otherwise the Message itself will be wrapped in a MessagingEvent instance.
diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java index 7eb6eb3340..835f2e4618 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java @@ -57,6 +57,22 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements Assert.notNull(this.outputChannel, "outputChannel is required"); } + /** + * Takes no action by default. Subclasses may override this if they + * need lifecycle-managed behavior. + */ + @Override + protected void doStart() { + } + + /** + * Takes no action by default. Subclasses may override this if they + * need lifecycle-managed behavior. + */ + @Override + protected void doStop() { + } + protected void sendMessage(Message message) { if (message == null) { throw new MessagingException("cannot send a null message"); diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java index 37d924f60c..6fddae8854 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java @@ -16,23 +16,25 @@ package org.springframework.integration.event.config; +import org.w3c.dom.Element; + import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; -import org.springframework.integration.event.ApplicationEventInboundChannelAdapter; -import org.w3c.dom.Element; /** * @author Oleg Zhurakousky + * @author Mark Fisher * @since 2.0 */ -public class EventInboundChannelAdapterParser extends AbstractChannelAdapterParser{ +public class EventInboundChannelAdapterParser extends AbstractChannelAdapterParser { @Override protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) { - BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.rootBeanDefinition(ApplicationEventInboundChannelAdapter.class); + BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.rootBeanDefinition( + "org.springframework.integration.event.inbound.ApplicationEventListeningMessageProducer"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(adapterBuilder, element, "channel", "outputChannel"); IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, element, "event-types"); IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, element, "payload-expression"); diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java index dea502859f..bb049925db 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParser.java b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParser.java index 6dea1cd10e..8e2cd0a5e5 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParser.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParser.java @@ -13,44 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.event.config; +import org.w3c.dom.Element; + import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; -import org.springframework.integration.event.ApplicationEventPublishingMessageHandler; -import org.w3c.dom.Element; /** * @author Oleg Zhurakousky * @since 2.0 */ public class EventOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser{ - @Override - protected AbstractBeanDefinition parseConsumer(Element element, - ParserContext parserContext) { - BeanDefinitionBuilder invokerBuilder = BeanDefinitionBuilder.genericBeanDefinition(ApplicationEventPublishingMessageHandler.class); -// BeanComponentDefinition innerHandlerDefinition = -// IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext); -// if (innerHandlerDefinition == null){ -// Assert.hasText(element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE), -// "You must provide 'ref' attribute or register inner bean for " + -// "Outbound Channel consumer."); -// invokerBuilder.addConstructorArgReference(element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE)); -// } else { -// invokerBuilder.addConstructorArgValue(innerHandlerDefinition); -// } -// invokerBuilder.addConstructorArgValue(element.getAttribute(IntegrationNamespaceUtils.METHOD_ATTRIBUTE)); -// String order = element.getAttribute(IntegrationNamespaceUtils.ORDER); -// if (StringUtils.hasText(order)) { -// invokerBuilder.addPropertyValue(IntegrationNamespaceUtils.ORDER, order); -// } - return invokerBuilder.getBeanDefinition(); + protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler"); + return builder.getBeanDefinition(); } - - } diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/MessagingEvent.java b/spring-integration-event/src/main/java/org/springframework/integration/event/core/MessagingEvent.java similarity index 95% rename from spring-integration-event/src/main/java/org/springframework/integration/event/MessagingEvent.java rename to spring-integration-event/src/main/java/org/springframework/integration/event/core/MessagingEvent.java index 8be2fe1eb5..a146355d28 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/MessagingEvent.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/core/MessagingEvent.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.event; +package org.springframework.integration.event.core; import org.springframework.context.ApplicationEvent; import org.springframework.integration.Message; diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java b/spring-integration-event/src/main/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducer.java similarity index 80% rename from spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java rename to spring-integration-event/src/main/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducer.java index 41f0b99773..41d1a1ad61 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducer.java @@ -14,13 +14,14 @@ * limitations under the License. */ -package org.springframework.integration.event; +package org.springframework.integration.event.inbound; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationContextEvent; import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.endpoint.MessageProducerSupport; @@ -31,16 +32,18 @@ import org.springframework.util.CollectionUtils; /** * An inbound Channel Adapter that passes Spring {@link ApplicationEvent ApplicationEvents} within messages. * If a {@link #setPayloadExpression(String) payloadExpression} is provided, it will be evaluated against - * the ApplicationEvent instance to create the Message payload. + * the ApplicationEvent instance to create the Message payload. Otherwise, the event itself will be the payload. * * @author Mark Fisher */ -public class ApplicationEventInboundChannelAdapter extends MessageProducerSupport implements ApplicationListener { +public class ApplicationEventListeningMessageProducer extends MessageProducerSupport implements ApplicationListener { private final Set> eventTypes = new CopyOnWriteArraySet>(); private volatile Expression payloadExpression; + private volatile boolean active; + private final SpelExpressionParser parser = new SpelExpressionParser(); @@ -77,29 +80,33 @@ public class ApplicationEventInboundChannelAdapter extends MessageProducerSuppor } public void onApplicationEvent(ApplicationEvent event) { - if (CollectionUtils.isEmpty(this.eventTypes)) { - this.sendEventAsMessage(event); - return; - } - for (Class eventType : this.eventTypes) { - if (eventType.isAssignableFrom(event.getClass())) { + if (this.active || event instanceof ApplicationContextEvent) { + if (CollectionUtils.isEmpty(this.eventTypes)) { this.sendEventAsMessage(event); return; } + for (Class eventType : this.eventTypes) { + if (eventType.isAssignableFrom(event.getClass())) { + this.sendEventAsMessage(event); + return; + } + } } } + @Override + protected void doStart() { + this.active = true; + } + + @Override + protected void doStop() { + this.active = false; + } + private void sendEventAsMessage(ApplicationEvent event) { Object payload = (this.payloadExpression != null) ? this.payloadExpression.getValue(event) : event; this.sendMessage(MessageBuilder.withPayload(payload).build()); } - @Override - protected void doStart() { - } - - @Override - protected void doStop() { - } - } diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventPublishingMessageHandler.java b/spring-integration-event/src/main/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandler.java similarity index 77% rename from spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventPublishingMessageHandler.java rename to spring-integration-event/src/main/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandler.java index 2959c27761..2e4236dadb 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventPublishingMessageHandler.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandler.java @@ -14,12 +14,13 @@ * limitations under the License. */ -package org.springframework.integration.event; +package org.springframework.integration.event.outbound; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.integration.Message; +import org.springframework.integration.event.core.MessagingEvent; import org.springframework.integration.handler.AbstractMessageHandler; import org.springframework.util.Assert; @@ -31,7 +32,7 @@ import org.springframework.util.Assert; * * @author Mark Fisher */ -public class ApplicationEventPublishingMessageHandler extends AbstractMessageHandler implements ApplicationEventPublisherAware { +public class ApplicationEventPublishingMessageHandler extends AbstractMessageHandler implements ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; @@ -43,7 +44,12 @@ public class ApplicationEventPublishingMessageHandler extends AbstractMessage @Override protected void handleMessageInternal(Message message) { Assert.notNull(this.applicationEventPublisher, "applicationEventPublisher is required"); - this.applicationEventPublisher.publishEvent(new MessagingEvent(message)); + if (message.getPayload() instanceof ApplicationEvent) { + this.applicationEventPublisher.publishEvent((ApplicationEvent) message.getPayload()); + } + else { + this.applicationEventPublisher.publishEvent(new MessagingEvent(message)); + } } } diff --git a/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd b/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd index c57993c7fe..eedb92b4c7 100644 --- a/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd +++ b/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd @@ -1,104 +1,98 @@ + xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" + xmlns:tool="http://www.springframework.org/schema/tool" + xmlns:integration="http://www.springframework.org/schema/integration" + targetNamespace="http://www.springframework.org/schema/integration/event" + elementFormDefault="qualified" attributeFormDefault="unqualified"> - - + + - - + - + - - - - Configures an inbound Channel Adapter which listens for an Application Context events, converts them to - Messages and sends them to a 'channel' - - - - - - - - - - - - - Identifies inbound 'channel' which accepts Messages generated from Application Context events. - - - - - - - Comma delimited list of event types (classes that extend ApplicationEvent) that - this adapter should send to the message channel. By default, all event - types will be sent [OPTIONAL] - - - - - - - - - - - - + - Defines a Channel Adapter that receives from a MessageChannel and passes to - a method-invoking - MessageHandler. + Configures an inbound Channel Adapter which listens for Application Context + events, converts them to Messages and sends them to a Message Channel. + + + + + + + + + + + + + The channel to which Messages generated from Application Context events will be sent. + + + + + + + Comma delimited list of event types (classes that extend ApplicationEvent) that this adapter + should send to the message channel. By default, all event types will be sent [OPTIONAL] + + + + + + + + + + + + + + + + Defines a Channel Adapter that receives Messages from a MessageChannel and then publishes + MessagingEvents containing those Messages. - - - - - - Specifies the order for invocation when this endpoint is connected as a - subscriber to a - SubscribableChannel. - - - - - + + + + + + + + + + + + The Message Channel from which this adapter receives Messages. + + + + + + + + Specifies the order for invocation when this endpoint is connected as a + subscriber to a SubscribableChannel. + + + + - - - - - - - - - - - - - - - - - diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java index d1e5d9eebc..6eff5c7cea 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java @@ -36,7 +36,7 @@ import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.expression.Expression; import org.springframework.integration.Message; import org.springframework.integration.core.PollableChannel; -import org.springframework.integration.event.ApplicationEventInboundChannelAdapter; +import org.springframework.integration.event.inbound.ApplicationEventListeningMessageProducer; import org.springframework.integration.history.MessageHistory; import org.springframework.integration.test.util.TestUtils; import org.springframework.test.context.ContextConfiguration; @@ -59,7 +59,7 @@ public class EventInboundChannelAdapterParserTests { public void validateEventParser() { Object adapter = context.getBean("eventAdapterSimple"); Assert.assertNotNull(adapter); - Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter); + Assert.assertTrue(adapter instanceof ApplicationEventListeningMessageProducer); DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); Assert.assertEquals(context.getBean("input"), adapterAccessor.getPropertyValue("outputChannel")); } @@ -69,7 +69,7 @@ public class EventInboundChannelAdapterParserTests { public void validateEventParserWithEventTypes() { Object adapter = context.getBean("eventAdapterFiltered"); Assert.assertNotNull(adapter); - Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter); + Assert.assertTrue(adapter instanceof ApplicationEventListeningMessageProducer); DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); Assert.assertEquals(context.getBean("inputFiltered"), adapterAccessor.getPropertyValue("outputChannel")); Set> eventTypes = (Set>) adapterAccessor.getPropertyValue("eventTypes"); @@ -84,7 +84,7 @@ public class EventInboundChannelAdapterParserTests { public void validateEventParserWithEventTypesAndPlaceholder() { Object adapter = context.getBean("eventAdapterFilteredPlaceHolder"); Assert.assertNotNull(adapter); - Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter); + Assert.assertTrue(adapter instanceof ApplicationEventListeningMessageProducer); DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); Assert.assertEquals(context.getBean("inputFilteredPlaceHolder"), adapterAccessor.getPropertyValue("outputChannel")); Set> eventTypes = (Set>) adapterAccessor.getPropertyValue("eventTypes"); @@ -113,7 +113,7 @@ public class EventInboundChannelAdapterParserTests { public void validatePayloadExpression() { Object adapter = context.getBean("eventAdapterSpel"); Assert.assertNotNull(adapter); - Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter); + Assert.assertTrue(adapter instanceof ApplicationEventListeningMessageProducer); DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); Expression expression = (Expression) adapterAccessor.getPropertyValue("payloadExpression"); Assert.assertEquals("source + '-test'", expression.getExpressionString()); diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTests.java b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTests.java index 32cb017381..caa379e49f 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTests.java +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTests.java @@ -16,6 +16,7 @@ package org.springframework.integration.event.config; +import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import junit.framework.Assert; @@ -34,7 +35,7 @@ import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.endpoint.EventDrivenConsumer; -import org.springframework.integration.event.ApplicationEventPublishingMessageHandler; +import org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler; import org.springframework.integration.message.GenericMessage; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -46,13 +47,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class EventOutboundChannelAdapterParserTests { + @Autowired - private ConfigurableApplicationContext context; - - private boolean recievedEvent; - + private volatile ConfigurableApplicationContext context; + + private volatile boolean receivedEvent; + + @Test - public void validateEventParser(){ + public void validateEventParser() { EventDrivenConsumer adapter = context.getBean("eventAdapter", EventDrivenConsumer.class); Assert.assertNotNull(adapter); DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); @@ -60,16 +63,16 @@ public class EventOutboundChannelAdapterParserTests { Assert.assertTrue(handler instanceof ApplicationEventPublishingMessageHandler); Assert.assertEquals(context.getBean("input"), adapterAccessor.getPropertyValue("inputChannel")); } + @Test - public void validateUsage(){ - - ApplicationListener listener = new ApplicationListener() { + public void validateUsage() { + ApplicationListener listener = new ApplicationListener() { public void onApplicationEvent(ApplicationEvent event) { Object source = event.getSource(); if (source instanceof Message){ - String payload = (String) ((Message)source).getPayload(); - if (payload.equals("hello")){ - recievedEvent = true; + String payload = (String) ((Message) source).getPayload(); + if (payload.equals("hello")) { + receivedEvent = true; } } } @@ -77,34 +80,38 @@ public class EventOutboundChannelAdapterParserTests { context.addApplicationListener(listener); DirectChannel channel = context.getBean("input", DirectChannel.class); channel.send(new GenericMessage("hello")); - Assert.assertTrue(recievedEvent); + Assert.assertTrue(receivedEvent); } - + @Test(timeout=2000) public void validateUsageWithPollableChannel() throws Exception { - ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("EventOutboundChannelAdapterParserTestsWithPollable-context.xml", EventOutboundChannelAdapterParserTests.class); + ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("EventOutboundChannelAdapterParserTestsWithPollable-context.xml", EventOutboundChannelAdapterParserTests.class); final CyclicBarrier barier = new CyclicBarrier(2); - ApplicationListener listener = new ApplicationListener() { + ApplicationListener listener = new ApplicationListener() { public void onApplicationEvent(ApplicationEvent event) { Object source = event.getSource(); if (source instanceof Message){ - String payload = (String) ((Message)source).getPayload(); + String payload = (String) ((Message) source).getPayload(); if (payload.equals("hello")){ - recievedEvent = true; + receivedEvent = true; try { barier.await(); - } catch (Exception e) { - e.printStackTrace(); - } + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + catch (BrokenBarrierException e) { + throw new IllegalStateException("broken barrier", e); + } } } } }; - ac.addApplicationListener(listener); - QueueChannel channel = ac.getBean("input", QueueChannel.class); + context.addApplicationListener(listener); + QueueChannel channel = context.getBean("input", QueueChannel.class); channel.send(new GenericMessage("hello")); barier.await(); - Assert.assertTrue(recievedEvent); + Assert.assertTrue(receivedEvent); } - + } diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTestsWithPollable-context.xml b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTestsWithPollable-context.xml index c8321ad960..086598d2cc 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTestsWithPollable-context.xml +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTestsWithPollable-context.xml @@ -15,9 +15,7 @@ - - - + diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java b/spring-integration-event/src/test/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducerTests.java similarity index 89% rename from spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java rename to spring-integration-event/src/test/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducerTests.java index e044983b9e..6664d3eb2c 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducerTests.java @@ -14,9 +14,14 @@ * limitations under the License. */ -package org.springframework.integration.event; +package org.springframework.integration.event.inbound; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import org.junit.Test; + import org.springframework.context.ApplicationEvent; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; @@ -26,21 +31,19 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.core.PollableChannel; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import org.springframework.integration.event.inbound.ApplicationEventListeningMessageProducer; /** * @author Mark Fisher */ -public class ApplicationEventInboundChannelAdapterTests { +public class ApplicationEventListeningMessageProducerTests { @Test public void anyApplicationEventSentByDefault() { QueueChannel channel = new QueueChannel(); - ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter(); + ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer(); adapter.setOutputChannel(channel); + adapter.start(); Message message1 = channel.receive(0); assertNull(message1); adapter.onApplicationEvent(new TestApplicationEvent1()); @@ -57,9 +60,10 @@ public class ApplicationEventInboundChannelAdapterTests { @SuppressWarnings("unchecked") public void onlyConfiguredEventTypesAreSent() { QueueChannel channel = new QueueChannel(); - ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter(); + ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer(); adapter.setOutputChannel(channel); adapter.setEventTypes(new Class[]{TestApplicationEvent1.class}); + adapter.start(); Message message1 = channel.receive(0); assertNull(message1); adapter.onApplicationEvent(new TestApplicationEvent1()); @@ -96,9 +100,10 @@ public class ApplicationEventInboundChannelAdapterTests { @Test public void payloadExpressionEvaluatedAgainstApplicationEvent() { QueueChannel channel = new QueueChannel(); - ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter(); + ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer(); adapter.setPayloadExpression("'received: ' + source"); adapter.setOutputChannel(channel); + adapter.start(); Message message1 = channel.receive(0); assertNull(message1); adapter.onApplicationEvent(new TestApplicationEvent1()); @@ -118,8 +123,6 @@ public class ApplicationEventInboundChannelAdapterTests { public TestApplicationEvent1() { super("event1"); } - - } diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml b/spring-integration-event/src/test/java/org/springframework/integration/event/inbound/applicationEventInboundChannelAdapterTests.xml similarity index 92% rename from spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml rename to spring-integration-event/src/test/java/org/springframework/integration/event/inbound/applicationEventInboundChannelAdapterTests.xml index c99b9a81d7..996da5c8cb 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/inbound/applicationEventInboundChannelAdapterTests.xml @@ -9,7 +9,7 @@ - + diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventPublishingMessageHandlerTests.java b/spring-integration-event/src/test/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandlerTests.java similarity index 65% rename from spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventPublishingMessageHandlerTests.java rename to spring-integration-event/src/test/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandlerTests.java index cc05728ea8..41ce0c4d9b 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventPublishingMessageHandlerTests.java +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandlerTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.event; +package org.springframework.integration.event.outbound; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -24,6 +24,8 @@ import org.junit.Test; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.integration.Message; +import org.springframework.integration.event.core.MessagingEvent; +import org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler; import org.springframework.integration.message.GenericMessage; /** @@ -32,8 +34,7 @@ import org.springframework.integration.message.GenericMessage; public class ApplicationEventPublishingMessageHandlerTests { @Test - @SuppressWarnings("unchecked") - public void testSendingEvent() throws InterruptedException { + public void messagingEvent() throws InterruptedException { TestApplicationEventPublisher publisher = new TestApplicationEventPublisher(); ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler(); handler.setApplicationEventPublisher(publisher); @@ -45,6 +46,19 @@ public class ApplicationEventPublishingMessageHandlerTests { assertEquals(message, ((MessagingEvent) event).getMessage()); } + @Test + public void payloadAsEvent() { + TestApplicationEventPublisher publisher = new TestApplicationEventPublisher(); + ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler(); + handler.setApplicationEventPublisher(publisher); + assertNull(publisher.getLastEvent()); + Message message = new GenericMessage(new TestEvent("foo")); + handler.handleMessage(message); + ApplicationEvent event = publisher.getLastEvent(); + assertEquals(TestEvent.class, event.getClass()); + assertEquals("foo", ((TestEvent) event).getSource()); + } + private static class TestApplicationEventPublisher implements ApplicationEventPublisher { @@ -59,4 +73,13 @@ public class ApplicationEventPublishingMessageHandlerTests { } } + + @SuppressWarnings("serial") + private static class TestEvent extends ApplicationEvent { + + public TestEvent(String text) { + super(text); + } + } + }