kiwiproject / kiwi

A set of Java utilities that we could not find in Guava or Apache Commons...or we just felt like having our own version.
MIT License
12 stars 1 forks source link

Add methods to KiwiIO that can "close" any object #1162

Closed sleberknight closed 3 months ago

sleberknight commented 4 months ago

Some classes have a close method but don't implement AutoCloseable or Closeable. For example, until version 3.1.0, the Jakarta REST Client has a close method but doesn't implement AutoCloseable. Similarly, java.util.concurrent.ExecutorService has shutdown and shutdownNow methods.

Sometimes, for example during an application shutdown process, it would be nice to be able to call these methods and ignore exceptions the way KiwiIO.closeQuietly does for Closeable objects.

This task adds new methods to KiwiIO that can "close" objects which do not implement AutoCloseable or Closeable, such as the Jakarta REST Client (before version 3.1.0) and the JDK's ExecutorService.

Proposed methods:

The methods that only accept Object will assume the method name is close. The other methods explicitly accept a method name to be used to "close" those objects. For example, shutdown or shutdownNow for an ExecutorService. The method name argument is first so that the varargs overload can be used naturally. Otherwise, you would not be able to use the varargs syntax and would need to explicitly create an Object[] array.

The proposed method names include the word "Object" to make it more clear they are doing something different from closing a Closeable, i.e., that they will attempt to "close" any Object regardless of what interfaces it implements or doesn't.