EJB @Schedule wait until method completed

后端 未结 4 770
陌清茗
陌清茗 2020-11-30 22:52

I want to write a back-ground job (EJB 3.1), which executes every minute. For this I use the following annotation:

@Schedule(minute = \"*/1\", hour = \"*\")
         


        
4条回答
  •  既然无缘
    2020-11-30 22:58

    Since Java EE 7 it is possible to use an "EE-aware" ManagedScheduledExecutorService, i.e. in WildFly:

    In for example a @Singleton @Startup @LocalBean, inject the default "managed-scheduled-executor-service" configured in standalone.xml:

    @Resource
    private ManagedScheduledExecutorService scheduledExecutorService;
    

    Schedule some task in @PostConstruct to be executed i.e. every second with fixed delay:

    scheduledExecutorService.scheduleWithFixedDelay(this::someMethod, 1, 1, TimeUnit.SECONDS);
    

    scheduleWithFixedDelay:

    Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.[...]

    Do not shutdown the scheduler in i.e. @PreDestroy:

    Managed Scheduled Executor Service instances are managed by the application server, thus Java EE applications are forbidden to invoke any lifecycle related method.

提交回复
热议问题