【问题标题】:Starting a EJB with timer when application starts应用程序启动时使用计时器启动 EJB
【发布时间】:2015-12-10 17:41:24
【问题描述】:

我想在 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 中调用它?

【问题讨论】:

  • ServletContextListener?

标签: java ejb weblogic ejb-3.0


【解决方案1】:

你需要使用一个servlet来触发你的ejb

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

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

类似这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多