简体   繁体   English

JNDI查找在java.util.TimerTask中失败

[英]JNDI look up is failing with in java.util.TimerTask

public class ReferenceDataTimer extends TimerTask
{
   private static ReferenceDataTimer refDataTimerTask = new ReferenceDataTimer();
   private Timer refDataTimer = null;
   public void start()
   {
        refDataTimer = new Timer();
        refDataTimer.schedule(refDataTimerTask, DELAY, refreshTime*PERIOD);
   }

   @Override
   public void run()
   {
    // Get DB connection using 
    // InitialContext lookup 
    // using resource reference             

    => This is where it is failing. lookup is not working from this run() method of timer task.

    // Then load the data to cache

   }
}

public class ReferenceDataManager implements ServletContextListener {
 // Invoking ReferenceDataTimer -> start() method.
}

And Im getting JNDI lookup error while getting data source Connection from server context using a resource reference. 我在使用资源引用从服务器上下文获取数据源连接时收到JNDI查找错误。 Please note that look up IS WORKING if I don't use TimerTask . 请注意,如果我不使用TimerTask ,则查找工作正常。

javax.naming.ConfigurationException: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component. javax.naming.ConfigurationException:无法完成对“ java:”名称的JNDI操作,因为服务器运行时无法将操作的线程与任何J2​​EE应用程序组件相关联。 This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request. 当未在服务器应用程序请求的线程上执行使用“ java:”名称的JNDI客户端时,会发生这种情况。 Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE application. 确保J2EE应用程序不在静态代码块内或该J2EE应用程序创建的线程中的“ java:”名称上执行JNDI操作。 Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on "java:" names. 这样的代码不一定在服务器应用程序请求的线程上运行,因此JNDI操作对“ java:”名称不支持。

Can any one please suggest? 有人可以建议吗? If it is not possible to get resource (available in server context) with in java.util.Timer task run() method then please suggest an alternative. 如果无法使用java.util.Timer任务run()方法获取资源(在服务器上下文中可用),请提出一个替代方案。 I even tried with java.util.concurrent.ScheduledExecutorService -> scheduleAtFixedRate but in vain. 我什至尝试使用java.util.concurrent.ScheduledExecutorService > scheduleAtFixedRate但徒劳。

You should not use TimerTask in an EJB application as this will create and start a custom Thread which is not allowed in a Java EE environment. 您不应在EJB应用程序中使用TimerTask ,因为这将创建并启动Java EE环境中不允许的自定义Thread

Instead, use the TimerService . 而是使用TimerService It is part of the Java EE standard. 它是Java EE标准的一部分。

http://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html http://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM