JNDI lookup in ServletContextListener.contextDestroyed
-
02-07-2021 - |
Question
I'm using JBoss 7.1.1 and servlet-api 2.5.
I have to shutdown some objects which located in JNDI on application shutdown.
I'm using ServletContextListener
for this purposes:
public class MyServletContextListener implements ServletContextListener {
...
@Override
public void contextDestroyed(ServletContextEvent sce) {
((TaskClient) new InitialContext().lookup("myName")).disconnect();
}
}
But I get exception:
Error looking up myName, service service jboss.naming.context.java.myName is not started
If I try to lookup the object when application is running everything is fine.
Thank you for any help.
update
How I bind data to jndi:
public class MyJbpmServletContextListener implements ServletContextListener {
public static final String TASK_CLIENT_JNDI_NAME = "myJbpmTaskClient";
private Log logger = SLF4JLogFactory.getLog(getClass());
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
TaskClient tc = ...
// long initialization of TaskClient
InitialContext context = new InitialContext();
context.bind(TASK_CLIENT_JNDI_NAME, client);
} catch (NamingException exception) {
logger.error("Cannot bind task client", exception);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
try {
((TaskClient) new InitialContext().lookup(TASK_CLIENT_JNDI_NAME)).disconnect();
} catch (NamingException exception) {
logger.error("Cannot obtain task client", exception);
}
}
}
The object is accessible on following path TASK_CLIENT_JNDI_NAME
when application runs. But when contextDestroyed
called I have NamingException
.
I don't know how and when it's destroyed. I only sure that I'm not rebind or unbind it.
update2
I also tried to use jndi paths java:comp/myName
, java:comp/env/myName
, java:/myName
, java:jboss/myName
. Behavior is the same: it possible to lookup object when application runs and unable to do it when ServletContextListener.contextDestroyed
method called.
Solution
Looks like a JBoss bug to me, I created AS7-5746