编程语言
首页 > 编程语言> > Java-禁用或取消部署我的应用程序后计划仍在运行

Java-禁用或取消部署我的应用程序后计划仍在运行

作者:互联网

我在ManagedScheduledExecutorService中遇到问题.如果我禁用或取消部署我的应用程序,则调度程序仍在运行.仅当我重新启动应用程序服务器时它才会停止.

我正在使用JEE,Application Server Wildfly 9和Java8.

下面的代码:

package br.com.decarli;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import javax.annotation.Resource;
import javax.enterprise.concurrent.LastExecution;
import javax.enterprise.concurrent.ManagedScheduledExecutorService;
import javax.enterprise.concurrent.Trigger;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;

@ApplicationScoped
public class ApplicationSchedulerX {

@Resource( lookup = "java:jboss/ee/concurrency/scheduler/MyScheduler" )
private ManagedScheduledExecutorService timerService;

public void init( @Observes @Initialized( ApplicationScoped.class ) Object o ) {

    timerService.schedule( () -> {

        System.out.println( " Run scheduler... "  );

    }, new Trigger() {

        @Override
        public Date getNextRunTime( LastExecution le, Date date ) {
            return getNextDateBySeconds( 60 );
        }

        @Override
        public boolean skipRun( LastExecution le, Date date ) {
            return false;
        }

    } );

}

private Date getNextDateBySeconds( long seconds ) {
    LocalDateTime ldt = LocalDateTime.now().plusSeconds( seconds );
    return Date.from( ldt.atZone( ZoneId.systemDefault() ).toInstant() );
}

public void destroy( @Observes @Destroyed( ApplicationScoped.class ) Object o ) {
    //TODO error: Lifecycle operation not supported
    //timerService.shutdown();
}
}

Wildfly 9调度程序配置:

<managed-scheduled-executor-service name="MyScheduler" jndi-name="java:jboss/ee/concurrency/scheduler/MyScheduler" hung-task-threshold="50000" long-running-tasks="true" core-threads="4" keepalive-time="500" reject-policy="ABORT"/>

解决方法:

我在链接https://issues.jboss.org/browse/WFLY-3683上找到了解决方案.

我创建了ScheduledTasks的列表,并在destroy方法上取消了.

正确的代码如下:

package br.com.ciss.cissmart.client.core;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

import javax.annotation.Resource;
import javax.enterprise.concurrent.LastExecution;
import javax.enterprise.concurrent.ManagedScheduledExecutorService;
import javax.enterprise.concurrent.Trigger;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;

@ApplicationScoped
public class ApplicationSchedulerX {

@Resource( lookup = "java:jboss/ee/concurrency/scheduler/MyScheduler" )
private ManagedScheduledExecutorService timerService;

private List<ScheduledFuture<?>> scheduledTasks;

/**
 * Método executado no start do servidor.
 *
 * @param o
 *             - {@link javax.servlet.ServletContext} - Contendo as configurações da aplicação
 */
public void init( @Observes @Initialized( ApplicationScoped.class ) Object o ) {

    this.scheduledTasks = Collections.synchronizedList(new ArrayList<ScheduledFuture<?>>());

    ScheduledFuture task =  timerService.schedule( () -> {


        System.out.println( "Run scheduler... " );

    }, new Trigger() {

        @Override
        public Date getNextRunTime( LastExecution le, Date date ) {
            return getNextDateBySeconds( 60 );
        }

        @Override
        public boolean skipRun( LastExecution le, Date date ) {
            return false;
        }

    } );

    scheduledTasks.add( task );

}

private Date getNextDateBySeconds( long seconds ) {
    LocalDateTime ldt = LocalDateTime.now().plusSeconds( seconds );
    return Date.from( ldt.atZone( ZoneId.systemDefault() ).toInstant() );
}

public void destroy( @Observes @Destroyed( ApplicationScoped.class ) Object o ) {

    // Cancel any scheduled tasks, ensuring that the map is locked.
    synchronized ( this.scheduledTasks ) {
        Iterator<ScheduledFuture<?>> i = this.scheduledTasks.iterator();

        while ( i.hasNext() ) {
            ScheduledFuture<?> future = i.next();
            // Cancel the task.
            future.cancel( true );
        }
    }
    this.scheduledTasks.clear();
    this.scheduledTasks = null;

}
}

标签:scheduler,wildfly,executorservice,java,java-ee
来源: https://codeday.me/bug/20191026/1939458.html