Connect a Java client to a single server using weblogic JNDI

Source: Internet
Author: User

 

To interact with WebLogic Server, the Java client must be able to obtain the object reference of a remote object and call operations on the object. To complete this operation, the client application code must perform the following steps:

1. Set the JNDI environment attribute of initialcontext.

Listing 3-1 describes how to use the context. initial_context_factory attribute and context. provider_url attribute to obtain the context.

Listing 3-1 get Context

Context CTX = NULL;

Hashtable ht = new hashtable ();

Ht. Put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory ");

Ht. Put (context. provider_url, "T3: // localhost: 7001 ");

Try {

CTX = new initialcontext (HT );

// Use the context in the program

} Catch (namingexception e ){

// Fault

} Finally {

Try {CTX. Close ();}

Catch (exception e ){

// Fault

}

}

2. Create a context from a server object.

You may also need to create a context from an object (EJB) instantiated on the Java Virtual Machine of the WebLogic Server. When using a server object, you do not need to specify the context. provider_url attribute. The user name and password are required only when you log on with a specific user.

To create a context from a server object, you must first create an initialcontext, as shown below:

Context CTX = new initialcontext (); you do not need to specify the factory or provider URL. By default, the context is created as context and connected to the local Naming Service.

3. Use context to find the named objects in the WebLogic Server namespace.

Try {

Servicebean bean = (servicebean) CTX. Lookup ("EJB. servicebean ");

// The parameter passed to the Lookup () method is a string containing the required object name.

} Catch (namenotfoundexception e ){

// The binding does not exist.

} Catch (namingexception e ){

// Fault

}

4. Use a named object to obtain the reference of a remote object and call operations on the remote object.

The EJB client application obtains the object reference of the remote EJB object from the EJB home.

Listing 3-4 contains a code snippet that retrieves a remote object and then calls a method to the object.

Servicebean bean = servicebean. Home. Create ("EJB. servicebean ");

Servicebean. additem (66 );

5. Disable the context.

BEA System recommends that the client disable the context to release resources and avoid Memory leakage after the context is used to complete the work. We recommend that you use finally {} blocks and wrap the close () method in try {} blocks. If you try to disable a context that has never been instantiated due to an error, the Java client application will cause an exception. In listing 3-5, the client closes the context and releases the resources in use.

Listing 3-5 disable Context

Try {

CTX. Close ();

} Catch (){

// Fault

}

 

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.