2015-12-10 98 views
0

我想與Weblogic上11G計時器啓動EJB 3.0,但我不能用PostConstruct開始用計時器的EJB應用程序時開始

我能做些什麼來啓動這個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我可以調用它嗎?

+0

在'ServletContextListener'? – Kayaman

回答

1

你需要使用一個servlet來觸發你的EJB

  1. 創建一個servlet和重載init方法

  2. 裏面的init方法做一個JNDI查找對你EJB和調用梅託德

是這樣的:

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); 
     } 

    } 
} 

或切換到EJB 3.1,如果你可以做這樣的事情:

@Startup 
@Stateless 
public class RunAtBootEJB { 
@Schedule(second = "*", minute = "*", hour = "*", persistent = false) 
public void triggerLog() { 
    logging.info("printed each second"); 
} 
}