Home » Enterprise Java » naming » Locating adding replacing removing and renaming a binding in the Naming service

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Locating adding replacing removing and renaming a binding in the Naming service

With this example we are going to show you how to perform basic operations over a binding in a Naming service, that is locating adding replacing removing and renaming a binding in the Naming service.

In order to run this example you should first follow the one describing how to implement a simple “calculator” service capable of RMI invocations here.

You must implement, compile, create the CalculatorServiceImpl “stub” class and launch the rmiregistry to use its Naming service.

Let’s see the code:

package com.javacodegeeks.snippets.enterprise;
 
import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

public class AddLookupReplaceRemoveRenameBindings {
 
  public static void main(String[] args) {

    try {

  

  /*

    * This example uses the RMI Registry naming service provider running on 

    * local host and on the default port (1099)

    * 

    * For connecting to alternate naming service providers you should construct

    * a Hashtable containing appropriate environment directives and initialize the 

    * InitialContext Object using the InitialContext(Hashtable env) constructor 

    * injecting the environment table.

    * 

    * For example; connecting to JBoss naming service running on local host the 

    * environment that should be created is like the one shown below :

    * 

    * Hashtable env = new Hashtable();

    * env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

    * env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

    * env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

    * 

    */

    Hashtable env = new Hashtable();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");

    env.put(Context.PROVIDER_URL, "rmi://localhost:1099");

    Context ctx = new InitialContext(env);


    System.out.println("Initial Context created successfully");

    

    // Add a binding. Object to bind must be Remote, Reference, or Referenceable

    ctx.bind("Calculator_Service", new CalculatorServiceImpl());

    

    // Display all bindings of the context

    NamingEnumeration bindings = ctx.listBindings(ctx.getNameInNamespace());

    System.out.println("After adding 'Calculator_Service' binding, Naming Context contains :");

    while(bindings.hasMore()) {


  Binding binding = (Binding)bindings.next();


  System.out.println("Binding name : " + binding.getName() + ", Binding Object hashCode : " + binding.getObject().hashCode());

    }


    // Replace a binding. Object to bind must be Remote, Reference, or Referenceable

    ctx.rebind("Calculator_Service", new CalculatorServiceImpl());


    // Display all bindings of the context

    bindings = ctx.listBindings(ctx.getNameInNamespace());

    System.out.println("After rebinding 'Calculator_Service', Naming Context contains :");

    while(bindings.hasMore()) {


  Binding binding = (Binding)bindings.next();


  System.out.println("Binding name : " + binding.getName() + ", Binding Object hashCode : " + binding.getObject().hashCode());

    }

    

    // Looking up an object by its binding name

    CalculatorService calculator = (CalculatorService)ctx.lookup("Calculator_Service");

    System.out.println("Calculating 2 + 2 = " + calculator.add(2, 2));

    

    // Rename a binding

    ctx.rename("Calculator_Service", "New_Calculator_Service");

    

    // Display all bindings of the context

    bindings = ctx.listBindings(ctx.getNameInNamespace());

    System.out.println("After replacing 'Calculator_Service' with 'New_Calculator_Service' binding, Naming Context contains :");

    while(bindings.hasMore()) {


  Binding binding = (Binding)bindings.next();


  System.out.println("Binding name : " + binding.getName() + ", Binding Object hashCode : " + binding.getObject().hashCode());

    }

    

    // Looking up an object by its binding name

    calculator = (CalculatorService)ctx.lookup("New_Calculator_Service");

    System.out.println("Calculating 10 / 2 = " + calculator.div(10, 2));

    

    // Remove a binding

    ctx.unbind("New_Calculator_Service");


    // Display all bindings of the context

    bindings = ctx.listBindings(ctx.getNameInNamespace());

    System.out.println("After removing 'New_Calculator_Service' binding, Naming Context contains :");

    while(bindings.hasMore()) {


  Binding binding = (Binding)bindings.next();


  System.out.println("Binding name : " + binding.getName() + ", Binding Object hashCode : " + binding.getObject().hashCode());

    }


} catch (NamingException e) {

    System.out.println("Exception occurred while utilizing bindings : " + e.getMessage());

} catch (RemoteException e) {

    System.out.println("Could not create calculator service : " + e.getMessage());

}

 }

}

Example Output:

Initial Context created successfully
After adding 'Calculator_Service' binding, Naming Context contains :
Binding name : Calculator_Service, Binding Object hashCode : 853137463
After rebinding 'Calculator_Service', Naming Context contains :
Binding name : Calculator_Service, Binding Object hashCode : 394882503
Calculating 2 + 2 = 4
After replacing 'Calculator_Service' with 'New_Calculator_Service' binding, Naming Context contains :
Binding name : New_Calculator_Service, Binding Object hashCode : 394882503
Calculating 10 / 2 = 5.0
After removing 'New_Calculator_Service' binding, Naming Context contains :

 
This is an example on locating, adding, replacing, removing and renaming a binding in the Naming service.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments