JNDI Tutorial-JNDI Naming Service Example

In the previous chapter  we discussed the very basics of JNDI. In this chapter we are going to discuss a JNDI example .In this chapter we are looking into the JNDI naming service interface .

JNDI Naming Service Example

This example shows how an object can be bound to a name. And how to access the bound object using another application.

JBossNS- Overview

The example we are discussing is with JBoss server. JBoss is having  its own JNDI naming implementation-JBossNS. JBossNS is the Socket/RMI  implementation  of javax.naming.Context interface.If we bound an object and if we try to access the same from the  same machine, then socket is not creating. In such case , global singleton object reference is using.If we are accessing the object from another machine , the sockets would be created between two  machines.In such situation , the the class whose object is bound , should implement the Serializable interface.In such scenario, the serialVersionUID attribute should be same at both sides.This concept is explained in a previous chapter.

Lets start our discussion from the very basics of setting up the environment.Java EE can be downloaded  from here. Eclipse Indigo is using as IDE. It can be downloaded  from here .Now we need to have JBoss application server. I am going to use JBoss  version 4.2. We can download it from here.

Step 1. Open eclipse in any workspace . And create a dynamic web project  with JBoss 4.2.0 GA  as target run time.

2.Create a new JBoss instance and add our newly created project to the server.

3.Create package com.jndi.samples in src directory.

4.Now create the following classes.

In this example , first application  binding a Student object. The second application retrieves the bound object.

Student.java

import java.io.Serializable;

public class Student implements Serializable {

public Student(int id, String name) {
this.id = id;
this.name = name;
}

private static final long serialVersionUID = 1L;

private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public String toString(){
return "Id = "+getId() +"Name = "+getName();
}

}

Now let us see how to bind a student object using naming service. The FirstApp.java is a stand alone application which binds a Student object.

FirstApp.Java

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class FirstApp {
public void bindObject() {
Student student = new Student(1, "Bijoy");
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL,
"jnp://localhost:1099");
initialProperties.put(InitialContext.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
try {
Context context = new InitialContext(initialProperties);
context.bind("student", student);
System.out.println("Bound object = "+student);
} catch (NamingException e) {

e.printStackTrace();
}

}

public static void main(String[] args) {
new FirstApp().bindObject();
}
}

The bindObject() method creates a Context object and a  Student object is binding to the specified context . The initialProperties has the details for initializing the context.It is desirable to give these properties through jndi.properties in src folder , if we need these things in a generic way.

Now lets see the SecondApp.java. It simply receovers the already bound Student object.

SecondApp.java

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SecondApp {
public void readObject() {
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL,
"jnp://localhost:1099");
initialProperties.put(InitialContext.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
try {
Context context = new InitialContext(initialProperties);
Student student = (Student) context.lookup("student");
System.out.println("Object received from context = "+student);
} catch (NamingException e) {

e.printStackTrace();
}

}

public static void main(String[] args) {
new SecondApp().readObject();

}
}

Output

Build the workspace.And restart the server manually if restart does not happen automatically. Run the FirstApp.java as Java application.

Output of FirstApp.Java

Bound object = Id = 1Name = Bijoy

Now run SecondApp.java as java application.

Output of SecondApp.Java

Object received from context = Id = 1Name = Bijoy

See also:

JNDI overview

Accessing LDAP using JNDI

Updating LDAP attribute

Adding new attribute to LDAP entry using JNDI

Deleting attribute from LDAP entry

Adding LDAP entry using JNDI

Deleting LDAP entry using JNDI

Displaying user entries in LDAP using JNDI