PublishSubscribeChannel now wraps a provided TaskExecutor with the ErrorHandlingTaskExecutor if necessary (INT-440). Also, added namespace support for the publish-subscribe-channel's 'error-handler' reference (INT-483).

This commit is contained in:
Mark Fisher
2008-11-19 16:53:25 +00:00
parent 41460e63be
commit 85d429d29f
8 changed files with 86 additions and 43 deletions

View File

@@ -16,15 +16,24 @@
package org.springframework.integration.channel;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.dispatcher.BroadcastingDispatcher;
import org.springframework.integration.executor.ErrorHandlingTaskExecutor;
import org.springframework.integration.util.ErrorHandler;
/**
* A channel that sends Messages to each of its subscribers.
*
* @author Mark Fisher
*/
public class PublishSubscribeChannel extends AbstractSubscribableChannel<BroadcastingDispatcher> {
public class PublishSubscribeChannel extends AbstractSubscribableChannel<BroadcastingDispatcher> implements BeanFactoryAware {
private volatile TaskExecutor taskExecutor;
private volatile ErrorHandler errorHandler;
/**
* Create a PublishSubscribeChannel that will use a {@link TaskExecutor}
@@ -32,9 +41,7 @@ public class PublishSubscribeChannel extends AbstractSubscribableChannel<Broadca
*/
public PublishSubscribeChannel(TaskExecutor taskExecutor) {
super(new BroadcastingDispatcher());
if (taskExecutor != null) {
this.getDispatcher().setTaskExecutor(taskExecutor);
}
this.taskExecutor = taskExecutor;
}
public PublishSubscribeChannel() {
@@ -42,8 +49,24 @@ public class PublishSubscribeChannel extends AbstractSubscribableChannel<Broadca
}
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public void setApplySequence(boolean applySequence) {
this.getDispatcher().setApplySequence(applySequence);
}
public void setBeanFactory(BeanFactory beanFactory) {
if (this.taskExecutor != null) {
if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
if (this.errorHandler == null) {
this.errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(beanFactory));
}
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
}
this.getDispatcher().setTaskExecutor(this.taskExecutor);
}
}
}

View File

@@ -37,6 +37,7 @@ public class PublishSubscribeChannelParser extends AbstractChannelParser {
if (StringUtils.hasText(taskExecutorRef)) {
builder.addConstructorArgReference(taskExecutorRef);
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "apply-sequence");
return builder;
}

View File

@@ -111,6 +111,7 @@
<xsd:element name="interceptors" type="channelInterceptorsType" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="task-executor" type="xsd:string"/>
<xsd:attribute name="error-handler" type="xsd:string"/>
<xsd:attribute name="apply-sequence" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>

View File

@@ -148,11 +148,11 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
this.transactionManager, this.transactionDefinition);
}
this.poller = this.createPoller();
if (this.taskExecutor != null) {
if (this.taskExecutor != null && !(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
if (this.errorHandler == null) {
this.errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(getBeanFactory()));
}
this.taskExecutor = new ErrorHandlingTaskExecutor(this.errorHandler, this.taskExecutor);
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
}
this.initialized = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -13,57 +13,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.executor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.util.ErrorHandler;
import org.springframework.util.Assert;
/**
* A {@link TaskExecutor} implementation that wraps an existing TaskExecutor
* instance in order to catch any exceptions. If an exception is thrown, it
* will be handled by the provided {@link ErrorHandler}.
*
* @author Jonas Partner
*
* @author Mark Fisher
*/
public class ErrorHandlingTaskExecutor implements TaskExecutor {
private final TaskExecutor taskExecutor;
private final ErrorHandler errorHandler;
private final TaskExecutor taskExecutor;
/**
* @param errorChannel
*/
public ErrorHandlingTaskExecutor(ErrorHandler errorHandler, TaskExecutor taskExecutor) {
this.errorHandler = errorHandler;
public ErrorHandlingTaskExecutor(TaskExecutor taskExecutor, ErrorHandler errorHandler) {
Assert.notNull(taskExecutor, "taskExecutor must not be null");
Assert.notNull(errorHandler, "errorHandler must not be null");
this.taskExecutor = taskExecutor;
this.errorHandler = errorHandler;
}
public void execute(Runnable task) {
taskExecutor.execute(new ErrorHandlingRunnableWrapper(task,errorHandler));
}
private static class ErrorHandlingRunnableWrapper implements Runnable {
private final ErrorHandler errorHandler;
private final Runnable runnableTarget;
public ErrorHandlingRunnableWrapper(Runnable runnableTarget,ErrorHandler errorHandler) {
this.runnableTarget = runnableTarget;
this.errorHandler = errorHandler;
}
public void run() {
try {
runnableTarget.run();
public void execute(final Runnable task) {
this.taskExecutor.execute(new Runnable() {
public void run() {
try {
task.run();
}
catch (Throwable t) {
errorHandler.handle(t);
}
}
catch (Throwable t) {
errorHandler.handle(t);
}
}
});
}
}