About JNDI and EJB

Source: Internet
Author: User
JNDI (the Java Naming and Directory Interface, Java
Naming and directory interfaces)
Is a group in Java
APIS for accessing the naming and directory services in the application. It provides developers with a universal and unified way to search for and access various naming and directory services. Using JNDI
Provides interfaces to locate users, machines, networks, and object services by name. Naming Service: Like DNS, most J2EE
All servers contain named servers. Directory Service: A simplified RDBMS
The system saves some simple information through the properties of the directory. Directory Services are implemented through directory servers, such as Microsoft Active Directory
. JNDIBenefits :(1) it contains a large number of naming and directory services and can use the same API
Call to access any naming or directory service. (2) Multiple naming and directory services can be connected at the same time. (3) allow the same name as Java
Objects or resources are associated, so you do not have to know the physical ID of objects or resources. (4) using common interfaces to access different types of Directory Services (5) enables developers to use and implement one type of naming or directory service customer APIs in a centralized manner
. Context: from 0
Or multiple bindings. For example, Java/MySQL, Java
Is the context, MySQL
Subconext: context under the context. For example, myjnditree/EJB/hellobean and EJB
Sub-context. Because JNDI
Is a set of interfaces, so we only need to program according to interface specifications. To pass JNDI
For resource access, we must set the initialization context parameters
, Mainly set JNDI
Driver Class Name (Java. Naming. Factory. Initial)
And the URL (Java. Naming. provider. url) that provides the Naming Service)
. Because JNDI
There are many implementation products. So java. Naming. Factory. Initial
The value
Different servers, java. Naming. provider. url
The value includes the host address and port number that provide the Naming Service. Access JBoss
Example code of the server:
Properties props = new Properties();props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");props.setProperty("java.naming.provider.url", "localhost:1099");InitialContext = new InitialContext(props);HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");

Access sun
Sample Code of the Application Server:

Properties props = new Properties();props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");props.setProperty("java.naming.provider.url", "localhost:3700");InitialContext = new InitialContext(props);HelloWorld helloworld = (HelloWorld) ctx.lookup("com.foshanshop.ejb3.HelloWorld");

Access weblogic10
Sample Code of the Application Server:

Properties props = new Properties();props.setProperty("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");props.setProperty("java.naming.provider.url", "t3://localhost:7001");InitialContext = new InitialContext(props);HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean#com.foshanshop.ejb3.HelloWorld");

JBossEnvironmentJNDI
Naming Conventions of the tree: (1) Java: Copm the Context Environment and Its subcontext environment can only be accessed and used by specific application components related to it (2) Java: the sub-Context Environment and bound object can only be JBoss
Application access in the Server Virtual Machine (3) other context environments can be called by remote users as long as serialization is implemented.

After you release EJB to JBoss, you can In JBoss
Management Platform to view their JNDI
Name
, Enter the following URL
Http: // localhost: 8080/JMX-console/, click "service = jndiview"
Link, find the "List ()" method in the "List of mbean operations:" column in the displayed page, click "INVOKE" button, and you will see helloworld in the following interface.
Session Bean
The JNDI path of, JNDI
The rule for creating a path name is "upper layer name/lower layer name, separated by"/"between each layer. Helloworld
Session Bean
The JNDI path name of is helloworldbean/remote. JBoss EJB JNDI
Default naming rules
The naming rules are as follows: 1> If EJB
Package and suffix *. Ear
J2EE
Publish a file, the default JNDI
The path name is the access local interface: Ear-file-base-name/EJB-CLASS-NAME/local access remote interface: Ear-file-base-name/EJB-CLASS-NAME/remote example: EJB helloworld
Package it into helloworld. Ear
J2EE
Application, access the JNDI of her remote interface
Name: helloworld/helloworldbean/remote 2> If the EJB
Package the application with the suffix *. jar.
,
Default JNDI
The path name is the access local interface: EJB-CLASS-NAME/local access remote interface: EJB-CLASS-NAME/remote example: helloworld
Package the application into helloworld. Jar
File to access the JNDI of her remote interface
Helloworldbean/remote Note: EJB-CLASS-NAME
Is without the package name, such as com. foshanshop. ejb3.impl. helloworldbean
You only need to retrieve helloworldbean. At present, many textbooks on the Internet get JNDI
The path name method is not applicable to JBoss.
For example, helloworld = (helloworld) CTX. Lookup (helloworld. Class. getname (); this method applies to Sun Application Server
And glassfish

 

Custom JNDI
Name
Default JNDI
The naming rules have been described above, but in some cases you need to customize the name. In JBoss
Medium
To customize JNDI
Name, which can be @ localbinding
And @ remotebinding
Note the key code (only add the binding comment before the bean Interface ):
Import Org. JBoss. annotation. EJB. remotebinding ;... @ remote @ remotebinding (jndibinding = "testbinding/myaccount") public interface myaccount extends serializable {publicint add (int A, int B); publicint getresult ();} the above EJB code is called on the client: initialcontext CTX = new initialcontext (props); myaccount bean1 = (myaccount) CTX. lookup ("testbinding/myaccount ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.