由于实现了重新启动机制,我在使用ScheduledExecutorService时遇到错误的行为。

问题-短

每次重新启动尝试都会创建一个新的计划任务,然后重新启动旧任务。

问题-长

该过程的目标是不时将消息发布到RabbitMQ中(它保持活动状态)。 RabbitMQ发生异常时,它将使用ExceptionObserver进行通知
发生异常。实现的ExceptionObserver停止服务,然后再次重新启动它。它尝试重新启动3次,如果成功重新启动,则计数为
重置为零。如果无法重新启动,则尝试计数将增加,并且如果达到尝试限制,它将关闭进程。

每次重新启动服务时,它都会创建一个新的“ KeepAliveService”并重新启动最后一个服务。因此,每次发生异常时,都会创建一个新服务,而旧的
服务重新启动。如果重启后发生1个异常,则有2个进程正在运行。如果发生2次异常,则有3个进程在运行,依此类推。

处理保持活动服务的服务类(启动/停止ScheduledExecutorService)

private KeepaliveExecutor keepaliveExecutor; // This is the runnable used inside the scheduledService
private ScheduledFuture futureTask; // The escheduled task
private ScheduledExecutorService scheduledService; // the scheduled service
private ExceptionObserver exceptionObserver; // The Exception Handler, which will handle the exceptions

public void startService( final int keepaliveTime ) throws IllegalArgumentException, FInfraException {
    keepaliveExecutor = new KeepaliveExecutor( new RabbitMQService( settings ), settings );
    keepaliveExecutor.setExceptionObserver( exceptionObserver );
    scheduledService = Executors.newSingleThreadScheduledExecutor();
    futureTask = scheduledService.scheduleAtFixedRate( keepaliveExecutor, 0, keepaliveTime, TimeUnit.MINUTES );
}

public void stopService() {
    futureTask.cancel(true);
    scheduledService.shutdown();
}


KeepaliveExecutor类

class KeepaliveExecutor implements Runnable {

private FInfraExceptionObserver exceptionObserver;

@Override
public void run() {
    try {
        final String keepAlive = JsonMapper.toJsonString( keepaliveMessage );
        rabbitService.publishMessage( keepAlive );
        keepaliveMessage.setFirtsPackage( false );
    } catch( FInfraException ex ) {
        if( exceptionObserver != null ) {
            exceptionObserver.notifyExpcetion(ex);
        }
    }
}


ExceptionObserver实现类

public class FInfraExceptionHandler implements FInfraExceptionObserver {

private final FInfraServiceHandler finfraHandler;

public FInfraExceptionHandler(FInfraServiceHandler finfraHandler) {
    this.finfraHandler = finfraHandler;
}

@Override
public void notifyExpcetion(Throwable ex) {
    Util.logger.log( Level.INFO, "F-Infra Exception occurred", ex);
    finfraHandler.stopService();
    Util.logger.log( Level.INFO, "Waiting 30s for restarting..." );
    Util.wait( 30, TimeUnit.SECONDS );
    finfraHandler.startService();
}


FInfraServiceHandler类

public class FInfraServiceHandler {

private static final int ATTEMPT_LIMIT = 3;

private FInfraService finfraService;
private int keepaliveTime;
private int attempt;

public FInfraServiceHandler() {
    this.finfraService = new FInfraService();
    this.finfraService.setExceptionObserver(new FInfraExceptionHandler( this ));
    this.attempt = 0;
}

void startService(){
    if( attempt <= ATTEMPT_LIMIT ) {
        try {
            attempt++;
            Util.logger.log(Level.INFO, "Starting F-Infra Service. Attemp[{0} of {1}]", new String[]{String.valueOf(attempt), String.valueOf(ATTEMPT_LIMIT)});
            finfraService.startService( keepaliveTime );
        } catch( FInfraException | RuntimeException ex ){
            Util.logger.log(Level.INFO, "F-INFRA EXCEPTION", ex);
            startService();
        }
        Util.logger.log( Level.INFO, "F-Infra started!");
        attempt = 0;
        return;
    }
    Util.logger.log( Level.INFO, "Restart attemp limit reached." );
    Main.closeAll(new ShutdownException("It's not possible stablish a connection with F-Infra Service."));
}

public void stopService() {
    if( attempt > 0 ){
        Util.logger.log(Level.INFO, "Stpoping F-Infra...");
        finfraService.stopService();
    }
}


接下来是日志,告诉我有多个服务正在运行

jul 16, 2017 2:58:03 PM domain.FInfraServiceHandler startService
INFO: Starting F-Infra Service. Attemp[1 of 3]
jul 16, 2017 2:58:03 PM domain.FInfraServiceHandler startService
INFO: F-Infra started!
jul 16, 2017 5:01:15 PM domain.FInfraExceptionHandler notifyExpcetion
INFO: F-Infra Exception occurred
domain.FInfraException: java.net.UnknownHostException: rabbit.domain
        at domain.RabbitMQService.openConnection(RabbitMQService.java:48)
        at domain.RabbitMQService.publishMessage(RabbitMQService.java:66)
        at domain.KeepaliveExecutor.run(KeepaliveExecutor.java:38)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.net.UnknownHostException: rabbit.domain
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
        at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:34)
        at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:91)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:670)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:722)
        at domain.RabbitMQService.openConnection(RabbitMQService.java:45)
        ... 9 more

