In the modern world, it is quite normal that while a JVM is running an application, the executing host machine cycles through several system states. For example, a chat application running on Java SE might be kept open while the laptop hosting the JVM is suspended or sent to hibernation, and after several hours it is resumed.
Currently the application running inside the JVM (here: the chat app) does not get informed of any state changes of the host. Neither its suspension, nor its wakeup. This is not very smart. Many application might benefit from these events either in a technical or even in a business level sense. For example, the chat app might send a "I'm away now" message to the chat partner so he immediately sees that the friend will be unavailable for longer time, and immediately sees when the friend is back online due to a "I'm back" event transmitted as soon as the laptop is resumed. Another example could be a server application where one node of a cluster kindly informs the other nodes that he will be unavailable due to a power loss detected by the UPS, reported by the OS to the JVM.
While native applications written e. g. in C will be informed by the OS since decades (e. g. there are Power Status events defined by the Windows API), until now there is no cross-platform API in the JRE. An application that wants to be informed by the OS in case of system status change has to make use of JNI and OS specific APIs. This is neither WORA, nor smart or contemporary.
Hence I'd like to propose the addition of a very simplistic API. It could look like this for example:
Runtime.addStateListener(Consumer<StateEvent> event);
Runtime.removeStateListener(Consumer<StateEvent> event);
public interface StateEvent {
}
public class SuspendStateEvent implements StateEvent {
}
public class ResumeStateEvent implements StateEvent {
}
While these are the most useful and essential events, certainly it would be beneficial to add more system state events, e. g. IdleStateEvent.
Currently the application running inside the JVM (here: the chat app) does not get informed of any state changes of the host. Neither its suspension, nor its wakeup. This is not very smart. Many application might benefit from these events either in a technical or even in a business level sense. For example, the chat app might send a "I'm away now" message to the chat partner so he immediately sees that the friend will be unavailable for longer time, and immediately sees when the friend is back online due to a "I'm back" event transmitted as soon as the laptop is resumed. Another example could be a server application where one node of a cluster kindly informs the other nodes that he will be unavailable due to a power loss detected by the UPS, reported by the OS to the JVM.
While native applications written e. g. in C will be informed by the OS since decades (e. g. there are Power Status events defined by the Windows API), until now there is no cross-platform API in the JRE. An application that wants to be informed by the OS in case of system status change has to make use of JNI and OS specific APIs. This is neither WORA, nor smart or contemporary.
Hence I'd like to propose the addition of a very simplistic API. It could look like this for example:
Runtime.addStateListener(Consumer<StateEvent> event);
Runtime.removeStateListener(Consumer<StateEvent> event);
public interface StateEvent {
}
public class SuspendStateEvent implements StateEvent {
}
public class ResumeStateEvent implements StateEvent {
}
While these are the most useful and essential events, certainly it would be beneficial to add more system state events, e. g. IdleStateEvent.