3 Replies Latest reply on Aug 19, 2005 7:14 PM by spambob

    ClassCastException when looking up HAPartition via JNDI

    aggiaggi

      Hello,

      I need help on the following problem:

      I get a ClassCastException when I try to lookup the DefaultPartion via JNDI from within client code. I run jboss 3.2.2 in the shipped "all" configuration and have only jbossall-client.jar in the client's CLASSPATH. Here is my code:

      import java.util.Properties;
      
      import javax.naming.Context;
      import javax.naming.InitialContext;
      
      import org.jboss.ha.framework.interfaces.HAPartition;
      
      public class TestJBoss {
      
       public static void main(String[] args) {
       Properties p = new Properties();
       p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
       p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
       p.put(Context.PROVIDER_URL, "169.254.195.178:1100"); // HA-JNDI port.
      
       try{
       InitialContext ctx = new InitialContext(p);
       Object obj = (Object) ctx.lookup("/HAPartition/DefaultPartition");
       System.out.println(obj);
       HAPartition myPartition = (HAPartition) obj;
       System.out.println(myPartition.getPartitionName());
       }catch(Exception e){
       e.printStackTrace();
       }
       }
      }


      The System.out.println(obj) produces the following output:

      Reference Class Name: org.jboss.ha.framework.server.HAPartitionImpl
      Type: nns
      Content: /HAPartition/DefaultPartition


      Cheers
      Gerd

        • 1. Re: ClassCastException when looking up HAPartition via JNDI
          starksm64

          This code cannot work outside of the jboss server. The HAPartition implementation is no serializable and able to function outside of the vm in which the partition service was configured. You would have to write your own rpc proxy using the detached invoker framework or interact with the service mbean via a jmx connector.

          • 2. Re: ClassCastException when looking up HAPartition via JNDI
            aggiaggi

            Thx for the quick answer.

            Best regards
            Gerd

            • 3. Re: ClassCastException when looking up HAPartition via JNDI
              spambob

              I'm seeing the same problem, when upgrading from 3.2.5 to 4.0.2.
              My code is:

               Properties env = new Properties();
              env.setProperty(NamingContext.JNP_PARTITION_NAME, "DefaultPartition");
              env.setProperty(NamingContext.JNP_DISCOVERY_PORT, "1102");
              env.setProperty(NamingContext.JNP_DISCOVERY_GROUP, "230.0.0.4");
              env.setProperty(NamingContext.JNP_DISCOVERY_TIMEOUT, "5000");
              env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
              InitialContext ctx = new InitialContext(env);
              String partitionName = env.getProperty(NamingContext.JNP_PARTITION_NAME);
              String partitionJndiName = "/HAPartition/" + partitionName;
              Object lookup = ctx.lookup(partitionJndiName);
              


              In jboss 4.0.2, this code gives a ClassCastException:

              HAPartition partition = (HAPartition) ctx.lookup(partitionJndiName);
              


              But this code works:

              Method method1 = lookup.getClass().getMethod("getCurrentView",null);
              Vector view = (Vector) method1.invoke(lookup,null);
              
              Method method2 = lookup.getClass().getMethod("getNodeName",null);
              String myNodeName = (String) method2.invoke(lookup,null);
              


              In jboss 3.2.5, the cast would work and then I could just invoke partition.getCurrentView() and partition.getNodeName()