Examples of Using JNDI in J2EE Applications


The EJB container in the application server is responsible for binding the EJB object with the naming service when the object is deployed. Therefore, the most common function of JNDI for J2EE clients is to look up the interface for a remote object. In the following examples, you can assume that the client is running on the same system as WebLogic Server. From this assumption, you construct the JNDI Context by simply using the default constructor for InitialContext . The default values for the environment apply under these conditions.

Looking Up the EJB Home Interface

The heart of a J2EE application is the EJB. The clients of the EJB use JNDI to look up the home interface. The home interface is then used to create an instance of the remote interface for the EJB. With EJB, the name to look up is the same as the name of the home interface class. Listing 14.10 shows an example of an EJB client using JNDI to look up the home interface.

Listing 14.10 An EJB Client Using JNDI to Look Up the Home Interface
 Context ctx = null; try {   ctx = new InitialContext();   MyEjbHome home = null;   Object homeObj = ctx.lookup("MyEjbHome");   home = (MyEjbHome)javax.rmi.PortableRemoteObject.narrow(homeObj, MyEjbHome.class); } catch (NamingException e) {   e.printStackTrace(); } catch( NamingException x ) {   System.err.println( x.toString() ); } 

Looking Up JMS Connection Factories and Distributed Destinations

The configuration of JMS includes connection factories and distributed destinations, each of which have a JNDI name. The connection factories enable JMS clients to create JMS connections. The distributed destinations are the JMS Queues and Topics.

For further information on JMS see Chapter 15, "The Java Messaging Service (JMS)," p. 471 .


Chapter 15 describes the details of JMS Queue Producer/Consumer and JMS Topic Producer/Consumer. Here, you see how to use JNDI to retrieve these services from the WebLogic naming service. The ConnectionFactory, Queue, and Topic are all retrieved by performing a JNDI lookup. The name of the default JMS connection factory is the full class name weblogic.jms.ConnectionFactory . The JNDI name for the Queue and Topic is manually administered through the WebLogic Server Console as part of the configuration for JMS. The client of the JMS Queue uses JNDI to look up the name, as shown in Listing 14.11.

Listing 14.11 A JMS Queue Client Using JNDI to Look Up the Name
 try { Context ctx = new InitialContext(); QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup( "weblogic.jms.ConnectionFactory" ); Queue queue = (Queue)ctx.lookup( "MyJmsQueue" ); QueueConnection qc = qcf.createQueueConnection(); // use the QueueConnection and message queue     } catch( NamingException x ) {         System.err.println( x.toString() );     } 

The JMS Topic Producer and Consumer use JNDI in a similar manner as the preceding Queue example, as shown in Listing 14.12.

Listing 14.12 JMS Topic Producer and Consumer Using JNDI to Look Up the Name
 try { Context ctx = new InitialContext(); TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup( "weblogic.jms.ConnectionFactory" ); Topic topic = (Topic)ctx.lookup( "MyJmsTopic" ); TopicConnection tc = qcf.createTopicConnection(); // use the QueueConnection and message queue     } catch( NamingException x ) {         System.err.println( x.toString() );     } 

Looking Up JDBC DataSources

Chapter 16, "Managing Java Transactions Using JTA," describes the advantages of using JDBC DataSources over the DriverManager . With WebLogic Server, the DataSource is bound to the naming service using the WebLogic Administrator Console. The client of the DataSource uses JNDI to look up the name, as shown in Listing 14.13.

Listing 14.13 A DataSource Client Using JNDI to Look Up the Name
 try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup( "MyJDBCDataSource" ); Connection con = ds.getConnection( DATABASE_USER, DATABASE_PASSWORD );     } catch( NamingException x ) {         System.err.println( x.toString() );     } 

Looking Up JTA UserTransactions

The UserTransaction object is covered in Chapter 16. In this case, there is no manual administration of the JNDI name; it is simply the full class name, javax.transaction.UserTransaction . The client that performs distributed transactions uses JNDI to look up the UserTransaction object, as shown in Listing 14.14.

Listing 14.14 A Client Performing Distributed Transaction Using JNDI to Look Up the UserTransaction Object
 try {     Context ctx = new InitialContext();     UserTransaction tx =         (UserTransaction)ctx.lookup( "javax.transaction.UserTransaction" );     tx.begin();    // start transaction     // locate DataSource, establish connection, execute operations, close connections     tx.commit();    // commit transactions } catch( Exception x ) {     System.err.println( "error has occurred" );     try {         tx.rollback();     } catch( javax.transaction.SystemException se ) { } } 


BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net