FreeKB - Name space bindings from WebSphere
Name space bindings from WebSphere

Home > Search


To understand how a name space binding works in WebSphere, let's consider a scenario where an application will be deployed to a few different WebSphere application servers, where one application server is being used for development (dev), one is used for integration testing (int), and one is used for production (prod).

 

If the app has markup that is environment specific, this creates challenges and introduces risk, as the markup of the application would need to be modified for each environment. For example, the below markup includes "dev" for the development environment. 

private String username = "dev_username";
private String password = "dev_password";
private String jdbc     = "jdbc:mysql://dev.example.com:3306/dev_database"; 

 

Instead of putting environment specific markup in the app, there are a few options to consider. One option is to create a variable in the application server that contains the application. This is a really good approach when the variable is unique to a single application server in the WebSphere cell.

 

However, this approach doesn't make much sense when you have numerous application servers, since each application server maintains it's own set of variables. In this design, you could easily end up with identical variables across multiple application servers.

 

Fortunately, IBM accounted for this challenge. What you can do is to create a name space binding at the cell scope, so that all of the application servers and applications in the cell can use the name space binding. A name space binding is like a variable, in that it is a key-value pair that can be used in a Java app to return a string.

 

It is also noteworthy that a properties file could be used, but it usually does not make much sense to use a properties file, since WebSphere can take care of this using variables or name space binding. Additionally, less markup is needed in the Java app when using WebSphere variables or name space binding.

 


Create a name space binding

  1. In the left panel of the WebSphere admin console, expand Environment > Naming, and select Name space bindings.
  2. Select a scope for the name space binding.
  3. Select New.
  4. Select String or EJB or CORBA or Indirect and select Next. In this tutorial, String is selected.
  5. For Binding Identifier and Name in name space relative to lookup name prefix 'cell/persistent/', enter a key (Greeting in this example).
  6. For String value, enter a value (Hello World in this example).
  7. Select Next.
  8. Select Finish.
  9. Select Save.

 

In this example, a name space binding named Greeting with a value of Hello World was created.

 

Restart the JVMs that contain the application that will use the name space binding. Once this is done, you shouldn't need to restart the JVMs again, as changes to the name space binding value will happen dynamically. In other words, if you change the value to "Greetings Earth", the applications using the name space binding will start to use "Greetings Earth" instead of "Hello World".

The name space bindings will be stored in the namebinding.xml file, which is located at ${was_install_root}/profiles/your_profile/config/cells/your_cell/namebinding.xml.

~]# cat namebindings.xml
<namebinding:String name="Greeting" value="Hello World" />

 

You should now be able to use the name space binding in your Java app. In this example, a string object called "sayHi" is created, and the object will obtain the value associated with the Greeting key (Hello World).

InitialContext ctx = new InitialContext();
String sayHi = (String)ctx.lookup("cell/persistent/Greeting");

 

The Hello World value can then be printed.

print sayHi;

 


Liberty

If using Liberty, the following can be added to server.xml to create the key-value pair.

<featureManager>
 <feature>jndi-1.0</feature>
</featureManager>

<jndiEntry jndiName="cell/persistent/Greeting" value="Hello World"/>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 0014a in the box below so that we can be sure you are a human.




Comments