On 2002.10.23 17:19:41 -0400 Greg Turner wrote: > All > > I have been working on this problem. I would like to know what you > think of my solution. > > First of all, you can cache EJBHome objects, but not EntityBean or > SessionBean objects. There are many Locator patterns on the web such as > this one: > http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html > > There is one problem with them. That is, if the EJBHome object goes > stale for whatever reason, say its server goes down, then it does not > work, and you have to re-get the InitialContext and then re-get the home > object from the InitialContext after the EJBHome's server comes back up.
What happens if you make a "cluster" with just one jboss server? The clustered proxies take care of failover for you if there is more than one server, I wonder what happens if there is only one. I think this would also allow you to cache at least stateless session beans. david jencks > I have come up with what I think might be a solution. > > Here are 3 attached java source files. The EjbHomeLocator does the > caching of the EJBHome objects. But for a FooBean and FooHome, the > FooHomeService takes care of the stale EJBHome problem. My intention is > that a XDoclet task could be written that would generate the > FooHomeService.java source so that would not have to be written for each > bean. > > What do you think? > > > > > > Emerson Cargnin - SICREDI Servi�os wrote: > > > I have some questions regarding to caching homes and session beans in > > the remote client : > > > > 1- I already cache initicalcontext and homes in businness delegate, is > > it secure to cache the real session object as a static attribute in > > business delegate and reuse it across a long time ? > > > > 2- If i call the same session object instance simultaneously from two > > different threads it will serialize the business method calls? > > > > I made this question because we use a satellite line between web and > > ejb layers (bank office and central bank site) and it takes 1,5 > > seconds to the cached home to just create the session facade bean. > > > > if someone has any better approache, i'd appreciate. > > > > Thanks in advance > > > > > > xxxxxxxxxxxxxxxxxxxxxxxxxxxx > > | Emerson Cargnin | > > | Analista de Sistemas Sr. | > > | Tel : (051) 3358-4959 | > > | SICREDI Servi�os | > > | Porto Alegre - Brasil | > > |xxxxxxxxxxxxxxxxxxxxxxxxxx| > > > > > > > > ------------------------------------------------------- > > This sf.net email is sponsored by: Influence the future of Java(TM) > > technology. Join the Java Community Process(SM) (JCP(SM)) program now. > > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en > > > > _______________________________________________ > > JBoss-user mailing list > > [EMAIL PROTECTED] > > https://lists.sourceforge.net/lists/listinfo/jboss-user > > > > > > -- > Greg Turner, JBoss Authorized Consultant > > Tiburon Enterprise Systems > http://www.tiburon-e-systems.com > Box 1171 > Tiburon, CA 94920 > 415-927-2543 > > > > /* > ************************************************************************** > * Copyright (c) 2002 Tiburon Enterprise Systems, Inc. All rights > reserved. > * com.tes.framework.ejbs.homeservice.EjbHomeLocator.java > ************************************************************************** > */ > > package com.tes.framework.ejbs.homeservice; > > import java.util.Properties; > import javax.ejb.EJBHome; > import javax.naming.Context; > import javax.naming.InitialContext; > > // this class is located in the Concurrent.jar file which comes with > JBoss ! > import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; > > /** > * This class keeps a cache of EJBHomes. If a EJBHome object becomes > stale, > * we ignore issue of trying to get a new instance from same Context. We > just > * get a new Context and try again. This is because whether or not there > are > * multiple instances of the same EJBHome into context is a vendor > implementation > * specific issue. > * > * This class ignores the issue of whether or not it is possible to have > different > * EJBHomes on different machines but cached in Context with same key. > Probably should > * revisit this !! > * > * This class assumes we are not using a clustered Jndi. Need to revisit > this issue !! > */ > > public class EjbHomeLocator { > > private static EjbHomeLocator INSTANCE = new EjbHomeLocator (); > > // We use this implementation of a HashMap because it neatly > // handles all issues of concurrent puts and gets to the HashMap > private ConcurrentReaderHashMap homes; > > public static EJBHome getEjbHome (String key, boolean refreshCache, > Properties props) throws Exception { > return INSTANCE.nonStaticGetEjbHome (key, refreshCache, props); > } > > private EjbHomeLocator () { > homes = new ConcurrentReaderHashMap(); > } > > private EJBHome nonStaticGetEjbHome (String key, boolean refreshCache, > Properties props) throws Exception { > EJBHome home = null; > if (refreshCache) > refreshCache (key, props); > home = (EJBHome)homes.get (key); > // If we got a null home and we did not call for a refresh of cache > // then do the refresh and try again. So, first time calling this > will > // force getting the home from Context. But if we already called for > // a refresh, we don't do it again. > if (home == null && !refreshCache) { > refreshCache (key, props); > home = (EJBHome)homes.get (key); > } > return home; > } > > private void refreshCache (String key, Properties props) throws > Exception { > Context ctx = new InitialContext (props); > EJBHome home = (EJBHome)ctx.lookup (key); > homes.put (key, home); > } > > } > > /* > ************************************************************************** > * Copyright (c) 2002 Tiburon Enterprise Systems, Inc. All rights > reserved. > * com.tes.image.ejbs.homeservice.ImageHomeService.java > ************************************************************************** > */ > > package foo; > > import java.util.Properties; > > import com.tes.framework.ejbs.homeservice.EjbHomeLocator; > import com.tes.framework.ejbs.homeservice.HomeServiceException; > > import com.tes.ejbs.interfaces.Foo; > import com.tes.ejbs.interfaces.FooHome; > > > > /** > * We claim this class can be generated by a xDoclet task which reads the > * JavaDoc comments in the FooBean.java file. So, the comments needs to > * include the following information: > * > * Amount of Sleep Interval > * Amount of Max Wait Interval > * Properties of Context/Machine where this ejb will live. > * What methods of FooHome we want to expose in this service. > */ > > public class FooHomeService { > > private static long sleepInterval = 2000; // 2 seconds > private static long maxWaitInterval = 10000; // 10 seconds > > /** > * This method gets EJBHome from EjbHomeLocator, then calls appropriate > method on home. > * If there is any exception thrown, then go to sleep for a while > (sleepInterval), then > * wake up and try again. We don't keep trying forever and after a > certain point (maxWaitInterval) > * we quit and throw a HomeServiceException. > */ > public static Foo create (int id, String name) throws > HomeServiceException { > Foo foo = null; > boolean firstime = true; > long amtTimeWaited = 0; > Exception ex = null; > long startTime = System.currentTimeMillis(); > while (foo == null && amtTimeWaited < maxWaitInterval) > try { > FooHome fooHome = (FooHome)EjbHomeLocator.getEjbHome > (FooHome.JNDI_NAME, !firstime, getProperties()); > foo = fooHome.create (id, name); > } > catch (Exception e) { > ex = e; > foo = null; > firstime = false; > try { Thread.sleep (sleepInterval); } catch (Exception ee) {}; > amtTimeWaited = System.currentTimeMillis() - startTime; > } > if (foo == null) > throw new HomeServiceException (ex); > return foo; > } > > public static Foo findByPrimaryKey (Integer pk) throws > HomeServiceException { > Foo foo = null; > boolean firstime = true; > long amtTimeWaited = 0; > Exception ex = null; > long startTime = System.currentTimeMillis(); > while (foo == null && amtTimeWaited < maxWaitInterval) > try { > FooHome fooHome = (FooHome)EjbHomeLocator.getEjbHome > (FooHome.JNDI_NAME, !firstime, getProperties()); > foo = fooHome.findByPrimaryKey (pk); > } > catch (Exception e) { > ex = e; > foo = null; > firstime = false; > try { Thread.sleep (sleepInterval); } catch (Exception ee) {}; > amtTimeWaited = System.currentTimeMillis() - startTime; > } > if (foo == null) > throw new HomeServiceException (ex); > return foo; > } > > public static void remove (Object key) throws HomeServiceException { > boolean firstime = true; > long amtTimeWaited = 0; > Exception ex = null; > long startTime = System.currentTimeMillis(); > while (amtTimeWaited < maxWaitInterval) > try { > FooHome fooHome = (FooHome)EjbHomeLocator.getEjbHome > (FooHome.JNDI_NAME, !firstime, getProperties()); > fooHome.remove (key); > return; > } > catch (Exception e) { > ex = e; > firstime = false; > try { Thread.sleep (sleepInterval); } catch (Exception ee) {}; > amtTimeWaited = System.currentTimeMillis() - startTime; > } > throw new HomeServiceException (ex); > } > > private static Properties getProperties () { > Properties props = new Properties(); > props.put >("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); > props.put ("java.naming.provider.url","jnp://localhost:1099"); > props.put ("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); > return props; > } > > > } > > /* > ************************************************************************** > * Copyright (c) 2002 Tiburon Enterprise Systems, Inc. All rights > reserved. > * com.tes.framework.ejbs.homeservice.HomeServiceException.java > ************************************************************************** > */ > > package com.tes.framework.ejbs.homeservice; > > import java.io.PrintStream; > import java.io.PrintWriter; > > public class HomeServiceException extends Exception { > > private Exception nestedException; > > public HomeServiceException (String s) { > super(s); > nestedException = null; > } > > public HomeServiceException (Exception nestedException) { > super(); > this.nestedException = nestedException; > } > > public HomeServiceException (String s, Exception nestedException) { > super(s); > this.nestedException = nestedException; > } > > public void printStackTrace() { > super.printStackTrace(); > if (nestedException != null) > nestedException.printStackTrace(); > } > > public void printStackTrace(PrintStream ps) { > super.printStackTrace(ps); > if (nestedException != null) > nestedException.printStackTrace(ps); > } > > public void printStackTrace(PrintWriter pw) { > super.printStackTrace(pw); > if (nestedException != null) > nestedException.printStackTrace(pw); > } > > public Exception getNestedException() { > return nestedException; > } > > } > ------------------------------------------------------- This sf.net email is sponsored by: Influence the future of Java(TM) technology. Join the Java Community Process(SM) (JCP(SM)) program now. http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en _______________________________________________ JBoss-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-user
