Make sure the JVM stop hook works #295

This commit is contained in:
wusheng 2017-07-18 21:47:24 +08:00
parent 75ada9feaa
commit cba4dc2883
7 changed files with 37 additions and 4 deletions

View File

@ -20,6 +20,7 @@ public class ConsumerPool<T> {
this(channels, num);
for (int i = 0; i < num; i++) {
consumerThreads[i] = new ConsumerThread("DataCarrier.Consumser." + i + ".Thread", getNewConsumerInstance(consumerClass));
consumerThreads[i].setDaemon(true);
}
}

View File

@ -1,14 +1,11 @@
package org.skywalking.apm.agent.core.jvm;
import io.grpc.ManagedChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.skywalking.apm.agent.core.boot.BootService;
import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.conf.Config;
@ -65,6 +62,12 @@ public class JVMService implements BootService, Runnable {
}
@Override
public void shutdown() throws Throwable {
collectMetricFuture.cancel(true);
sendMetricFuture.cancel(true);
}
@Override
public void run() {
if (RemoteDownstreamConfig.Agent.APPLICATION_ID != DictionaryUtil.nullValue()

View File

@ -77,6 +77,11 @@ public class AppAndServiceRegisterClient implements BootService, GRPCChannelList
TracingContext.ListenerManager.add(this);
}
@Override
public void shutdown() throws Throwable {
applicationRegisterFuture.cancel(true);
}
@Override
public void run() {
if (CONNECTED.equals(status)) {

View File

@ -1,6 +1,7 @@
package org.skywalking.apm.agent.core.remote;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.skywalking.apm.agent.core.boot.BootService;
import org.skywalking.apm.agent.core.conf.Config;
@ -11,6 +12,8 @@ import org.skywalking.apm.agent.core.conf.Config;
* @author wusheng
*/
public class CollectorDiscoveryService implements BootService {
private ScheduledFuture<?> future;
@Override
public void beforeBoot() throws Throwable {
@ -18,7 +21,7 @@ public class CollectorDiscoveryService implements BootService {
@Override
public void boot() throws Throwable {
Executors.newSingleThreadScheduledExecutor()
future = Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(new DiscoveryRestServiceClient(), 0,
Config.Collector.DISCOVERY_CHECK_INTERVAL, TimeUnit.SECONDS);
}
@ -27,4 +30,9 @@ public class CollectorDiscoveryService implements BootService {
public void afterBoot() throws Throwable {
}
@Override
public void shutdown() throws Throwable {
future.cancel(true);
}
}

View File

@ -49,6 +49,12 @@ public class GRPCChannelManager implements BootService, Runnable {
}
@Override
public void shutdown() throws Throwable {
connectCheckFuture.cancel(true);
managedChannel.shutdownNow();
}
@Override
public void run() {
if (reconnect) {

View File

@ -49,6 +49,11 @@ public class TraceSegmentServiceClient implements BootService, IConsumer<TraceSe
TracingContext.ListenerManager.add(this);
}
@Override
public void shutdown() throws Throwable {
carrier.shutdownConsumers();
}
@Override
public void init() {

View File

@ -61,6 +61,11 @@ public class SamplingService implements BootService {
}
@Override
public void shutdown() throws Throwable {
scheduledFuture.cancel(true);
}
/**
* @return true, if sampling mechanism is on, and get the sampling factor successfully.
*/