This commit is contained in:
Mark Fisher
2008-10-20 11:38:00 +00:00
parent e31520bc64
commit 25d0ac78b6
20 changed files with 183 additions and 336 deletions

View File

@@ -17,9 +17,7 @@
package org.springframework.integration.bus;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import org.apache.commons.logging.Log;
@@ -63,16 +61,12 @@ public class ApplicationContextMessageBus implements MessageBus, ApplicationCont
private final Set<MessageEndpoint> endpoints = new CopyOnWriteArraySet<MessageEndpoint>();
private final MessageBusInterceptorsList interceptors = new MessageBusInterceptorsList();
private volatile TaskScheduler taskScheduler;
private volatile ApplicationContext applicationContext;
private volatile boolean autoStartup = true;
private volatile boolean initialized;
private volatile boolean running;
private final Object lifecycleMonitor = new Object();
@@ -189,13 +183,11 @@ public class ApplicationContextMessageBus implements MessageBus, ApplicationCont
}
public void start() {
if (!this.initialized) {
this.initialize();
}
if (this.running) {
return;
}
this.interceptors.preStart();
Assert.notNull(this.applicationContext, "ApplicationContext must not be null");
Assert.notNull(this.taskScheduler, "TaskScheduler must not be null");
synchronized (this.lifecycleMonitor) {
this.activateEndpoints();
if (this.taskScheduler instanceof SimpleTaskScheduler) {
@@ -207,7 +199,7 @@ public class ApplicationContextMessageBus implements MessageBus, ApplicationCont
this.taskScheduler.start();
}
this.running = true;
this.interceptors.postStart();
this.applicationContext.publishEvent(new MessageBusStartedEvent(this));
if (logger.isInfoEnabled()) {
logger.info("message bus started");
}
@@ -217,34 +209,26 @@ public class ApplicationContextMessageBus implements MessageBus, ApplicationCont
if (!this.isRunning()) {
return;
}
this.interceptors.preStop();
synchronized (this.lifecycleMonitor) {
this.deactivateEndpoints();
this.running = false;
this.taskScheduler.stop();
}
this.interceptors.postStop();
this.applicationContext.publishEvent(new MessageBusStoppedEvent(this));
if (logger.isInfoEnabled()) {
logger.info("message bus stopped");
}
}
// ApplicationListener implementation
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent && this.autoStartup) {
this.start();
}
}
private void initialize() {
synchronized (this.lifecycleMonitor) {
if (this.initialized) {
return;
}
Assert.notNull(this.applicationContext, "ApplicationContext must not be null");
Assert.notNull(this.taskScheduler, "TaskScheduler must not be null");
this.initialized = true;
}
}
// DisposableBean implementation
public void destroy() throws Exception {
this.stop();
@@ -253,61 +237,4 @@ public class ApplicationContextMessageBus implements MessageBus, ApplicationCont
}
}
public void addInterceptor(MessageBusInterceptor interceptor) {
this.interceptors.add(interceptor);
}
public void removeInterceptor(MessageBusInterceptor interceptor) {
this.interceptors.remove(interceptor);
}
public void setInterceptors(List<MessageBusInterceptor> interceptor) {
this.interceptors.set(interceptor);
}
/*
* Wrapper class for the interceptor list
*/
private class MessageBusInterceptorsList {
private CopyOnWriteArrayList<MessageBusInterceptor> messageBusInterceptors = new CopyOnWriteArrayList<MessageBusInterceptor>();
public void set(List<MessageBusInterceptor> interceptors) {
this.messageBusInterceptors.clear();
this.messageBusInterceptors.addAll(interceptors);
}
public void add(MessageBusInterceptor interceptor) {
this.messageBusInterceptors.add(interceptor);
}
public void remove(MessageBusInterceptor interceptor) {
this.messageBusInterceptors.remove(interceptor);
}
public void preStart() {
for (MessageBusInterceptor messageBusInterceptor : messageBusInterceptors) {
messageBusInterceptor.preStart(ApplicationContextMessageBus.this);
}
}
public void postStart() {
for (MessageBusInterceptor messageBusInterceptor : messageBusInterceptors) {
messageBusInterceptor.postStart(ApplicationContextMessageBus.this);
}
}
public void preStop() {
for (MessageBusInterceptor messageBusInterceptor : messageBusInterceptors) {
messageBusInterceptor.preStop(ApplicationContextMessageBus.this);
}
}
public void postStop() {
for (MessageBusInterceptor messageBusInterceptor : messageBusInterceptors) {
messageBusInterceptor.postStop(ApplicationContextMessageBus.this);
}
}
}
}

View File

@@ -16,21 +16,22 @@
package org.springframework.integration.bus;
import org.springframework.context.ApplicationEvent;
/**
* Interface for interceptors that are able be notified of the
* lifecycle of the {@link MessageBus Message Bus}.
*
* @author Marius Bogoevici
* Event raised when a <code>MessageBus</code> is started.
*
* @author Mark Fisher
*/
public interface MessageBusInterceptor {
public class MessageBusStartedEvent extends ApplicationEvent {
void preStart(MessageBus bus);
void postStart(MessageBus bus);
void preStop(MessageBus bus);
void postStop(MessageBus bus);
/**
* Create a new MessageBusStartedEvent
* @param source the <code>MessageBus</code> that has been started
* (must not be <code>null</code>)
*/
public MessageBusStartedEvent(MessageBus source) {
super(source);
}
}

View File

@@ -16,25 +16,22 @@
package org.springframework.integration.bus;
import org.springframework.context.ApplicationEvent;
/**
* No-op implementation of a {@link MessageBusInterceptor}. Subclasses shall
* override only the methods for which they intend to provide behaviour.
* Event raised when a <code>MessageBus</code> is stopped.
*
* @author Marius Bogoevici
* @author Mark Fisher
*/
public class MessageBusInterceptorAdapter implements MessageBusInterceptor {
public class MessageBusStoppedEvent extends ApplicationEvent {
public void preStart(MessageBus bus) {
/**
* Create a new MessageBusStoppedEvent
* @param source the <code>MessageBus</code> that has been stopped
* (must not be <code>null</code>)
*/
public MessageBusStoppedEvent(MessageBus source) {
super(source);
}
public void postStart(MessageBus bus) {
}
public void preStop(MessageBus bus) {
}
public void postStop(MessageBus bus) {
}
}

View File

@@ -20,18 +20,14 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
@@ -132,7 +128,6 @@ public class MessageBusParser extends AbstractSimpleBeanDefinitionParser {
AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
}
this.processChildElements(builder, element);
this.addPostProcessors(element, parserContext);
}
@@ -147,23 +142,6 @@ public class MessageBusParser extends AbstractSimpleBeanDefinitionParser {
return executor;
}
@SuppressWarnings("unchecked")
private void processChildElements(BeanDefinitionBuilder builder, Element element) {
NodeList childNodes = element.getChildNodes();
ManagedList interceptors = new ManagedList();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
if ("interceptor".equals(child.getLocalName())) {
interceptors.add(new RuntimeBeanReference(((Element)child).getAttribute("ref")));
}
}
}
if (interceptors.size() > 0) {
builder.addPropertyValue("interceptors", interceptors);
}
}
/**
* Adds extra post-processors to the context, to inject the objects configured by the MessageBus
*/

View File

@@ -23,13 +23,6 @@
Defines the Message Bus for this Application Context.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="interceptor" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="ref" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="enable-annotations" type="xsd:boolean"/>
<xsd:attribute name="task-scheduler" type="xsd:string"/>
<xsd:attribute name="auto-startup" type="xsd:boolean"/>

View File

@@ -43,8 +43,8 @@ import org.springframework.util.ClassUtils;
/**
* @author Mark Fisher
*/
public abstract class AbstractPollingEndpoint implements MessageEndpoint,
TaskSchedulerAware, Lifecycle, InitializingBean, BeanClassLoaderAware {
public abstract class AbstractPollingEndpoint extends AbstractEndpoint
implements TaskSchedulerAware, Lifecycle, InitializingBean, BeanClassLoaderAware {
public static final int MAX_MESSAGES_UNBOUNDED = -1;