应用程序启动时使用计时器启动EJB

时间:2015-12-10 17:41:24

标签: java ejb weblogic ejb-3.0

我想在Weblogic 11G上使用计时器启动EJB 3.0,但我不能使用PostConstruct

当应用程序启动时,我可以做些什么来启动这个EJB?

@Resource TimerService timerService;

@PostConstruct
public void initialize() {


}

@Timeout
public void timeout(Timer timer)
{
    System.out.println("Timeout occurred !!!");
    if (timerService.getTimers().size() <= 1) {
        Timer newtimer = timerService.createTimer(5000,"Clean Timer");
    }

}


@Override
public void inicia() {
    if (timerService.getTimers().size() == 0) {
        Timer timer = timerService.createTimer(5000,"Clean Timer");
    }
}

也许在PostConstruct中的另一个EJB中我可以调用它吗?

1 个答案:

答案 0 :(得分:1)

您需要使用servlet来触发您的ejb

  1. 创建一个servlet并覆盖init方法

  2. 在init方法中对您的ejb执行JNDI查找并调用metod

  3. 类似的东西:

    fit <- lm(Ozone ~ Solar.R + Wind + Temp,data=airquality)
    visreg(fit, xlab="Distance (m)", 
           ylab=expression(italic("Sciurus vulgaris  ") (Presence/Absence)))
    

    或切换到ejb 3.1,如果可以,并执行以下操作:

    public class FilesystemCleanerServlet
        extends HttpServlet
    {
    
        private static final long serialVersionUID = 3555552219242063583L;
    
        private final Logger LOG = LoggerFactory.getLogger(this.getClass());
    
        public void init(ServletConfig sc) throws ServletException
        {
            super.init(sc);
    
            try
            {
                InitialContext ctx = new InitialContext();
                Object o =  ctx.lookup( "java:comp/env/ejb/WBFilesystemCleaner" );
            }
            catch(Exception e)
            {
                LOG.error( e.getMessage() ,e );
            }
    
        }   
    }