Pi4J / pi4j-v2

Pi4J Version 2.0
Apache License 2.0
273 stars 57 forks source link

allowing configuring shutdown hook #372

Closed eitch closed 1 month ago

eitch commented 2 months ago

The shutdown hook is hooked into the JVM and is executed when the JVM stops.

This can be beneficial when the application is not managing pi4j's life cycle, but when an application wants to properly manage the life cycle, then this can be detrimental to the shutdown sequence of the application.

This change allows such an application to disable this feature.

The shutdown hook is still enabled by default, a future version might decide to disable it by default.

eitch commented 2 months ago

@FDelporte should we disable it by default, or leave it enabled as is currently the case?

FDelporte commented 2 months ago

@FDelporte should we disable it by default, or leave it enabled as is currently the case?

Not sure, I started doubting myself based on your remark in the ticket...

eitch commented 2 months ago

Well in the end, it is quite simple for those who care. Change the following line:

Context pi4j = Pi4J.newAutoContext();

to

Context pi4j = Pi4J.newContextBuilder().autoDetect().disableShutdownHook().build();

or if it was default disabled, they can enable it again:

Context pi4j = Pi4J.newContextBuilder().autoDetect().enableShutdownHook().build();
eitch commented 2 months ago

I personally would prefer having it opt-in / disabled by default.

FDelporte commented 2 months ago

That calls for some extra documentation ;-) Will add with new release.

eitch commented 2 months ago

Ok, i now disabled the hook by default. After the checks passed, I'll merge, and then think we can do a 2.6.2, or do you think this warrants a 2.7.0? @FDelporte

IAmNickNack commented 1 month ago

I'm a little confused 😄

Which option most closely resembles the existing behaviour?

It sounds like this decision could introduce a breaking change for existing users, but maybe I'm misreading this thread

eitch commented 1 month ago

I'm a little confused 😄

Which option most closely resembles the existing behaviour?

It sounds like this decision could introduce a breaking change for existing users, but maybe I'm misreading this thread

Yes, this is a breaking change. Existing behaviour can be reactivated with:

Context pi4j = Pi4J.newContextBuilder().autoDetect().enableShutdownHook().build();

We will communicate this in the release notes, when we release.