How to connect Standalone Application with DataSource / EJB Beans / any Container Resources ?.
Java Naming and Directory Interface (JNDI)
An application client helps to connect to container resources by referring resources in config xmls. We really not interested in doing all this magic. We know the resource(DataSource/Beans/JMS QUeue/topic) type and the location where it is located.
A JNDI(Java Naming and Directory Interface) name is the reference name specified in container to refer the resource from local and remote.
A J2EE application client is a standalone program launched from the command line or desktop, and typically accesses Enterprise JavaBean programs running on the J2EE application server. In Application client, we have to bundle an application such a way that to access the beans. Please refer for more info J2EE Application Client.
Again this is hectic way of accessing the resources from Application container, then what will I do ?.
yeah, we have the answer for this. Using RMI protocol, we can access any of the Beans/DataSource/Topic/Queue from server.
The following four properties and requesting object executables(classes) in your class path is enough to communicate with server.
java.naming.factory.initial=oracle.j2ee.rmi.RMIInitialContextFactory
java.naming.provider.url=ormi://localhost:23791/
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome1
The above mentioned properties will vary based on what kind of Application server, we are using. Here, I have taken Oracle 10.1.3 Application server OC4J to explain.
we can specify these properties in either of three approaches
1. Programmatically including these attributes
In this Approach, we have to always pass a Hashtable to create a Initialcontext. That HashTable has to store all these properties and values. Most of the time this approach is not suggested, because of the reason we have to modify these values(hard coded values) or we have to pick from external environment and given to Hash table
Hashtable env = new Hashtable(); env.put("java.naming.factory.initial","oracle.j2ee.rmi.RMIInitialContextFactory"); env.put(java.naming.provider.url","ormi://localhost:23791/"); env.put(java.naming.security.principal","oc4jadmin"); env.put(java.naming.security.credentials","welcome1");
2. Specifying in Java Command
Other way is directly specifying in Java command using -D option.
java -Djava.naming.factory.initial=oracle.j2ee.rmi.RMIInitialContextFactory \ -Djava.naming.provider.url=ormi://localhost:23791/ \ -Djava.naming.security.principal=oc4jadmin \ -Djava.naming.security.credentials=welcome1 \ <javaprogram>
3. Specifying in jndi.properties file
Best way of segregating configuration and application is specify these configuration details separately in a proprty file and name it as jndi.properties. This file containing folder has to be included in classpath.
java -cp .;%cp% yourapplication
Note:
This %cp% should load all of your application required classes containing jar files.
I am not able to access some of my beans, what to do ?.
Check java.naming.provider.url is correct or not. If you are accessing beans/topic/queue then you have to specify EJB Application also in this URL. Example, If your EJB Application name is Bank then specify like java.naming.provider.url=ormi://localhost:23791/Bank
Refer Specifying Environment Properties
Weblogic
Weblogic uses t3 protocol to make RMI connection:
java.naming.provider.url=t3://<hostname>:7001
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.security.principal=weblogic
java.naming.security.credentials=weblogic1
1 comment:
I know this thread is a little old, but can you provide what the classpath is for a client that needs to access a container provided data source?
Also, if there are any additional configuration files needed, that would be helpful.
I've been trying to resolve an issue where we have TWO versions of the same code because the web version uses a datasource, and the client uses a JDBC URL. It doesn't make sense to have two versions. I was thinking of possibly using Spring, but it would require more rewriting. I thought this would be something that was mainly CONFIGURATION.
Thanks.
egivler at pa dot gov
Post a Comment