当前位置: 动力学知识库 > 问答 > 编程问答 >

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. Please note that look up IS WORKING if I don't use 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. This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request. 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. 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.

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.

I even tried with java.util.concurrent.ScheduledExecutorService -> scheduleAtFixedRate but in vain.

网友答案:

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.

Instead, use the TimerService. It is part of the Java EE standard.

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: