forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ROCKETMQ-39] avoid duplicated codes closes apache#34
- Loading branch information
1 parent
4d12d11
commit c0fe02e
Showing
3 changed files
with
85 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
srvutil/src/main/java/org/apache/rocketmq/srvutil/ShutdownHookThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.rocketmq.srvutil; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.slf4j.Logger; | ||
|
||
/** | ||
* {@link ShutdownHookThread} is the standard hook for filtersrv and namesrv modules. | ||
* Through {@link Callable} interface, this hook can customization operations in anywhere. | ||
*/ | ||
public class ShutdownHookThread extends Thread { | ||
private volatile boolean hasShutdown = false; | ||
private AtomicInteger shutdownTimes = new AtomicInteger(0); | ||
private final Logger log; | ||
private final Callable callback; | ||
|
||
/** | ||
* Create the standard hook thread, with a call back, by using {@link Callable} interface. | ||
* | ||
* @param log The log instance is used in hook thread. | ||
* @param callback The call back function. | ||
*/ | ||
public ShutdownHookThread(Logger log, Callable callback) { | ||
super("ShutdownHook"); | ||
this.log = log; | ||
this.callback = callback; | ||
} | ||
|
||
/** | ||
* Thread run method. | ||
* Invoke when the jvm shutdown. | ||
* 1. count the invocation times. | ||
* 2. execute the {@link ShutdownHookThread#callback}, and time it. | ||
*/ | ||
@Override | ||
public void run() { | ||
synchronized (this) { | ||
log.info("shutdown hook was invoked, " + this.shutdownTimes.incrementAndGet() + " times."); | ||
if (!this.hasShutdown) { | ||
this.hasShutdown = true; | ||
long beginTime = System.currentTimeMillis(); | ||
try { | ||
this.callback.call(); | ||
} catch (Exception e) { | ||
log.error("shutdown hook callback invoked failure.", e); | ||
} | ||
long consumingTimeTotal = System.currentTimeMillis() - beginTime; | ||
log.info("shutdown hook done, consuming time total(ms): " + consumingTimeTotal); | ||
} | ||
} | ||
} | ||
} |