JNDI

JNDI is a standard Java API that is bundled with JDK1.3 and higher. JNDI provides a common interface to a variety of existing naming services: DNS, LDAP, Active Directory, RMI registry, COS registry, NIS, and file systems.

http://docs.jboss.org/jbossas/jboss4guide/r1/html/ch3.chapter.html

http://stackoverflow.com/questions/13643127/how-does-the-ejb-client-locate-the-ejb-server-without-url

Sample

Properties:

File test/Client.java

   1 package test;
   2 import java.security.Security;
   3 import java.util.Properties;
   4 import javax.naming.Context;
   5 import javax.naming.InitialContext;
   6 import org.allowed.bitarus.IWSTest;
   7                     
   8 public class Client
   9 {
  10     public static void main(String args[]) throws Exception
  11     {
  12         Properties env = new Properties();
  13         env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  14         env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
  15         env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
  16         InitialContext context = new InitialContext(env);
  17         IWSTest test = (IWSTest) context.lookup("WSTest/remote");
  18         System.out.println( test.helloWorld() );
  19     }
  20 }

Build:

   1 cd test #package folder
   2 javac  -cp .:test2-0.0.1.jar Client.java 

Run:

   1 java -cp .:/opt/jboss-5.1.0.GA/client/*:test/test2-0.0.1.jar test/Client # JBoss client Jars, EJB with interface IWSTest
   2 

Maven sample

Structure

.
|-- pom.xml
|-- src
|   `-- main
|       `-- java
|           `-- org
|               `-- allowed
|                   `-- bitarus
|                       `-- ejbclient
|                           `-- Client.java
`-- target
    |-- classes
    |   `-- org
    |       `-- allowed
    |           `-- bitarus
    |               `-- ejbclient
    |                   `-- Client.class
    |-- ejbclient-1.0.jar
    |-- maven-archiver
    |   `-- pom.properties
    `-- surefire

Content pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0" 
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   4         http://maven.apache.org/maven-v4_0_0.xsd">
   5   <modelVersion>4.0.0</modelVersion>
   6   <groupId>org.allowed.bitarus</groupId>
   7   <artifactId>ejbclient</artifactId>
   8   <packaging>jar</packaging>
   9   <version>1.0</version>
  10   <name>ejbclient</name>
  11   <url>http://maven.apache.org</url>
  12   <!-- 
  13        mkdir -p src/main/java/org/allowed/bitarus/ejbclient 
  14        mvn clean compile package
  15        java -classpath "/opt/jboss-5.1.0.GA/client/*:target/ejbclient-1.0.jar:/tmp/test2/target/test2-0.0.1.jar" org.allowed.bitarus.ejbclient.Client
  16   -->
  17   <build>
  18     <plugins>
  19       <plugin>
  20         <groupId>org.apache.maven.plugins</groupId>
  21         <artifactId>maven-jar-plugin</artifactId>
  22         <version>2.4</version>
  23         <configuration>
  24           <archive>
  25             <manifest>
  26               <mainClass>org.allowed.bitarus.ejbclient.Client</mainClass>
  27             </manifest>
  28           </archive>
  29         </configuration>
  30       </plugin>
  31     </plugins>
  32   </build>
  33   <dependencies>
  34     <dependency>
  35       <groupId>org.allowed.bitarus</groupId>
  36       <artifactId>test2</artifactId>
  37       <version>0.0.1</version>
  38     </dependency>
  39   </dependencies>  
  40 </project>

Content Client.java

   1 package org.allowed.bitarus.ejbclient;
   2 import java.security.Security;
   3 import java.util.Properties;
   4 import javax.naming.Context;
   5 import javax.naming.InitialContext;
   6 import org.allowed.bitarus.IWSTest;
   7                     
   8 public class Client
   9 {
  10     public static void main(String args[]) throws Exception
  11     {
  12         Properties env = new Properties();
  13         env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  14         env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
  15         env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
  16         InitialContext context = new InitialContext(env);
  17         IWSTest test = (IWSTest) context.lookup("WSTest/remote");
  18         System.out.println( test.helloWorld() );
  19     }
  20 }

Maven steps:

   1 mvn clean compile package

Run program:

   1 java -classpath "/opt/jboss-5.1.0.GA/client/*:target/ejbclient-1.0.jar:/tmp/test2/target/test2-0.0.1.jar" org.allowed.bitarus.ejbclient.Client

Java/JNDI (last edited 2023-05-29 09:04:38 by 127)