【问题标题】:How to set timer values in EJB scheduling?如何在 EJB 调度中设置计时器值?
【发布时间】:2013-11-19 06:33:27
【问题描述】:

目前我正在使用以下配置来安排我的调度程序。

 @Schedule(second ="1/10", minute = "*", hour = "*")
 private void scheduleUser() {      
    try {
        new UserFacade().insertUserInfo();            
    } catch (Exception e) {
        logger.error("Error in : " + e);
    }
 }

现在我想在运行时设置计时器值,而不是以硬编码的方式。假设我有一个 bean 调用 Property,它有一个名为 frequency 的字段。

现在我想为 EJB 调度程序设置像 new Property().getFrequency() 这样的值。

有什么方法可以做如下的事情吗?

 @Schedule(second =new Property().getFrequency(), minute = "*", hour = "*")
 private void scheduleUser() {      
    try {
        new UserFacade().insertUserInfo();            
    } catch (Exception e) {
        logger.error("Error in : " + e);
    }
 }

【问题讨论】:

    标签: java ejb ejbscheduler


    【解决方案1】:

    不带注释,不。您必须使用编程计时器(Java 7、EE7):

    package com.foo;
    
    import java.util.Date;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.annotation.Resource;
    import javax.ejb.EJBException;
    import javax.ejb.Singleton;
    import javax.ejb.Timeout;
    import javax.ejb.Timer;
    import javax.ejb.TimerService;
    
    @Singleton
    public class MyTimer {
        @Resource
        private TimerService timerService;
    
        @Timeout
        public void timeout(Timer timer) {
            System.out.println("TimerBean: timeout occurred");
        }
    
        public void schedule(Date start, long intervalMilis) {
            try{
                timerService.createTimer(start, intervalMilis, "my timer");            
            } catch (EJBException|IllegalArgumentException|IllegalStateException ex)  {
                Logger.getLogger(MyTimer.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
    

    更多信息请参见https://docs.oracle.com/javaee/7/tutorial/ejb-basicexamples004.htm

    【讨论】:

      【解决方案2】:

      类似于 Vegard 提交的答案,除了这个使用 ScheduleExpression 的答案。 更多信息Beginning Java EE 7 book

      import javax.ejb.ScheduleExpression;
      
      @Singleton
      public class MyTimer {
      
          @Resource
          private TimerService timerService;
      
          @Timeout
          public void timeout(Timer timer) {
              System.out.println("TimerBean: timeout occurred");
          }
      
          public void schedule(String DOW, String H, String M, String S) {
              try{
                  ScheduleExpression scheduleExpression = new ScheduleExpression();
                  scheduleExpression.dayOfWeek(DOW); 
                  scheduleExpression.hour(H); 
                  scheduleExpression.minute(M); 
                  scheduleExpression.second(S); 
      
                  timerService.createCalendarTimer(scheduleExpression, new TimerConfig(this, false));
              } catch (EJBException|IllegalArgumentException|IllegalStateException ex)  {
                  Logger.getLogger(MyTimer.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-07-10
        • 2012-03-20
        • 1970-01-01
        • 2010-10-02
        • 2021-09-22
        • 2011-06-03
        • 1970-01-01
        相关资源
        最近更新 更多