blob: d6c5a814548d3a5430febb005bc68ac6e0e86bf4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*
* Copyright (C) 2023, SAP SE and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.util;
import java.text.MessageFormat;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jgit.internal.JGitText;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A hook registered as a JVM shutdown hook managing a set of objects needing
* cleanup during JVM shutdown. See {@link Runtime#addShutdownHook}.
*/
@SuppressWarnings("ImmutableEnumChecker")
public enum ShutdownHook {
/**
* Singleton
*/
INSTANCE;
/**
* Object that needs to cleanup on JVM shutdown.
*/
public interface Listener {
/**
* Cleanup resources when JVM shuts down, called from JVM shutdown hook.
* <p>
* Implementations should be coded defensively
* <ul>
* <li>they should finish their work quickly
* <li>they should be written to be thread-safe and to avoid deadlocks
* insofar as possible
* <li>they should not rely blindly upon services that may have
* registered their own shutdown hooks and therefore may themselves be
* in the process of shutting down
* <li>attempts to use other thread-based services may lead to
* deadlocks.
* </ul>
* See {@link Runtime#addShutdownHook} for more details.
*/
public void onShutdown();
}
private static final Logger LOG = LoggerFactory
.getLogger(ShutdownHook.class);
private final Set<Listener> listeners = ConcurrentHashMap.newKeySet();
private volatile boolean shutdownInProgress;
private ShutdownHook() {
try {
Runtime.getRuntime().addShutdownHook(new Thread(this::cleanup));
} catch (IllegalStateException e) {
// ignore - the VM is already shutting down
}
}
private void cleanup() {
shutdownInProgress = true;
ExecutorService runner = Executors.newWorkStealingPool();
try {
runner.submit(() -> {
this.doCleanup();
return null;
}).get(30L, TimeUnit.SECONDS);
} catch (RejectedExecutionException | InterruptedException
| ExecutionException | TimeoutException e) {
LOG.error(JGitText.get().shutdownCleanupFailed, e);
}
runner.shutdownNow();
}
private void doCleanup() {
listeners.parallelStream().forEach(this::notify);
}
private void notify(Listener l) {
LOG.debug(JGitText.get().shutdownCleanup, l);
try {
l.onShutdown();
} catch (RuntimeException e) {
LOG.error(MessageFormat.format(
JGitText.get().shutdownCleanupListenerFailed, l), e);
}
}
/**
* Register object that needs cleanup during JVM shutdown if it is not
* already registered. Registration is disabled when JVM shutdown is already
* in progress.
*
* @param l
* the object to call {@link Listener#onShutdown} on when JVM
* shuts down
* @return {@code true} if this object has been registered
*/
public boolean register(Listener l) {
if (shutdownInProgress) {
return listeners.contains(l);
}
LOG.debug("register {} with shutdown hook", l); //$NON-NLS-1$
listeners.add(l);
return true;
}
/**
* Unregister object that no longer needs cleanup during JVM shutdown if it
* is still registered. Unregistration is disabled when JVM shutdown is
* already in progress.
*
* @param l
* the object registered to be notified for cleanup when the JVM
* shuts down
* @return {@code true} if this object is no longer registered
*/
public boolean unregister(Listener l) {
if (shutdownInProgress) {
return !listeners.contains(l);
}
LOG.debug("unregister {} from shutdown hook", l); //$NON-NLS-1$
listeners.remove(l);
return true;
}
/**
* Whether a JVM shutdown is in progress
*
* @return {@code true} if a JVM shutdown is in progress
*/
public boolean isShutdownInProgress() {
return shutdownInProgress;
}
}
|