Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Spring EJB's do not failover automatically

  1. #1

    Default Spring EJB's do not failover automatically

    Hi all,
    My program uses Spring to connect to EJB's hosted on JBoss. The EJB's on JBoss are clustered. In the Spring's configuration file, I have provided the list of JBoss IP addresses and HA-JNDI Port numbers, for the Spring beans to access. When the program starts, it connects to which ever JBoss it finds first that is up and is available. If the Jboss is brought down, the bean does not automatically failover to another JBoss node even though the IP of the JBoss is provided in the list of url's. Below is the definition of one of the beans ..
    <bean id="oneBean" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="sample/oneBean/remote"/>
    <property name="expectedType" value="sample.OneBeanIntf"/>
    <property name="jndiEnvironment">
    <props>
    <prop key="java.naming.factory.initial">
    org.jnp.interfaces.NamingContextFactory</prop>
    <prop key="java.naming.factory.url.pkgs">
    org.jboss.naming:org.jnp.interfaces</prop>
    <prop key="java.naming.provider.url">
    jnp://3.63.62.90:1200,jnp://3.63.2.22:1200</prop>
    </props>
    </property>
    </bean>

    Has anybody faced this problem before ?

  2. #2
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    What is the type of the object? Looking at the source code, your defintion should return what a normal JNDI lookup would return, which in this case would probably be a remote reference to your EJB that you can make invocations on.

    What thing that JndiObjectFactoryBean does different than a straight JNDI lookup: it caches the object. So multiple calls to your ApplicationContext will always return the same JNDI object.

    Can you try a test program doing the lookup by hand and see if that can do the failover properly? I'd be surprised if the problem is the JndiObjectFactoryBean, but this test could help eliminate that possibility.
    Bill

  3. #3

    Default

    Hi ,
    Thanks for your reply, I have written a sample program, for testing if it is a problem with Spring configuration or with JBoss configuration.
    public class FailoverTest {
    public static void main(String args[]){
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "org.jnp.interfaces.NamingContextFactory");
    env.put(Context.PROVIDER_URL, "jnp://3.63.62.9:1200,jnp://3.63.20.22:1200");
    try{
    Context initialContext = new InitialContext(env);
    while(true)
    System.out.println(initialContext.lookup("sample/beanOne"));
    }
    catch(Exception ex)
    {
    System.out.println(ex.getMessage());
    }
    }

    This peice of code does a perfect failover. What can be the possible problem with the Spring configuration or with JndiObjectFactoryBean?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    When posting code please use [ code][/code ] tags. Also understand what is happening and realize that your testcase isn't the same as what spring does.

    You iterate numerous times over a lookup, ie retrieve a new bean each time. Spring only retrieves ones and caches the result. So it does only 1 lookup and then caches the object. You can disable this by setting cache to false, then it will do a lookup before calling a method. Mind you this affects performance!

    If you use EJBs you also might want to check to EJB classes spring provides which provide more flexibility/configurability for EJB stuff. You can for instance configure if it should retrieve a new instance of an exception occurs.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default Spring and EJB's do not failover

    Thanks for the reply ! Is there any link where I can find examples for these configurations ?? I tried setting
    Code:
    <bean id="oneBean" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="sample/oneBean/remote"/>
    <property name="expectedType" value="sample.OneBeanIntf"/>
    <property name="cache" value="false">
    <property name="jndiEnvironment">
    <props>
    <prop key="java.naming.factory.initial">
    org.jnp.interfaces.NamingContextFactory</prop>
    <prop key="java.naming.factory.url.pkgs">
    org.jboss.namingrg.jnp.interfaces</prop>
    <prop key="java.naming.provider.url">
    jnp://3.63.62.90:1200,jnp://3.63.2.22:1200</prop>
    </props>
    </property>
    </bean>
    cache to false but still it does not work . Some other way so that performance is not impacted ?

    Thanks in advance
    Last edited by sonali.majumdar; May 26th, 2008 at 02:45 PM.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    The reference guide is quite clear on how to use EJB's with Spring. Check the relevant chapter. Chapter 18 is the one to read, that contains a lot of samples.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default Spring EJB's do not failover automatically

    Hi! Thanks again ! I tried out the following code , a very crude way of doing it . Is there a better solution ?
    Code:
    public class FailoverTest {
    public static void main(String args[]){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("contextFailover1.xml");
    				try{
    while(true){
    System.out.println("first "+ctx.getBean("sample/oneBean"));
    }
    }catch(Exception ex){
    System.out.println(ex.getMessage());
    ApplicationContext ctx2 =  new ClassPathXmlApplicationContext("contextFailover2.xml");
    System.out.println("Failover "+ ctx2.getBean("sample/oneBean"));
    }
    }
    }
    Thanks !

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Not sure what you are trying to test here... So it is hard to tell. If you use ejb's and use the EJB ProxyFactoryBeans from spring then spring will handle all the failover/reconnection stuff for you. You have to use the EJB ProxyFactoryBeans for that and not the simpler JndiObjectFactoryBean.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default Spring EJB's do not failover automatically

    Yeah, Thanks again ! I am trying failover logic in Spring .but I am in Spring 2.0.6 and using EJB3 . I read in the post
    that SimpleRemoteStatelessSessionProxyFactoryBean support for EJB3 is included in Spring 2.5.1 . Any suggestions ?

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Upgrade .

    There isn't much you can do with the JndiObjectFactoryBean in the regard of failover. The only thing you can do is disable the lookupOnStartup and the caching so that a new instance will always be retrieved. But that isn't failover.

    If you cannot upgrade for some reason, you might want to extend the JndiObjectFactoryBean yourself and create one that has some failover capability. You might want to take a look at the EJB stuff (from 2.5) on how it is done.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •