Adding new 'org.springframework.integration.event' module (INT-375).

This commit is contained in:
Mark Fisher
2008-09-17 18:15:23 +00:00
parent f7ea3f765d
commit af402eaa7b
11 changed files with 433 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-4.4.0.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-sources-4.4.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.beans/2.5.5.A/org.springframework.beans-2.5.5.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.beans/2.5.5.A/org.springframework.beans-sources-2.5.5.A.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.context/2.5.5.A/org.springframework.context-2.5.5.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.context/2.5.5.A/org.springframework.context-sources-2.5.5.A.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.core/2.5.5.A/org.springframework.core-2.5.5.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.core/2.5.5.A/org.springframework.core-sources-2.5.5.A.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.integration"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.springframework.integration.event</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="org.springframework.integration.event">
<property file="${basedir}/../build.properties"/>
<import file="${basedir}/../build-spring-integration/package-bundle.xml"/>
<import file="${basedir}/../spring-build/standard/default.xml"/>
</project>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://ivyrep.jayasoft.org/ivy-doc.xsl"?>
<ivy-module
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://incubator.apache.org/ivy/schemas/ivy.xsd"
version="1.3">
<info organisation="org.springframework.integration" module="${ant.project.name}">
<license name="Apache 2.0" url="http://www.apache.org/licenses/LICENSE-2.0"/>
<ivyauthor name="Mark Fisher"/>
</info>
<configurations>
<include file="${spring.build.dir}/common/default-ivy-configurations.xml"/>
</configurations>
<publications>
<artifact name="${ant.project.name}"/>
<artifact name="${ant.project.name}-sources" type="src" ext="jar"/>
</publications>
<dependencies>
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0" conf="test->runtime"/>
<dependency org="org.springframework.integration" name="org.springframework.integration" rev="latest.integration" conf="compile->compile"/>
<dependency org="org.springframework" name="org.springframework.context" rev="2.5.5.A" conf="compile->runtime"/>
</dependencies>
</ivy-module>

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.event;
import java.util.ArrayList;
import java.util.List;
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.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* A message source for passing Spring
* {@link ApplicationEvent ApplicationEvents} within messages.
*
* @author Mark Fisher
*/
public class ApplicationEventSource 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;
}
/**
* Set the 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.
*/
public void setEventTypes(List<Class<? extends ApplicationEvent>> eventTypes) {
Assert.notEmpty(eventTypes, "at least one event type is required");
this.eventTypes = eventTypes;
}
public void onApplicationEvent(ApplicationEvent event) {
if (CollectionUtils.isEmpty(this.eventTypes)) {
this.sendMessage(event);
return;
}
for (Class<? extends ApplicationEvent> eventType : this.eventTypes) {
if (eventType.isAssignableFrom(event.getClass())) {
this.sendMessage(event);
return;
}
}
}
private boolean sendMessage(ApplicationEvent event) {
return this.channelTemplate.send(
new GenericMessage<ApplicationEvent>(event), this.channel);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageTarget;
/**
* 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.
*
* @author Mark Fisher
*/
public class ApplicationEventTarget<T> implements MessageTarget, ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public boolean send(Message<?> message) {
this.applicationEventPublisher.publishEvent(
new MessagingEvent((Message<?>) message));
return true;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.integration.message.Message;
/**
* A subclass of {@link ApplicationEvent} that wraps a {@link Message}.
*
* @author Mark Fisher
*/
public class MessagingEvent extends ApplicationEvent {
public MessagingEvent(Message<?> message) {
super(message);
}
public Message<?> getMessage() {
return (Message<?>) this.getSource();
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.event;
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 org.junit.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class ApplicationEventSourceTests {
@Test
public void testAnyApplicationEventSentByDefault() {
QueueChannel channel = new QueueChannel();
ApplicationEventSource adapter = new ApplicationEventSource(channel);
Message<?> message1 = channel.receive(0);
assertNull(message1);
adapter.onApplicationEvent(new TestApplicationEvent1());
adapter.onApplicationEvent(new TestApplicationEvent2());
Message<?> message2 = channel.receive(20);
assertNotNull(message2);
assertEquals("event1", ((ApplicationEvent) message2.getPayload()).getSource());
Message<?> message3 = channel.receive(20);
assertNotNull(message3);
assertEquals("event2", ((ApplicationEvent) message3.getPayload()).getSource());
}
@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);
Message<?> message1 = channel.receive(0);
assertNull(message1);
adapter.onApplicationEvent(new TestApplicationEvent1());
adapter.onApplicationEvent(new TestApplicationEvent2());
Message<?> message2 = channel.receive(20);
assertNotNull(message2);
assertEquals("event1", ((ApplicationEvent) message2.getPayload()).getSource());
Message<?> message3 = channel.receive(0);
assertNull(message3);
}
@Test
public void testApplicationContextEvents() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationEventSourceTests.xml", this.getClass());
PollableChannel channel = (PollableChannel) context.getBean("channel");
Message<?> refreshedEventMessage = channel.receive(0);
assertNotNull(refreshedEventMessage);
assertEquals(ContextRefreshedEvent.class, refreshedEventMessage.getPayload().getClass());
context.start();
Message<?> startedEventMessage = channel.receive(0);
assertNotNull(startedEventMessage);
assertEquals(ContextStartedEvent.class, startedEventMessage.getPayload().getClass());
context.stop();
Message<?> stoppedEventMessage = channel.receive(0);
assertNotNull(stoppedEventMessage);
assertEquals(ContextStoppedEvent.class, stoppedEventMessage.getPayload().getClass());
context.close();
Message<?> closedEventMessage = channel.receive(0);
assertNotNull(closedEventMessage);
assertEquals(ContextClosedEvent.class, closedEventMessage.getPayload().getClass());
}
private static class TestApplicationEvent1 extends ApplicationEvent {
public TestApplicationEvent1() {
super("event1");
}
}
private static class TestApplicationEvent2 extends ApplicationEvent {
public TestApplicationEvent2() {
super("event2");
}
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.event;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class ApplicationEventTargetTests {
@Test
@SuppressWarnings("unchecked")
public void testSendingEvent() throws InterruptedException {
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
ApplicationEventTarget adapter = new ApplicationEventTarget();
adapter.setApplicationEventPublisher(publisher);
assertNull(publisher.getLastEvent());
Message<?> message = new StringMessage("testing");
adapter.send(message);
ApplicationEvent event = publisher.getLastEvent();
assertEquals(MessagingEvent.class, event.getClass());
assertEquals(message, ((MessagingEvent) event).getMessage());
}
private static class TestApplicationEventPublisher implements ApplicationEventPublisher {
private volatile ApplicationEvent lastEvent;
public ApplicationEvent getLastEvent() {
return this.lastEvent;
}
public void publishEvent(ApplicationEvent event) {
this.lastEvent = event;
}
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus" class="org.springframework.integration.bus.DefaultMessageBus"/>
<bean id="channel" class="org.springframework.integration.channel.QueueChannel"/>
<bean id="source" class="org.springframework.integration.event.ApplicationEventSource">
<constructor-arg ref="channel"/>
</bean>
</beans>

View File

@@ -0,0 +1,12 @@
Bundle-SymbolicName: org.springframework.integration.event
Bundle-Name: Spring Integration ApplicationEvent Support
Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Template:
org.springframework.integration.*;version="[1.0.0, 1.0.1)",
org.springframework.context;version="[2.5.5.A, 3.0.0)",
org.springframework.util;version="[2.5.5.A, 3.0.0)",
org.apache.commons.logging;version="[1.1.1, 2.0.0)"
Unversioned-Imports:
org.w3c.dom