2016-07-29 53 views
2

我遇到問題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"/> 
+0

可以使用[的ServletContextListener(http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContextListener.html)? [示例](https://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/) – ebanouz

+0

它啓動時正常工作,當它結束時,方法執行,但timerService仍在運行。我還沒有找到一種方法來清除timerService的所有時間表。 –

回答

3

我發現的方案中,鏈路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; 

} 
} 
相關問題