JNDI
Java terminology
Java Naming and Directory Interface
Glossary: A group of APIs that help you create multiple naming and directory service interfaces.
JNDI (Java Naming and Directory Interface) is a standard Java Naming System Interface provided by SUN. JNDI provides a unified client API through the implementation of jndi spi through different access provider interfaces, the Administrator maps the jndi api to a specific naming service and directory system, so that Java applications can interact with these naming services and directory services. Cluster JNDI implements high-reliability JNDI [8]. Through the cluster of the server, the load balancing and error recovery of JNDI are ensured. In the global sharing mode, an application server in the cluster ensures the independence of the local JNDI tree and has a global JNDI tree. Each application server binds the deployed service object to its local JNDI tree and a shared global JNDI tree to realize the connection between global JNDI and its own JNDI.
Java Naming and Directory Interface is an API designed for applications. It provides developers with a universal and unified Interface for searching and accessing various Naming and Directory services, JDBC is built on the abstraction layer.
The available directories and services that can be accessed by JNDI are:
DNS, XNam, Novell Directory Service, LDAP (Lightweight Directory Access Protocol light Directory Access Protocol), the Registry, RMI, DSML v1 & v2, and NIS of the CORBA object service, file system, Windows XP/2000/NT/Me/9x.
JNDI advantages:
It contains a large number of naming and directory services and uses common interfaces to access different types of services;
You can connect to multiple naming or directory services at the same time;
Establish a logical association. The name can be associated with a Java object or resource without guiding the physical ID of the object or resource.
JNDI package:
Javax. naming: Name operation;
Javax. naming. directory: directory operation;
Javax. naming. event: Request event Notification in the named directory server;
Javax. naming. ldap: Provides LDAP support;
Javax. naming. spi: allows dynamic insertion of different implementations.
The JNDI naming and service functions are used to provide enterprise-level APIs access to naming and services. For example, EJBs, JMS, JDBC 2.0, and RMI on IIOP use the name service of CORBA through JNDI.
JNDI and JDBC:
JNDI provides a unified method for searching and accessing services on the network. By specifying a resource name, the name corresponds to a record in the database or naming service, and returns the information required for database connection establishment.
Sample Code:
Try {
Context cntxt = new InitialContext ();
DataSource ds = (DataSource) cntxt. lookup ("jdbc/dpt ");
}
Catch (NamingException ne ){
...
}
JNDI and JMS:
Message Communication is a method used by software components or applications to communicate. JMS is a JAVA technology that allows applications to create, send, receive, and read messages.
Sample Code:
Try {
Properties env = new Properties ();
InitialContext inictxt = new InitialContext (env );
TopicConnectionFactory connFactory = (TopicConnectionFactory) inictxt. lookup ("TTopicConnectionFactory ");
...
}
Catch (NamingException ne ){
...
}
Access a specific directory: for example, a person is an object and has several attributes, such as the name, phone number, email address, and zip code of the person. GetAttributes () method
Attribute attr =
Directory. getAttributes (personName). get ("email ");
String email = (String) attr. get ();
By using JNDI, the customer can use the object name or attribute to find the object:
Foxes = directory. search ("o = Wiz, c = US", "sn = Fox", controls );
Use JNDI to search for objects such as printers and databases and the printer example:
Printer printer = (Printer) namespace. lookup (printerName );
Printer. print (document );
Browse namespace:
NamingEnumeration list = namespace. list ("o = Widget, c = US ");
While (list. hasMore ()){
NameClassPair entry = (NameClassPair) list. next ();
Display (entry. getName (), entry. getClassName ());
}
References:
Http://java.sun.com/products/jndi/examples.html
Http://java.sun.com/products/jndi/serviceproviders.html
Common JNDI operations:
Common JNDI operations:
Void bind (String sName, Object object);-binding: Process of associating names with objects
Void rebind (String sName, Object object);-rebind: Used to rebind an Object with an existing name
Void unbind (String sName);-release: Used to release an object from the directory.
Void lookup (String sName, Object object);-query: returns an Object of the total directory.
Void rename (String sOldName, String sNewName);-rename: used to modify the name bound to the object name
NamingEnumeration listBinding (String sName);-list: list of objects bound to a specific context
NamingEnumeration list (String sName );
Code example: Get the name, class name, and bound object again.
NamingEnumeration namEnumList = ctxt. listBinding ("cntxtName ");
...
While (namEnumList. hasMore ()){
Binding bnd = (Binding) namEnumList. next ();
String sObjName = bnd. getName ();
String sClassName = bnd. getClassName ();
SomeObject objLocal = (SomeObject) bnd. getObject ();
}