-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: SSpirits <[email protected]>
- Loading branch information
1 parent
90c342a
commit cc2c072
Showing
17 changed files
with
543 additions
and
303 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
common/src/main/java/com/automq/rocketmq/common/ServiceThread.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,145 @@ | ||
/* | ||
* 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 com.automq.rocketmq.common; | ||
|
||
import com.automq.rocketmq.common.util.Lifecycle; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.apache.rocketmq.common.CountDownLatch2; | ||
import org.apache.rocketmq.common.constant.LoggerName; | ||
import org.apache.rocketmq.logging.org.slf4j.Logger; | ||
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory; | ||
|
||
public abstract class ServiceThread implements Runnable, Lifecycle { | ||
private static final Logger log = LoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME); | ||
|
||
private static final long JOIN_TIME = 90 * 1000; | ||
|
||
protected Thread thread; | ||
protected final CountDownLatch2 waitPoint = new CountDownLatch2(1); | ||
protected volatile AtomicBoolean hasNotified = new AtomicBoolean(false); | ||
protected volatile boolean stopped = false; | ||
protected boolean isDaemon = false; | ||
|
||
//Make it able to restart the thread | ||
private final AtomicBoolean started = new AtomicBoolean(false); | ||
|
||
public ServiceThread() { | ||
|
||
} | ||
|
||
public abstract String getServiceName(); | ||
|
||
@Override | ||
public void start() { | ||
log.info("Try to start service thread:{} started:{} lastThread:{}", getServiceName(), started.get(), thread); | ||
if (!started.compareAndSet(false, true)) { | ||
return; | ||
} | ||
stopped = false; | ||
this.thread = new Thread(this, getServiceName()); | ||
this.thread.setDaemon(isDaemon); | ||
this.thread.start(); | ||
log.info("Start service thread:{} started:{} lastThread:{}", getServiceName(), started.get(), thread); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
this.shutdown(false); | ||
} | ||
|
||
public void shutdown(final boolean interrupt) { | ||
log.info("Try to shutdown service thread:{} started:{} lastThread:{}", getServiceName(), started.get(), thread); | ||
if (!started.compareAndSet(true, false)) { | ||
return; | ||
} | ||
this.stopped = true; | ||
log.info("shutdown thread[{}] interrupt={} ", getServiceName(), interrupt); | ||
|
||
//if thead is waiting, wakeup it | ||
wakeup(); | ||
|
||
try { | ||
if (interrupt) { | ||
this.thread.interrupt(); | ||
} | ||
|
||
long beginTime = System.currentTimeMillis(); | ||
if (!this.thread.isDaemon()) { | ||
this.thread.join(this.getJoinTime()); | ||
} | ||
long elapsedTime = System.currentTimeMillis() - beginTime; | ||
log.info("join thread[{}], elapsed time: {}ms, join time:{}ms", getServiceName(), elapsedTime, this.getJoinTime()); | ||
} catch (InterruptedException e) { | ||
log.error("Interrupted", e); | ||
} | ||
} | ||
|
||
public long getJoinTime() { | ||
return JOIN_TIME; | ||
} | ||
|
||
public void makeStop() { | ||
if (!started.get()) { | ||
return; | ||
} | ||
this.stopped = true; | ||
log.info("makestop thread[{}] ", this.getServiceName()); | ||
} | ||
|
||
public void wakeup() { | ||
if (hasNotified.compareAndSet(false, true)) { | ||
waitPoint.countDown(); // notify | ||
} | ||
} | ||
|
||
protected void waitForRunning(long interval) { | ||
if (hasNotified.compareAndSet(true, false)) { | ||
this.onWaitEnd(); | ||
return; | ||
} | ||
|
||
//entry to wait | ||
waitPoint.reset(); | ||
|
||
try { | ||
waitPoint.await(interval, TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException e) { | ||
log.error("Interrupted", e); | ||
} finally { | ||
hasNotified.set(false); | ||
this.onWaitEnd(); | ||
} | ||
} | ||
|
||
protected void onWaitEnd() { | ||
} | ||
|
||
public boolean isStopped() { | ||
return stopped; | ||
} | ||
|
||
public boolean isDaemon() { | ||
return isDaemon; | ||
} | ||
|
||
public void setDaemon(boolean daemon) { | ||
isDaemon = daemon; | ||
} | ||
} | ||
|
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
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
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
Oops, something went wrong.