jul 16, 2017 5:01:15 PM domain.FInfraExceptionHandler notifyExpcetion
INFO: Waiting 30s for restarting...
jul 16, 2017 5:01:45 PM domain.FInfraServiceHandler startService
INFO: Starting F-Infra Service. Attemp[1 of 3]
jul 16, 2017 5:01:45 PM domain.FInfraServiceHandler startService
INFO: F-Infra started!
 jul 16, 2017 6:01:58 PM domain.FInfraExceptionHandler notifyExpcetion
INFO: F-Infra Exception occurred
domain.FInfraException: java.net.UnknownHostException: rabbit.domain
        at domain.RabbitMQService.openConnection(RabbitMQService.java:48)
        at domain.RabbitMQService.publishMessage(RabbitMQService.java:66)
        at domain.KeepaliveExecutor.run(KeepaliveExecutor.java:38)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.net.UnknownHostException: rabbit.domain
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
        at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:34)
        at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:91)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:670)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:722)
        at domain.RabbitMQService.openConnection(RabbitMQService.java:45)
        ... 9 more

jul 16, 2017 6:01:58 PM domain.FInfraExceptionHandler notifyExpcetion
INFO: Waiting 30s for restarting...
jul 16, 2017 6:02:03 PM domain.FInfraExceptionHandler notifyExpcetion
INFO: F-Infra Exception occurred
domain.FInfraException: java.net.UnknownHostException: rabbit.domain
        at domain.RabbitMQService.openConnection(RabbitMQService.java:48)
        at domain.RabbitMQService.publishMessage(RabbitMQService.java:66)
        at domain.KeepaliveExecutor.run(KeepaliveExecutor.java:38)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.net.UnknownHostException: rabbit.domain
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
        at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:34)
        at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:91)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:670)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:722)
        at domain.RabbitMQService.openConnection(RabbitMQService.java:45)
        ... 9 more

jul 16, 2017 6:02:03 PM domain.FInfraExceptionHandler notifyExpcetion
INFO: Waiting 30s for restarting...
jul 16, 2017 6:02:28 PM domain.FInfraServiceHandler startService
INFO: Starting F-Infra Service. Attemp[1 of 3]
jul 16, 2017 6:02:28 PM domain.FInfraServiceHandler startService
INFO: F-Infra started!
jul 16, 2017 6:02:33 PM domain.FInfraServiceHandler startService
INFO: Starting F-Infra Service. Attemp[1 of 3]
jul 16, 2017 6:02:33 PM domain.FInfraServiceHandler startService
INFO: F-Infra started!


我不知道该怎么做才能关闭旧线程或使用当前线程重新启动。我试过的不是调用Thread.currentThread()。interrupt();在调用start方法之前,在ExceptionObserver类上进行操作。
但这是行不通的。

我不知道该怎么办。

最佳答案

FInfraServiceHandler类中,如果stopService为零,则您的attempt方法不执行任何操作。

public void stopService() {
    if( attempt > 0 ){
        Util.logger.log(Level.INFO, "Stpoping F-Infra...");
        finfraService.stopService();
    }
}


因此原始的ScheduledExecutorService继续前进。当我删除条件时,代码表现良好。

请注意,顺便说一句,您在同一实例上从不同的线程调用startServicestopService。我认为您需要在可变字段attempt上进行某种同步。

关于java - ScheduledExecutorService线程创建多个线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45132856/

10-12 01:36
查看更多