-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to add single execution to scheduler interface.
- Loading branch information
Showing
18 changed files
with
163 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 0 additions & 122 deletions
122
core/src/main/java/ch/cyberduck/core/shared/AbstractSchedulerFeature.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
core/src/main/java/ch/cyberduck/core/shared/ThreadPoolSchedulerFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ch.cyberduck.core.shared; | ||
|
||
/* | ||
* Copyright (c) 2002-2017 iterate GmbH. All rights reserved. | ||
* https://cyberduck.io/ | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
|
||
import ch.cyberduck.core.PasswordCallback; | ||
import ch.cyberduck.core.exception.BackgroundException; | ||
import ch.cyberduck.core.features.Scheduler; | ||
import ch.cyberduck.core.threading.ScheduledThreadPool; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public abstract class ThreadPoolSchedulerFeature<R> implements Scheduler<R> { | ||
private static final Logger log = LogManager.getLogger(ThreadPoolSchedulerFeature.class); | ||
|
||
private final long period; | ||
private final ScheduledThreadPool scheduler = new ScheduledThreadPool(); | ||
|
||
public ThreadPoolSchedulerFeature(final long period) { | ||
this.period = period; | ||
} | ||
|
||
@Override | ||
public Future<R> repeat(final PasswordCallback callback) { | ||
return (ScheduledFuture<R>) scheduler.repeat(new FailureAwareRunnable(callback), period, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Override | ||
public Future<R> execute(final PasswordCallback callback) { | ||
return (ScheduledFuture<R>) scheduler.schedule(new FailureAwareRunnable(callback), 0L, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
protected abstract R operate(PasswordCallback callback) throws BackgroundException; | ||
|
||
@Override | ||
public void shutdown() { | ||
log.debug("Shutting down scheduler thread pool"); | ||
scheduler.shutdown(); | ||
} | ||
|
||
private final class FailureAwareRunnable implements Runnable { | ||
private final PasswordCallback callback; | ||
|
||
public FailureAwareRunnable(final PasswordCallback callback) { | ||
this.callback = callback; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
ThreadPoolSchedulerFeature.this.operate(callback); | ||
} | ||
catch(BackgroundException e) { | ||
log.warn("Failure processing scheduled task. {}", e.getMessage(), e); | ||
} | ||
catch(Exception e) { | ||
log.error("Failure processing scheduled task {}", e.getMessage(), e); | ||
ThreadPoolSchedulerFeature.this.shutdown(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.