Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allowing configuring shutdown hook #372

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/context/ContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ default ContextBuilder setAutoInject(boolean autoInject){
return noAutoInject();
}

/**
* <p>enableShutdownHook.</p>
*
* @return a {@link com.pi4j.context.ContextBuilder} object.
*/
ContextBuilder enableShutdownHook();

/**
* <p>disableShutdownHook.</p>
*
* @return a {@link com.pi4j.context.ContextBuilder} object.
*/
ContextBuilder disableShutdownHook();

/**
* <p>setShutdownHook.</p>
*
* @param enableShutdownHook a boolean.
*
* @return a {@link com.pi4j.context.ContextBuilder} object.
*/
default ContextBuilder setShutdownHook(boolean enableShutdownHook) {
if (enableShutdownHook)
return enableShutdownHook();
else
return disableShutdownHook();
}

/**
* <p>toConfig.</p>
*
Expand Down
6 changes: 6 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/context/ContextConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ default Collection<Platform> getPlatforms(){
* @return a boolean.
*/
boolean autoInject();
/**
* <p>enableShutdownHook.</p>
*
* @return a boolean.
*/
boolean enableShutdownHook();
/**
* <p>getAutoInject.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class DefaultContextBuilder implements ContextBuilder {
protected boolean autoDetectPlatforms = false;
protected boolean autoDetectProviders = false;
protected boolean autoInject = false;
protected boolean enableShutdownHook = true;

// default platform identifier
protected String defaultPlatformId = null;
Expand Down Expand Up @@ -157,6 +158,20 @@ public ContextBuilder noAutoInject() {
return this;
}

/** {@inheritDoc} */
@Override
public ContextBuilder enableShutdownHook() {
this.enableShutdownHook = true;
return this;
}

/** {@inheritDoc} */
@Override
public ContextBuilder disableShutdownHook() {
this.enableShutdownHook = false;
return this;
}

/** {@inheritDoc} */
@Override
public ContextBuilder property(String key, String value){
Expand Down Expand Up @@ -258,11 +273,17 @@ public String defaultPlatform() {
public boolean autoDetectMockPlugins() {
return builder.autoDetectMockPlugins;
}

@Override
public boolean autoDetectPlatforms() {
return builder.autoDetectPlatforms;
}

@Override
public boolean enableShutdownHook() {
return builder.enableShutdownHook;
}

@Override
public boolean autoInject() { return builder.autoInject; }

Expand Down
20 changes: 11 additions & 9 deletions pi4j-core/src/main/java/com/pi4j/runtime/impl/DefaultRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ private DefaultRuntime(Context context) {

// listen for shutdown to properly clean up
// TODO :: ADD PI4J INTERNAL SHUTDOWN CALLBACKS/EVENTS
java.lang.Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
// shutdown Pi4J
if (!isShutdown)
shutdown();
} catch (Exception e) {
logger.error("Failed to shutdown Pi4J runtime", e);
}
}, "pi4j-shutdown"));
if (this.context.config().enableShutdownHook()) {
java.lang.Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
// shutdown Pi4J
if (!isShutdown)
shutdown();
} catch (Exception e) {
logger.error("Failed to shutdown Pi4J runtime", e);
}
}, "pi4j-shutdown"));
}
}

/**
Expand Down