Merge pull request #52 from olegz/INT-2094

removed deprecated use of task-executor
This commit is contained in:
Mark Fisher
2011-09-01 17:36:23 -04:00
5 changed files with 70 additions and 101 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.integration.mail;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledFuture;
import javax.mail.FolderClosedException;
@@ -26,7 +25,6 @@ import javax.mail.MessagingException;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.scheduling.TaskScheduler;
@@ -49,50 +47,36 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport {
private volatile boolean shouldReconnectAutomatically = true;
private volatile Executor taskExecutor = new SimpleAsyncTaskExecutor();
private final ImapMailReceiver mailReceiver;
private volatile int reconnectDelay = 10000; // seconds
private volatile ScheduledFuture<?> receivingTask;
private volatile ScheduledFuture<?> pingTask;
private volatile long connectionPingInterval = 10000;
public ImapIdleChannelAdapter(ImapMailReceiver mailReceiver) {
Assert.notNull(mailReceiver, "mailReceiver must not be null");
Assert.notNull(mailReceiver, "'mailReceiver' must not be null");
this.mailReceiver = mailReceiver;
}
/**
* Specify whether the IDLE task should reconnect automatically after
* catching a {@link FolderClosedException} while waiting for messages. The
* default value is <code>true</code>.
*/
public void setShouldReconnectAutomatically(
boolean shouldReconnectAutomatically) {
public void setShouldReconnectAutomatically(boolean shouldReconnectAutomatically) {
this.shouldReconnectAutomatically = shouldReconnectAutomatically;
}
/**
* @deprecated As of release 2.0.5
* @param taskExecutor
*/
@Deprecated
public void setTaskExecutor(Executor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public String getComponentType() {
return "mail:imap-idle-channel-adapter";
}
protected void handleMailMessagingException(MessagingException e) {
if (logger.isWarnEnabled()) {
logger.warn("error occurred in idle task", e);
}
}
/*
* Lifecycle implementation
@@ -102,63 +86,56 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport {
protected void doStart() {
final TaskScheduler scheduler = this.getTaskScheduler();
Assert.notNull(scheduler, "'taskScheduler' must not be null" );
receivingTask = scheduler.schedule(new Runnable(){
public void run() {
taskExecutor.execute(new Runnable(){
public void run() {
try {
idleTask.run();
if (mailReceiver.getFolder().isOpen()){
if (logger.isDebugEnabled()){
logger.debug("Task completed successfully. Re-scheduling it again right away");
}
scheduler.schedule(this, new Date());
}
}
catch (IllegalStateException e) { //run again after a delay
logger.warn("Failed to execute IDLE task. Will atempt to resubmit in " + reconnectDelay + " milliseconds", e);
scheduler.schedule(this, new Date(System.currentTimeMillis() + reconnectDelay));
}
}
});
}
}, new Date());
pingTask = scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
Store store = mailReceiver.getStore();
if (store != null) {
store.isConnected();
}
}
catch (Exception ignore) {
}
}
}, connectionPingInterval);
this.receivingTask = scheduler.schedule(new ReceivingTask(scheduler), new Date());
this.pingTask = scheduler.scheduleAtFixedRate(new PingTask(), this.connectionPingInterval);
}
@Override
// guarded by super#lifecycleLock
protected void doStop() {
receivingTask.cancel(true);
pingTask.cancel(true);
this.receivingTask.cancel(true);
this.pingTask.cancel(true);
try {
mailReceiver.destroy();
} catch (Exception e) {
this.mailReceiver.destroy();
}
catch (Exception e) {
throw new IllegalStateException(
"Failure during the destruction of " + mailReceiver, e);
"Failure during the destruction of Mail receiver: " + mailReceiver, e);
}
}
private class ReceivingTask implements Runnable {
private final TaskScheduler scheduler;
ReceivingTask(TaskScheduler scheduler) {
this.scheduler = scheduler;
}
public void run() {
try {
idleTask.run();
if (mailReceiver.getFolder().isOpen()) {
if (logger.isDebugEnabled()) {
logger.debug("Task completed successfully. Re-scheduling it again right away.");
}
scheduler.schedule(this, new Date());
}
}
catch (IllegalStateException e) { //run again after a delay
logger.warn("Failed to execute IDLE task. Will attempt to resubmit in " + reconnectDelay + " milliseconds.", e);
scheduler.schedule(this, new Date(System.currentTimeMillis() + reconnectDelay));
}
}
}
private class IdleTask implements Runnable {
public void run() {
final TaskScheduler scheduler = getTaskScheduler();
Assert.notNull(scheduler, "'taskScheduler' must not be null" );
try {
if (logger.isDebugEnabled()) {
logger.debug("waiting for mail");
@@ -167,35 +144,43 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport {
if (mailReceiver.getFolder().isOpen()) {
Message[] mailMessages = mailReceiver.receive();
if (logger.isDebugEnabled()) {
logger.debug("received " + mailMessages.length
+ " mail messages");
logger.debug("received " + mailMessages.length + " mail messages");
}
for (Message mailMessage : mailMessages) {
final MimeMessage copied = new MimeMessage(
(MimeMessage) mailMessage);
if (taskExecutor != null) {
taskExecutor.execute(new Runnable() {
public void run() {
sendMessage(MessageBuilder.withPayload(
copied).build());
}
});
} else {
sendMessage(MessageBuilder.withPayload(copied)
.build());
}
final MimeMessage copied = new MimeMessage((MimeMessage) mailMessage);
sendMessage(MessageBuilder.withPayload(copied).build());
}
}
} catch (MessagingException e) {
ImapIdleChannelAdapter.this.handleMailMessagingException(e);
}
catch (MessagingException e) {
if (logger.isWarnEnabled()) {
logger.warn("error occurred in idle task", e);
}
if (shouldReconnectAutomatically) {
throw new IllegalStateException(
"Failure in 'idle' task. Will resubmit", e);
} else {
"Failure in 'idle' task. Will resubmit.", e);
}
else {
throw new org.springframework.integration.MessagingException(
"Failure in 'idle' task. Will NOT resubmit", e);
"Failure in 'idle' task. Will NOT resubmit.", e);
}
}
}
}
private class PingTask implements Runnable {
public void run() {
try {
Store store = mailReceiver.getStore();
if (store != null) {
store.isConnected();
}
}
catch (Exception ignore) {
}
}
}
}

View File

@@ -58,7 +58,6 @@ public class ImapIdleChannelAdapterParser extends AbstractSingleBeanDefinitionPa
builder.addConstructorArgValue(this.parseImapMailReceiver(element, parserContext));
builder.addPropertyReference("outputChannel", channel);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-channel", "errorChannel");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "task-executor");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
}

View File

@@ -104,18 +104,6 @@
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="inboundMailAdapterType">
<xsd:attribute name="task-executor" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
[DEPRECATED as of 2.0.5]
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.core.task.TaskExecutor"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="error-channel" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>

View File

@@ -63,8 +63,7 @@
channel="channel"
auto-startup="false"
java-mail-properties="javaMailProperties"
should-delete-messages="${mail.delete}"
task-executor="executor"/>
should-delete-messages="${mail.delete}"/>
<util:properties id="javaMailProperties">
<prop key="foo">bar</prop>

View File

@@ -129,9 +129,7 @@ public class ImapIdleChannelAdapterParserTests {
assertEquals(ImapIdleChannelAdapter.class, adapter.getClass());
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
Object channel = context.getBean("channel");
Object executor = context.getBean("executor");
assertSame(channel, adapterAccessor.getPropertyValue("outputChannel"));
assertSame(executor, adapterAccessor.getPropertyValue("taskExecutor"));
assertEquals(Boolean.FALSE, adapterAccessor.getPropertyValue("autoStartup"));
Object receiver = adapterAccessor.getPropertyValue("mailReceiver");
assertEquals(ImapMailReceiver.class, receiver.getClass());