-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to globally set the scheduling mode to 'manual'
This commit provides the functionality for parameterized GuiceAsync initialization. When setting the respective flag, all @scheduled methods behave as if they were annotated with @ManuallyScheduled #7
- Loading branch information
Showing
18 changed files
with
378 additions
and
98 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/it/java/de/skuzzle/inject/async/ManuallyScheduledIT.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,86 @@ | ||
package de.skuzzle.inject.async; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import org.junit.Test; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Inject; | ||
|
||
import de.skuzzle.inject.async.guice.GuiceAsync; | ||
import de.skuzzle.inject.async.guice.ScheduleFeature; | ||
import de.skuzzle.inject.async.schedule.ScheduleProperties; | ||
import de.skuzzle.inject.async.schedule.SchedulingService; | ||
import de.skuzzle.inject.async.schedule.annotation.Scheduled; | ||
import de.skuzzle.inject.async.schedule.annotation.SimpleTrigger; | ||
|
||
public class ManuallyScheduledIT { | ||
|
||
private volatile CountDownLatch manualLatch = new CountDownLatch(2); | ||
private volatile int counterManual; | ||
|
||
@Inject | ||
private SchedulingService schedulingService; | ||
|
||
@Scheduled | ||
@SimpleTrigger(100) | ||
public void testManual() { | ||
++counterManual; | ||
manualLatch.countDown(); | ||
} | ||
|
||
@Test | ||
public void testManuallyStart() throws Exception { | ||
Guice.createInjector(new AbstractModule() { | ||
|
||
@Override | ||
protected void configure() { | ||
final ScheduleProperties disableAutoScheduling = ScheduleProperties.defaultProperties() | ||
.disableAutoScheduling(); | ||
|
||
GuiceAsync.enableFeaturesFor(binder(), | ||
ScheduleFeature.withProperties(disableAutoScheduling)); | ||
} | ||
}).injectMembers(this); | ||
|
||
schedulingService.startManualScheduling(); | ||
manualLatch.await(); | ||
assertTrue(counterManual > 0); | ||
} | ||
|
||
@Test | ||
public void testDoNotManuallyStart() throws Exception { | ||
Guice.createInjector(new AbstractModule() { | ||
|
||
@Override | ||
protected void configure() { | ||
final ScheduleProperties disableAutoScheduling = ScheduleProperties.defaultProperties() | ||
.disableAutoScheduling(); | ||
|
||
GuiceAsync.enableFeaturesFor(binder(), | ||
ScheduleFeature.withProperties(disableAutoScheduling)); | ||
} | ||
}).injectMembers(this); | ||
|
||
// this thing waits forever because scheduler is never started | ||
final Thread waitForTimeout = new Thread(() -> { | ||
try { | ||
manualLatch.await(); | ||
// XXX: this would not fail the test because it occurs in wrong thread | ||
fail(); | ||
} catch (final InterruptedException ignore) { | ||
} | ||
}); | ||
|
||
waitForTimeout.start(); | ||
Thread.sleep(1000); | ||
waitForTimeout.interrupt(); | ||
assertEquals(0, counterManual); | ||
} | ||
|
||
} |
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
72 changes: 72 additions & 0 deletions
72
src/main/java/de/skuzzle/inject/async/guice/DefaultFeatures.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,72 @@ | ||
package de.skuzzle.inject.async.guice; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Injector; | ||
|
||
import de.skuzzle.inject.async.methods.AsyncModule; | ||
import de.skuzzle.inject.async.methods.annotation.Async; | ||
import de.skuzzle.inject.async.schedule.ScheduleModule; | ||
import de.skuzzle.inject.async.schedule.ScheduleProperties; | ||
import de.skuzzle.inject.async.schedule.annotation.Scheduled; | ||
|
||
/** | ||
* Supported features that can be passed when initializing the async/scheduling subsystem. | ||
* Each feature is self contained and has no dependence to other features being present. | ||
* | ||
* @author Simon Taddiken | ||
* @since 2.0.0 | ||
*/ | ||
public enum DefaultFeatures implements Feature { | ||
/** This feature enables handling of the {@link Async} annotation. */ | ||
ASYNC { | ||
@Override | ||
public void installModuleTo(Binder binder, GuiceAsync principal) { | ||
binder.install(new AsyncModule(principal)); | ||
} | ||
|
||
@Override | ||
public boolean cleanupExecutor(Injector injector, long timeout, TimeUnit timeUnit) { | ||
final ExecutorService executor = injector.getInstance(Keys.DEFAULT_EXECUTOR_KEY); | ||
if (!Shutdown.executor(executor, timeout, timeUnit)) { | ||
LOG.warn("There are still active tasks lingering in default executor after shutdown. Wait time: {} {}", | ||
timeout, timeUnit); | ||
return false; | ||
} | ||
return true; | ||
} | ||
}, | ||
/** | ||
* This feature enables handling of the {@link Scheduled} annotation. You may also use | ||
* an instance of {@link ScheduleFeature} instead of this instance (but better do not | ||
* provide both). | ||
*/ | ||
SCHEDULE { | ||
@Override | ||
public void installModuleTo(Binder binder, GuiceAsync principal) { | ||
binder.install(new ScheduleModule( | ||
principal, | ||
ScheduleProperties.defaultProperties())); | ||
} | ||
|
||
@Override | ||
public boolean cleanupExecutor(Injector injector, long timeout, TimeUnit timeUnit) { | ||
final ScheduledExecutorService scheduler = injector.getInstance(Keys.DEFAULT_SCHEDULER_KEY); | ||
if (!Shutdown.executor(scheduler, timeout, timeUnit)) { | ||
LOG.warn("There are still active tasks lingering in default scheduler after shutdown. Wait time: {} {}", | ||
timeout, timeUnit); | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DefaultFeatures.class); | ||
|
||
} |
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 |
---|---|---|
@@ -1,17 +1,37 @@ | ||
package de.skuzzle.inject.async.guice; | ||
|
||
import de.skuzzle.inject.async.methods.annotation.Async; | ||
import de.skuzzle.inject.async.schedule.annotation.Scheduled; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Injector; | ||
|
||
/** | ||
* Supported features that can be passed when initializing the async/scheduling subsystem. | ||
* Each feature is self contained and has no dependence to other features being present. | ||
* A stand alone feature that can be passed to {@link GuiceAsync}. Use | ||
* {@link DefaultFeatures} or the dedicated {@link ScheduleFeature} which allows | ||
* customization. | ||
* | ||
* @author Simon Taddiken | ||
* @since 2.0.0 | ||
*/ | ||
public enum Feature { | ||
/** This feature enables handling of the {@link Async} annotation. */ | ||
ASYNC, | ||
/** This feature enables handling of the {@link Scheduled} annotation. */ | ||
SCHEDULE | ||
public interface Feature { | ||
|
||
/** | ||
* Installs the modules relevant to this feature to the given {@link Binder}. | ||
* | ||
* @param binder The binder to install any required modules to. | ||
* @param principal The {@link GuiceAsync} instance guarding the modules from | ||
* unintended instantiation. | ||
*/ | ||
void installModuleTo(Binder binder, GuiceAsync principal); | ||
|
||
/** | ||
* Makes sure to shutdown this feature's executor when | ||
* {@link GuiceAsyncService#shutdown(long, TimeUnit)} is being called. | ||
* | ||
* @param injector The injector. | ||
* @param timeout The time to wait for an orderly shutdown. | ||
* @param timeUnit Unit for the timeout parameter. | ||
* @return Whether the executor orderly shutdown within given time. | ||
*/ | ||
boolean cleanupExecutor(Injector injector, long timeout, TimeUnit timeUnit); | ||
} |
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.