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

时间:2016-07-29 17:07:09

标签: java java-ee wildfly scheduler executorservice

我遇到了 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"/>

1 个答案:

答案 0 :(得分:4)

我在链接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;

}
}