Skip to content

Commit

Permalink
Merge pull request #372
Browse files Browse the repository at this point in the history
* [New] allowing configuring shutdown hook

* [Minor] shutdown hook is disabled by default
  • Loading branch information
eitch authored Aug 27, 2024
1 parent a338a96 commit ab9eacf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
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 = false;

// 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

0 comments on commit ab9eacf

Please sign in to comment.