InitialContext and Lookup

Source: Internet
Author: User
Tags jboss

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

Recently because the work need to start learning Ejb3, met a let me very depressed things, do a little summary--watch out for new InitialContext ().

When doing the client, discover the connection server, search the database, and then return the result set. Each time the execution, the first time will always take more time, and then each time the operation will be much faster. A lot of methods were found during the period. The first thought was that the EJB server set the service consumption time, and later felt that it was not right, because when JBoss started, the service was started. After a week of troubleshooting, it turns out that the new InitialContext () consumes a lot of time, and then the lookup () method consumes a certain amount of time. Where the network is in good condition, each time the new InitialContext () method takes about 100 milliseconds to 200 milliseconds, and each lookup () takes about 10 milliseconds to 30 milliseconds. Therefore, the decision to optimize the code, the creation of the Ejbhomefactory tool class, the use of a singleton mode, you are welcome to advise. The following is the code for this class:

Java code
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 Ejbhomefactory ();
}
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 Ejbhomefactory () 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's file contents are as follows:

Java code
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 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 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);
}
}



Then call this:

Java code
Ejbhomefactory f = ejbhomefactory.getinstance ();
Object o = f.lookup (remote);

Ejbhomefactory f = ejbhomefactory.getinstance ();
Object o = f.lookup (remote);

This ensures that the context instance is initialized only once, saving a lot of time.

Share to: Use of Load-on-startup | Getting Started with Java cloning and in-depth
    • 2010-06-01 13:53
    • Browse 19438
    • Comments (2)
    • Category: Programming languages
    • Related recommendations
Reviews 2 Floor Surpassno 2013-08-29 1 floor ybzshizds 2010-08-06 This Factory mode solution in fact there is still a problem, that is, after the EJB container restarts, the client should also go back to the new InitialContext, otherwise with the original InitialContext go to lookup and throw a wrong.

After thinking about it, I solved it this way.

For each ejbhome, create a new Ejbhelper class

Such as:

Java code
  1. Package com.company.vas.ejb.helper;
  2. Import java.rmi.RemoteException;
  3. Import javax.ejb.CreateException;
  4. Import javax.naming.NamingException;
  5. Import Com.company.util.Log;
  6. Import Com.company.vas.ejb.Invoice;
  7. Import Com.company.vas.ejb.home.InvoiceHome;
  8. Public class Invoicehelper {
  9. private static final String class_name = "Invoicehelpler";
  10. private static Invoicehome home;
  11. public static Invoice Getinvoice () {
  12. try {
  13. if (home = = null) {
  14. Home = (invoicehome) ejbgetter.getejbhome (
  15. Iinvoice.jndi_name, Invoicehome.  class);
  16. }
  17. return home.create ();
  18. } catch (Namingexception e) {
  19. Log.error (class_name, "Getinvoice ()", E.getmessage ());
  20. home = null;
  21. } catch (RemoteException e) {
  22. Log.error (class_name, "Getinvoice ()", E.getmessage ());
  23. home = null;
  24. } catch (CreateException e) {
  25. Log.error (class_name, "Getinvoice ()", E.getmessage ());
  26. home = null;
  27. }
  28. return null;
  29. }
  30. }
  31. Package com.company.vas.ejb.helper;
  32. Import java.util.Properties;
  33. Import Javax.ejb.EJBHome;
  34. Import Javax.naming.Context;
  35. Import Javax.naming.InitialContext;
  36. Import javax.naming.NamingException;
  37. Public class Ejbgetter {
  38. public static EJBHome getejbhome (String service_jndiname, Class homeinterface) throws namingexception{
  39. Properties env = new properties ();
  40. Env.put (Context.initial_context_factory, "org.jnp.interfaces.NamingContextFactory");
  41. Env.put (Context.provider_url, "192.168.60.120:1099");
  42. Env.put ("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
  43. Context IC = new InitialContext (env);
  44. EJBHome EJBHome = (EJBHome) javax.rmi.PortableRemoteObject.narrow (Ic.lookup (service_jndiname), homeinterface);
  45. return ejbHome;
  46. }
  47. }

InitialContext and Lookup

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.