From 2993f95c5259021433bf4f3d53b6d5ccb99effc1 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 19 Jan 2011 12:02:29 -0500 Subject: [PATCH] INT-1747 added the new class ResubmittingTask --- .../integration/mail/ResubmittingTask.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 spring-integration-mail/src/main/java/org/springframework/integration/mail/ResubmittingTask.java diff --git a/spring-integration-mail/src/main/java/org/springframework/integration/mail/ResubmittingTask.java b/spring-integration-mail/src/main/java/org/springframework/integration/mail/ResubmittingTask.java new file mode 100644 index 0000000000..4d4093734c --- /dev/null +++ b/spring-integration-mail/src/main/java/org/springframework/integration/mail/ResubmittingTask.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2011 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; + +import java.security.ProviderException; +import java.util.Date; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.scheduling.TaskScheduler; + +/** + * @author Mark Fisher + * @author Oleg Zhurakousky + * @since 2.0.2 + * + */ +class ResubmittingTask implements Runnable { + private static final Log logger = LogFactory.getLog(ResubmittingTask.class); + private final Runnable targetTask; + private final TaskScheduler scheduler; + private final long delay; + + public ResubmittingTask(Runnable targetTask, TaskScheduler scheduler, long delay) { + this.targetTask = targetTask; + this.scheduler = scheduler; + this.delay = delay; + } + + public void run() { + try { + targetTask.run(); + logger.debug("Task completed successfully. Re-scheduling it again right away"); + scheduler.schedule(this, new Date()); + } + catch (ProviderException e) { //run again after a delay + logger.warn("Failed to execute IDLE task. Will atempt to resubmit in " + delay + " milliseconds", e); + scheduler.schedule(this, new Date(System.currentTimeMillis() + delay)); + } + } +}