Service locator for ejb, Uddi for ejb


When we are client EJB we must know server IP address where are EJB components deployed. When we use ESB we have EDDI and client don't have to know IP servers where ESB services are deployed. I solved this problem and I wrote simple project. similar to Uddi for ESB.

How does it work?
On all servers where are deployed EJB and where are clients of this EJB (for example WAR projects) you have to deploy my application, and it connects to database and writes EJB locations. The only one condition is that all EJB must bind to jndi name which starts with 'ejb/....', for example: 'ejb/com/company/project/UserBean', it will not works for jndi name 'UserBean'.

How to install?
Create database and table on mysql database:

CREATE TABLE IF NOT EXISTS `locations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`jndi` varchar(100) NOT NULL,
`host` varchar(100) NOT NULL,
`date` datetime NOT NULL,
`port` varchar(5) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


Copy to your deploy catalog on jboss 2 files : servicelocator.war and servicelocator-ds.xml and configure this datasource.

How to use?
On client application, for example in war applications create class:

package com.company.project;

import java.util.Map;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

public class ServiceLocator {

public static Object getEjbInterface(String jndi) throws Exception{
InitialContext ic = new InitialContext();
Map map = (Map)ic.lookup("ejb_locator");

String hosts = map.get(jndi);

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
p.put(Context.PROVIDER_URL, hosts);
InitialContext ic2 = new InitialContext(p);
return ic2.lookup(jndi);
}
}
--------------------------------------------------------------------------

And now you can use it:

String jndi = "ejb/pl/com/zagiel/HelloBean";
HelloLocal hello = (HelloLocal)ServiceLocator.getEjbInterface(jndi);
hello.sayHello();


See also:
Hibernate vs jdbc
tomcat monitoring