INT-3099 Add IMAP Idle Application Events

Allow an application to be informed of problems on the IMAP idle
thread by emitting an event containing the exception.

Introduce IntegrationApplicationEvent hierarchy for all
events emitted by SI components.

INT-3099 Polishing (PR Comments)

Separate TCP events into discrete subclasses.
This commit is contained in:
Gary Russell
2013-08-11 14:34:47 -04:00
parent 754273b0a0
commit 5cbdfe9e43
30 changed files with 516 additions and 154 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2013 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.
@@ -29,9 +29,13 @@ import javax.mail.MessagingException;
import javax.mail.Store;
import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.mail.event.MailIntegrationEvent;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.transaction.IntegrationResourceHolder;
import org.springframework.integration.transaction.TransactionSynchronizationFactory;
@@ -54,7 +58,8 @@ import org.springframework.util.CollectionUtils;
* @author Oleg Zhurakousky
* @author Gary Russell
*/
public class ImapIdleChannelAdapter extends MessageProducerSupport implements BeanClassLoaderAware {
public class ImapIdleChannelAdapter extends MessageProducerSupport implements BeanClassLoaderAware,
ApplicationEventPublisherAware {
private final IdleTask idleTask = new IdleTask();
@@ -82,6 +87,8 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
private volatile TransactionSynchronizationFactory transactionSynchronizationFactory;
private volatile ApplicationEventPublisher applicationEventPublisher;
public ImapIdleChannelAdapter(ImapMailReceiver mailReceiver) {
Assert.notNull(mailReceiver, "'mailReceiver' must not be null");
this.mailReceiver = mailReceiver;
@@ -126,6 +133,11 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
this.classLoader = classLoader;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
/*
* Lifecycle implementation
*/
@@ -174,6 +186,7 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
catch (Exception e) { //run again after a delay
logger.warn("Failed to execute IDLE task. Will attempt to resubmit in " + reconnectDelay + " milliseconds.", e);
receivingTaskTrigger.delayNextExecution();
ImapIdleChannelAdapter.this.publishException(e);
}
}
}
@@ -256,6 +269,17 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
return sendingTask;
}
private void publishException(Exception e) {
if (this.applicationEventPublisher != null) {
this.applicationEventPublisher.publishEvent(new ImapIdleExceptionEvent(e));
}
else {
if (logger.isDebugEnabled()) {
logger.debug("No application event publisher for exception: " + e.getMessage());
}
}
}
private class PingTask implements Runnable {
public void run() {
@@ -289,4 +313,14 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
this.delayNextExecution = true;
}
}
public class ImapIdleExceptionEvent extends MailIntegrationEvent {
private static final long serialVersionUID = -5875388810251967741L;
public ImapIdleExceptionEvent(Exception e) {
super(ImapIdleChannelAdapter.this, e);
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2013 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.mail.event;
import org.springframework.integration.event.IntegrationEvent;
/**
* @author Gary Russell
* @since 3.0
*
*/
@SuppressWarnings("serial")
public abstract class MailIntegrationEvent extends IntegrationEvent {
public MailIntegrationEvent(Object source) {
super(source);
}
public MailIntegrationEvent(Object source, Throwable cause) {
super(source, cause);
}
}

View File

@@ -0,0 +1,4 @@
/**
* Events generated by the mail module
*/
package org.springframework.integration.mail.event;

View File

@@ -36,6 +36,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
@@ -53,8 +54,11 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.expression.Expression;
@@ -64,6 +68,7 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.mail.ImapIdleChannelAdapter.ImapIdleExceptionEvent;
import org.springframework.integration.mail.config.ImapIdleChannelAdapterParserTests;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -612,6 +617,29 @@ public class ImapMailReceiverTests {
adapter.stop();
}
@Test
public void testConnectionException() throws Exception {
ImapMailReceiver mailReceiver = new ImapMailReceiver("imap:foo");
ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(mailReceiver);
final AtomicReference<ImapIdleExceptionEvent> theEvent = new AtomicReference<ImapIdleExceptionEvent>();
final CountDownLatch latch = new CountDownLatch(1);
adapter.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(ApplicationEvent event) {
assertNull("only one event expected", theEvent.get());
theEvent.set((ImapIdleExceptionEvent) event);
latch.countDown();
}
});
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
adapter.setTaskScheduler(taskScheduler);
adapter.start();
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertTrue(theEvent.get().toString().endsWith("cause=java.lang.IllegalStateException: Failure in 'idle' task. Will resubmit.]"));
}
@Test // see INT-1801
public void testImapLifecycleForRaceCondition() throws Exception{

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -15,6 +15,7 @@
*/
package org.springframework.integration.mail.config;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
@@ -44,13 +45,13 @@ import org.springframework.util.ReflectionUtils;
public class ImapIdelIntegrationTests {
@Test
//@Ignore
public void testWithTransactionSynchronization() throws Exception{
final AtomicBoolean block = new AtomicBoolean(false);
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("imap-idle-mock-integration-config.xml", this.getClass());
PostTransactionProcessor processor = context.getBean("syncProcessor", PostTransactionProcessor.class);
ImapIdleChannelAdapter adapter = context.getBean("customAdapter", ImapIdleChannelAdapter.class);
assertNotNull(TestUtils.getPropertyValue(adapter, "applicationEventPublisher"));
ImapMailReceiver receiver = TestUtils.getPropertyValue(adapter, "mailReceiver", ImapMailReceiver.class);
// setup mock scenario