use of org.springframework.integration.util.ErrorHandlingTaskExecutor in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method testInappropriateAdvice.
@Test
public void testInappropriateAdvice() throws Exception {
final AtomicBoolean called = new AtomicBoolean(false);
Advice advice = new AbstractRequestHandlerAdvice() {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
called.set(true);
return callback.execute();
}
};
PollableChannel inputChannel = new QueueChannel();
PollingConsumer consumer = new PollingConsumer(inputChannel, message -> {
});
consumer.setAdviceChain(Collections.singletonList(advice));
ExecutorService exec = Executors.newSingleThreadExecutor();
consumer.setTaskExecutor(new ErrorHandlingTaskExecutor(exec, t -> {
}));
consumer.setBeanFactory(mock(BeanFactory.class));
consumer.afterPropertiesSet();
consumer.setTaskScheduler(mock(TaskScheduler.class));
consumer.start();
Callable<?> pollingTask = TestUtils.getPropertyValue(consumer, "poller.pollingTask", Callable.class);
assertTrue(AopUtils.isAopProxy(pollingTask));
Log logger = TestUtils.getPropertyValue(advice, "logger", Log.class);
logger = spy(logger);
when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
final AtomicReference<String> logMessage = new AtomicReference<String>();
doAnswer(invocation -> {
logMessage.set(invocation.getArgument(0));
return null;
}).when(logger).warn(Mockito.anyString());
DirectFieldAccessor accessor = new DirectFieldAccessor(advice);
accessor.setPropertyValue("logger", logger);
pollingTask.call();
assertFalse(called.get());
assertNotNull(logMessage.get());
assertThat(logMessage.get(), Matchers.containsString("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint"));
consumer.stop();
exec.shutdownNow();
}
use of org.springframework.integration.util.ErrorHandlingTaskExecutor in project spring-integration by spring-projects.
the class RedisQueueInboundGateway method onInit.
@Override
protected void onInit() throws Exception {
super.onInit();
if (!this.extractPayload) {
Assert.notNull(this.serializer, "'serializer' has to be provided where 'extractPayload == false'.");
}
if (this.taskExecutor == null) {
String beanName = this.getComponentName();
this.taskExecutor = new SimpleAsyncTaskExecutor((beanName == null ? "" : beanName + "-") + this.getComponentType());
}
if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor) && this.getBeanFactory() != null) {
MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(getBeanFactory()));
errorHandler.setDefaultErrorChannel(getErrorChannel());
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, errorHandler);
}
}
use of org.springframework.integration.util.ErrorHandlingTaskExecutor in project spring-integration by spring-projects.
the class ExecutorChannel method onInit.
@Override
public final void onInit() throws Exception {
Assert.state(getDispatcher().getHandlerCount() == 0, "You cannot subscribe() until the channel " + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition");
super.onInit();
if (!(this.executor instanceof ErrorHandlingTaskExecutor)) {
ErrorHandler errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(this.getBeanFactory()));
this.executor = new ErrorHandlingTaskExecutor(this.executor, errorHandler);
}
UnicastingDispatcher unicastingDispatcher = new UnicastingDispatcher(this.executor);
unicastingDispatcher.setFailover(this.failover);
if (this.maxSubscribers == null) {
this.maxSubscribers = getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
}
unicastingDispatcher.setMaxSubscribers(this.maxSubscribers);
if (this.loadBalancingStrategy != null) {
unicastingDispatcher.setLoadBalancingStrategy(this.loadBalancingStrategy);
}
unicastingDispatcher.setMessageHandlingTaskDecorator(task -> {
if (ExecutorChannel.this.executorInterceptorsSize > 0) {
return new MessageHandlingTask(task);
} else {
return task;
}
});
this.dispatcher = unicastingDispatcher;
}
use of org.springframework.integration.util.ErrorHandlingTaskExecutor in project spring-integration by spring-projects.
the class PublishSubscribeChannel method onInit.
/**
* Callback method for initialization.
* @throws Exception the exception.
*/
@Override
public final void onInit() throws Exception {
super.onInit();
if (this.executor != null) {
Assert.state(getDispatcher().getHandlerCount() == 0, "When providing an Executor, you cannot subscribe() until the channel " + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition");
if (!(this.executor instanceof ErrorHandlingTaskExecutor)) {
if (this.errorHandler == null) {
this.errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(this.getBeanFactory()));
}
this.executor = new ErrorHandlingTaskExecutor(this.executor, this.errorHandler);
}
this.dispatcher = new BroadcastingDispatcher(this.executor);
getDispatcher().setIgnoreFailures(this.ignoreFailures);
getDispatcher().setApplySequence(this.applySequence);
getDispatcher().setMinSubscribers(this.minSubscribers);
}
if (this.maxSubscribers == null) {
Integer maxSubscribers = getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS, Integer.class);
this.setMaxSubscribers(maxSubscribers);
}
getDispatcher().setBeanFactory(this.getBeanFactory());
getDispatcher().setMessageHandlingTaskDecorator(task -> {
if (PublishSubscribeChannel.this.executorInterceptorsSize > 0) {
return new MessageHandlingTask(task);
} else {
return task;
}
});
}
use of org.springframework.integration.util.ErrorHandlingTaskExecutor in project spring-integration by spring-projects.
the class AbstractPollingEndpoint method onInit.
@Override
protected void onInit() {
synchronized (this.initializationMonitor) {
if (this.initialized) {
return;
}
Assert.notNull(this.trigger, "Trigger is required");
if (this.taskExecutor != null) {
if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
if (this.errorHandler == null) {
Assert.notNull(this.getBeanFactory(), "BeanFactory is required");
this.errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(getBeanFactory()));
this.errorHandlerIsDefault = true;
}
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
}
}
if (this.transactionSynchronizationFactory == null && this.adviceChain != null) {
if (this.adviceChain.stream().anyMatch(TransactionInterceptor.class::isInstance)) {
this.transactionSynchronizationFactory = new PassThroughTransactionSynchronizationFactory();
}
}
this.initialized = true;
}
try {
super.onInit();
} catch (Exception e) {
throw new BeanInitializationException("Cannot initialize: " + this, e);
}
}
Aggregations