-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-boot: introduce DbSchedulerStopper
This lets the user control when the scheduler should be stopped. By default it stops the scheduler when Spring issues the ContextClosedEvent. Fixes #423
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
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
18 changes: 18 additions & 0 deletions
18
...tarter/src/main/java/com/github/kagkarlsson/scheduler/boot/config/DbSchedulerStopper.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,18 @@ | ||
/* | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* <p>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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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 com.github.kagkarlsson.scheduler.boot.config; | ||
|
||
public interface DbSchedulerStopper { | ||
void doStop(); | ||
} |
48 changes: 48 additions & 0 deletions
48
.../java/com/github/kagkarlsson/scheduler/boot/config/shutdown/AbstractSchedulerStopper.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,48 @@ | ||
/* | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* <p>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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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 com.github.kagkarlsson.scheduler.boot.config.shutdown; | ||
|
||
import com.github.kagkarlsson.scheduler.Scheduler; | ||
import com.github.kagkarlsson.scheduler.SchedulerState; | ||
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerStopper; | ||
import java.util.Objects; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class AbstractSchedulerStopper implements DbSchedulerStopper { | ||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||
private final Scheduler scheduler; | ||
|
||
protected AbstractSchedulerStopper(Scheduler scheduler) { | ||
this.scheduler = Objects.requireNonNull(scheduler, "A scheduler must be provided"); | ||
} | ||
|
||
@Override | ||
public void doStop() { | ||
SchedulerState state = scheduler.getSchedulerState(); | ||
|
||
if (state.isShuttingDown()) { | ||
log.warn("Scheduler is already shutting down - will not attempting to stop"); | ||
return; | ||
} | ||
|
||
if (!state.isStarted()) { | ||
log.info("Scheduler not started - will not attempt to start"); | ||
return; | ||
} | ||
|
||
log.info("Triggering scheduler stop"); | ||
scheduler.stop(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...main/java/com/github/kagkarlsson/scheduler/boot/config/shutdown/ContextClosedStopper.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,29 @@ | ||
/* | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* <p>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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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 com.github.kagkarlsson.scheduler.boot.config.shutdown; | ||
|
||
import com.github.kagkarlsson.scheduler.Scheduler; | ||
import org.springframework.context.event.ContextClosedEvent; | ||
import org.springframework.context.event.EventListener; | ||
|
||
public class ContextClosedStopper extends AbstractSchedulerStopper { | ||
public ContextClosedStopper(Scheduler scheduler) { | ||
super(scheduler); | ||
} | ||
|
||
@EventListener(ContextClosedEvent.class) | ||
public void whenContextIsStopped() { | ||
doStop(); | ||
} | ||
} |
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