Add method to allow users to add application shutdown hooks
This commit is contained in:
parent
024c757eab
commit
d86f261f6b
|
@ -36,8 +36,10 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides native access to serial ports and devices without requiring external libraries or tools.
|
* This class provides native access to serial ports and devices without requiring external libraries or tools.
|
||||||
|
@ -52,6 +54,7 @@ public final class SerialPort
|
||||||
// Static initializer loads correct native library for this machine
|
// Static initializer loads correct native library for this machine
|
||||||
static private final String versionString = "2.9.1";
|
static private final String versionString = "2.9.1";
|
||||||
static private final String tmpdirAppIdProperty = "fazecast.jSerialComm.appid";
|
static private final String tmpdirAppIdProperty = "fazecast.jSerialComm.appid";
|
||||||
|
static private List<Thread> shutdownHooks = new ArrayList<Thread>();
|
||||||
static private volatile boolean isAndroid = false;
|
static private volatile boolean isAndroid = false;
|
||||||
static private volatile boolean isWindows = false;
|
static private volatile boolean isWindows = false;
|
||||||
static private volatile boolean isShuttingDown = false;
|
static private volatile boolean isShuttingDown = false;
|
||||||
|
@ -406,6 +409,17 @@ public final class SerialPort
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
|
// Run any user-specified shutdown hooks
|
||||||
|
try {
|
||||||
|
for (Thread hook : shutdownHooks)
|
||||||
|
{
|
||||||
|
hook.start();
|
||||||
|
hook.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (InterruptedException e) {}
|
||||||
|
|
||||||
|
// Un-initialize the native library
|
||||||
isShuttingDown = true;
|
isShuttingDown = true;
|
||||||
uninitializeLibrary();
|
uninitializeLibrary();
|
||||||
}
|
}
|
||||||
|
@ -443,6 +457,23 @@ public final class SerialPort
|
||||||
*/
|
*/
|
||||||
static public final String getVersion() { return versionString; }
|
static public final String getVersion() { return versionString; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a shutdown hook that will run just before the application fully closes, due to either
|
||||||
|
* exiting normally or in response to a user interrupt such as Ctrl+C.
|
||||||
|
* <p>
|
||||||
|
* These hooks can be used to carry out any final serial port operations that should be executed to
|
||||||
|
* ensure graceful disconnection or device shutdown.
|
||||||
|
* <p>
|
||||||
|
* There is no need to add a shutdown hook just to close all open ports, as this is done automatically
|
||||||
|
* by the library; however, any special reads, writes, or port handling that should take place prior
|
||||||
|
* to closing of the ports should be handled in a shutdown hook registered with this method.
|
||||||
|
*
|
||||||
|
* @param hook A {@link java.lang.Thread} object that will run just before the application shuts down.
|
||||||
|
* @see java.lang.Runtime#addShutdownHook(Thread)
|
||||||
|
* @see java.lang.Thread
|
||||||
|
*/
|
||||||
|
static public final synchronized void addShutdownHook(Thread hook) { shutdownHooks.add(hook); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of all available serial ports on this machine.
|
* Returns a list of all available serial ports on this machine.
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* SerialPortTest.java
|
* SerialPortTest.java
|
||||||
*
|
*
|
||||||
* Created on: Feb 27, 2015
|
* Created on: Feb 27, 2015
|
||||||
* Last Updated on: Jan 28, 2022
|
* Last Updated on: Feb 18, 2022
|
||||||
* Author: Will Hedgecock
|
* Author: Will Hedgecock
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012-2022 Fazecast, Inc.
|
* Copyright (C) 2012-2022 Fazecast, Inc.
|
||||||
|
@ -84,6 +84,7 @@ public class SerialPortTest
|
||||||
static public void main(String[] args)
|
static public void main(String[] args)
|
||||||
{
|
{
|
||||||
System.out.println("\nUsing Library Version v" + SerialPort.getVersion());
|
System.out.println("\nUsing Library Version v" + SerialPort.getVersion());
|
||||||
|
SerialPort.addShutdownHook(new Thread() { public void run() { System.out.println("\nRunning shutdown hook"); } });
|
||||||
SerialPort[] ports = SerialPort.getCommPorts();
|
SerialPort[] ports = SerialPort.getCommPorts();
|
||||||
System.out.println("\nAvailable Ports:\n");
|
System.out.println("\nAvailable Ports:\n");
|
||||||
for (int i = 0; i < ports.length; ++i)
|
for (int i = 0; i < ports.length; ++i)
|
||||||
|
|
Loading…
Reference in New Issue