gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:55:04 25 4
gpt4 key购买 nike

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

我正在使用 JEE、应用服务器 Wildfly 9Java8

以下代码:

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 上找到了解决方案.

我创建了一个预定任务列表并在 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;

}
}

关于java - 禁用或取消部署我的应用程序后计划仍在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com