How to check jndi bind/unbind status?
I'm looking for way of checking bind status of object and appropriate JNDI name.
For example, I've got some LDAP jms queue name: "/TheRootContext/SomeSubContext/SOME.QUEUE.NAME:queue"
I need to check that appropriate queue exists and it is binded with passed name.
What will be the correct way to check "bind status"?
I see such algorithm:
- Perform jndi lookup to ensure that provided name exists.
Code:
Object obj;
try {
obj = ctx.lookup("/TheRootContext/SomeSubContext/SOME.QUEUE.NAME:queue");
} catch(NamingException ex) {
// process naming exception: name not found, name syntax incorrect, ...
}
- %I don't know what to do next%, but suggest something like checking that:
Code:
obj != null && obj instanceof javax.jms.Queue
===
Another way, suggested here: java - How to check jndi bind/unbind status? - Stack Overflow
is to use listBindings method, passing to it parent context name: and checking what interested binding present in returned NamingEnumeration. But this is actually the same as previous method. This way something like this should be done:
Code:
NamingEnumeration bindings;
try {
bindings = ctx.listBindings("/TheRootContext/SomeSubContext/SOME.QUEUE.NAME");
} catch(NamingException ex) {
// process naming exception: name not found, name syntax incorrect, ...
}
boolean isFound, isBinded;
while (bindings!=null && bindings.hasMore()) {
Binding bd = (Binding) bindings.next();
if(bd.getName().equals("queue")) {
isFound = true;
isBinded = bd.getObject() != null && bd.getObject() instanceof javax.jms.Queue) ;
break;
}
}
System.out.println("Is binded: " + isFound && isBinded);
So, what is the correct way?