InitialContext and lookup), context. lookup

Source: Internet
Author: User

InitialContext and lookup (convert), context. lookup

Address: http://wxg6203.iteye.com/blog/680830

Recently, I started to learn Ejb3 because of my work. I encountered a very depressing problem and made a small summary-be careful with new InitialContext ().

When the client is running, it finds that it is connected to the server, searches for the database, and then returns the result set. During each execution, more time is required for the first time, and each operation is much faster. A lot of methods are found during this period, which won't work. At first, I thought it was time consuming the Ejb server to establish a service. Later I thought it was wrong because the service was started when Jboss was started. After a week of troubleshooting, we found that new InitialContext () consumes a lot of time, and the later lookup () method will consume a certain amount of time. When the network is in good condition, each new InitialContext () method takes about 100 milliseconds to 200 milliseconds, and each lookup () it may take 10 ms to 30 ms. Therefore, I decided to optimize the code and created the EJBHomeFactory tool class. I used the singleton mode. Thank you for your advice. This type of code is as follows:

Import javax. naming. initialContext; import javax. naming. namingException; import com. cithinc. util. tool; public class EJBHomeFactory {private static EJBHomeFactory instance; private InitialContext context; private EJBHomeFactory () throws NamingException {context = Tool. getInitialContext ();} public static EJBHomeFactory getInstance () throws NamingException {if (instance = null) {instance = new E JBHomeFactory ();} return instance;} public Object lookup (String jndiName) throws NamingException {Object obj = new Object (); obj = context. lookup (jndiName); return obj ;}} import javax. naming. initialContext; import javax. naming. namingException; import com. cithinc. util. tool; public class EJBHomeFactory {private static EJBHomeFactory instance; private InitialContext context; private EJBHomeFact Ory () throws NamingException {context = Tool. getInitialContext ();} public static EJBHomeFactory getInstance () throws NamingException {if (instance = null) {instance = new EJBHomeFactory ();} return instance ;} public Object lookup (String jndiName) throws NamingException {Object obj = new Object (); obj = context. lookup (jndiName); return obj ;}among them, Tool. java file content: Java code import java. util. ha Shtable; import javax. naming. context; import javax. naming. initialContext; import javax. naming. namingException; public class Tool {@ SuppressWarnings ("unchecked") public static InitialContext getInitialContext () throws NamingException {Hashtable environment = new Hashtable (); environment. put (Context. INITIAL_CONTEXT_FACTORY, "org. jnp. interfaces. namingContextFactory "); environment. put (Context. URL_PKG _ PREFIXES, "org. jboss. naming: org. jnp. interfaces "); environment. put (Context. PROVIDER_URL, "jnp: // 127.0.0.1: 1099"); return new InitialContext (environment) ;}} import java. util. hashtable; import javax. naming. context; import javax. naming. initialContext; import javax. naming. namingException; public class Tool {@ SuppressWarnings ("unchecked") public static InitialContext getInitialContext () throws Naming Exception {Hashtable environment = new Hashtable (); environment. put (Context. INITIAL_CONTEXT_FACTORY, "org. jnp. interfaces. namingContextFactory "); environment. put (Context. URL_PKG_PREFIXES, "org. jboss. naming: org. jnp. interfaces "); environment. put (Context. PROVIDER_URL, "jnp: // 127.0.0.1: 1099"); return new InitialContext (environment) ;}} then call: Java code EJBHomeFactory f = EJBHomeFactory. getInstance (); Obj Ect o = f. lookup (remote); EJBHomeFactory f = EJBHomeFactory. getInstance (); Object o = f. lookup (remote); this ensures that only the context instance is initialized once, saving a lot of time.

 

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.