JBoss Community

Re: AS 7 demos

created by Kabir Khan in JBoss AS7 Development - View the full discussion

I think the previous post can be ignored, I messed up my branches somehow… 
Trying out a JMS example I didn't see the ConnectionFactory and queue from standalone.xml getting bound in JNDI. JMSServerManagerImpl.bindToJndi() ignores the requests to bind to JNDI because no context has been set yet. If I modify this it gets bound in JNDI
+++ b/messaging/src/main/java/org/jboss/as/messaging/jms/JMSService.java
@@ -23,6 +23,8 @@
package org.jboss.as.messaging.jms;
import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.jms.server.JMSServerManager;
@@ -31,10 +33,10 @@ import org.jboss.as.messaging.MessagingSubsystemElement;
import org.jboss.logging.Logger;
import org.jboss.msc.service.BatchBuilder;
import org.jboss.msc.service.Service;
-import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
+import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.value.InjectedValue;
/**
@@ -49,10 +51,15 @@ public class JMSService implements Service<JMSServerManager> {
     private JMSServerManager jmsServer;
     public static void addService(final BatchBuilder builder) {
-        final JMSService service = new JMSService();
-        builder.addService(JMSSubsystemElement.JMS_MANAGER, service)
-            .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())
-            .setInitialMode(Mode.ACTIVE);
+        try {
+            final JMSService service = new JMSService();
+            builder.addService(JMSSubsystemElement.JMS_MANAGER, service)
+                .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())
+                .addInjection(service.getContextInjector(), (Context)new InitialContext())
+                .setInitialMode(Mode.ACTIVE);
+        } catch (NamingException e) {
+            throw new RuntimeException(e);
+        }
     }
     protected JMSService() {
@@ -65,7 +72,7 @@ public class JMSService implements Service<JMSServerManager> {
             final JMSServerManager jmsServer = new JMSServerManagerImpl(hornetQServer.getValue());
             final Context jndiContext = contextInjector.getOptionalValue();
-            if(context != null) {
+            if(jndiContext != null) {
                 jmsServer.setContext(jndiContext);
             }
~
Next when I try to look up 'ConnectionFactory' from my example I get the following error:
00:12:02,953 ERROR [deployment] Caught exception handling update (param is 643db326-7dd6-4da3-97ca-f1ea0d2de596): org.jboss.msc.service.StartException in service service jboss.mbean.service.jboss:name=test,type=jms.start: Failed to execute legacy service start
at org.jboss.as.service.StartStopService.start(StartStopService.java:62)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:923)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_20]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_20]
at java.lang.Thread.run(Thread.java:637) [:1.6.0_20]
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_20]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_20]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_20]
at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_20]
at org.jboss.as.service.StartStopService.start(StartStopService.java:59)
... 4 more
Caused by: java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.QueueConnectionFactory
at org.jboss.as.demos.jms.mbean.Test.start(Test.java:58)
... 9 more
It appears to happen because in this path ObjectFactoryBuilder.factoryFromReference()'s contextCl is null, resulting in an exception, which returns 'this' i.e. the original Reference and not the ObjectFactory. Since the reference is a not a ModularReference we are trying the TCL.
ObjectFactoryBuilder.factoryFromReference(Reference, Hashtable<?,?>) line: 104
ObjectFactoryBuilder.createObjectFactory(Object, Hashtable<?,?>) line: 58
NamingManager.getObjectInstance(Object, Name, Context, Hashtable<?,?>) line: 282
NamingContext.getObjectInstance(Object, Name, Hashtable) line: 430
NamingContext.lookup(Name) line: 163
NamingContext.lookup(String) line: 173
InitialContext.lookup(String) line: 392
Test.start() line: 58
The object that is originally passed in to NamingContext.rebind() is HornetQConectionFactory which is an instance of Referenceable and whose getReference() method returns a plain Reference:
   public Reference getReference() throws NamingException
   {
      return new Reference(this.getClass().getCanonicalName(),
                           new SerializableObjectRefAddr("HornetQ-CF", this),
                           ConnectionFactoryObjectFactory.class.getCanonicalName(),
                           null);
   }
this then is what bound in JNDI, I don't think there is a way to override this to create a ModularReference. I can see queue/test and topic/test getting bound the same way so there will probably be a similar exception looking this up. Putting in some debug statements
        InitialContext ctx = new InitialContext();
        System.out.println(ctx.lookup("ConnectionFactory"));
        System.out.println(ctx.lookup("queue/test"));
Shows this:
09:27:22,965 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQConnectionFactory
09:27:22,966 INFO  [stdout] Type: HornetQ-CF
09:27:22,966 INFO  [stdout] Content: [B@f4b2263
09:27:22,967 INFO  [stdout]
09:27:22,968 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQQueue
09:27:22,968 INFO  [stdout] Type: HornetQ-DEST
09:27:22,968 INFO  [stdout] Content: [B@60da5686
09:27:22,969 INFO  [stdout]

I think the previous post can be ignored, I messed up my branches somehow… 

 

Trying out a JMS example I didn't see the ConnectionFactory and queue from standalone.xml getting bound in JNDI. JMSServerManagerImpl.bindToJndi() ignores the requests to bind to JNDI because no context has been set yet. If I modify this it gets bound in JNDI

+++ b/messaging/src/main/java/org/jboss/as/messaging/jms/JMSService.java

@@ -23,6 +23,8 @@

package org.jboss.as.messaging.jms;

 

import javax.naming.Context;

+import javax.naming.InitialContext;

+import javax.naming.NamingException;

 

import org.hornetq.core.server.HornetQServer;

import org.hornetq.jms.server.JMSServerManager;

@@ -31,10 +33,10 @@ import org.jboss.as.messaging.MessagingSubsystemElement;

import org.jboss.logging.Logger;

import org.jboss.msc.service.BatchBuilder;

import org.jboss.msc.service.Service;

-import org.jboss.msc.service.ServiceController.Mode;

import org.jboss.msc.service.StartContext;

import org.jboss.msc.service.StartException;

import org.jboss.msc.service.StopContext;

+import org.jboss.msc.service.ServiceController.Mode;

import org.jboss.msc.value.InjectedValue;

 

/**

@@ -49,10 +51,15 @@ public class JMSService implements Service<JMSServerManager> {

     private JMSServerManager jmsServer;

 

     public static void addService(final BatchBuilder builder) {

-        final JMSService service = new JMSService();

-        builder.addService(JMSSubsystemElement.JMS_MANAGER, service)

-            .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())

-            .setInitialMode(Mode.ACTIVE);

+        try {

+            final JMSService service = new JMSService();

+            builder.addService(JMSSubsystemElement.JMS_MANAGER, service)

+                .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())

+                .addInjection(service.getContextInjector(), (Context)new InitialContext())

+                .setInitialMode(Mode.ACTIVE);

+        } catch (NamingException e) {

+            throw new RuntimeException(e);

+        }

     }

 

     protected JMSService() {

@@ -65,7 +72,7 @@ public class JMSService implements Service<JMSServerManager> {

             final JMSServerManager jmsServer = new JMSServerManagerImpl(hornetQServer.getValue());

 

             final Context jndiContext = contextInjector.getOptionalValue();

-            if(context != null) {

+            if(jndiContext != null) {

                 jmsServer.setContext(jndiContext);

             }

 

~

 

Next when I try to look up 'ConnectionFactory' from my example I get the following error:

 

00:12:02,953 ERROR [deployment] Caught exception handling update (param is 643db326-7dd6-4da3-97ca-f1ea0d2de596): org.jboss.msc.service.StartException in service service jboss.mbean.service.jboss:name=test,type=jms.start: Failed to execute legacy service start

at org.jboss.as.service.StartStopService.start(StartStopService.java:62)

at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:923)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_20]

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_20]

at java.lang.Thread.run(Thread.java:637) [:1.6.0_20]

Caused by: java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_20]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_20]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_20]

at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_20]

at org.jboss.as.service.StartStopService.start(StartStopService.java:59)

... 4 more

Caused by: java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.QueueConnectionFactory

at org.jboss.as.demos.jms.mbean.Test.start(Test.java:58)

... 9 more

 

It appears to happen because in this path ObjectFactoryBuilder.factoryFromReference()'s contextCl is null, resulting in an exception, which returns 'this' i.e. the original Reference and not the ObjectFactory. Since the reference is a not a ModularReference we are trying the TCL.

 

ObjectFactoryBuilder.factoryFromReference(Reference, Hashtable<?,?>) line: 104

ObjectFactoryBuilder.createObjectFactory(Object, Hashtable<?,?>) line: 58

NamingManager.getObjectInstance(Object, Name, Context, Hashtable<?,?>) line: 282

NamingContext.getObjectInstance(Object, Name, Hashtable) line: 430

NamingContext.lookup(Name) line: 163

NamingContext.lookup(String) line: 173

InitialContext.lookup(String) line: 392

Test.start() line: 58

 

The object that is originally passed in to NamingContext.rebind() is HornetQConectionFactory which is an instance of Referenceable and whose getReference() method returns a plain Reference:

 

   public Reference getReference() throws NamingException

   {

      return new Reference(this.getClass().getCanonicalName(),

                           new SerializableObjectRefAddr("HornetQ-CF", this),

                           ConnectionFactoryObjectFactory.class.getCanonicalName(),

                           null);

   }

this then is what bound in JNDI, I don't think there is a way to override this to create a ModularReference. I can see queue/test and topic/test getting bound the same way so there will probably be a similar exception looking this up. Putting in some debug statements

 

        InitialContext ctx = new InitialContext();

 

        System.out.println(ctx.lookup("ConnectionFactory"));

        System.out.println(ctx.lookup("queue/test"));

 

Shows this:

09:27:22,965 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQConnectionFactory

09:27:22,966 INFO  [stdout] Type: HornetQ-CF

09:27:22,966 INFO  [stdout] Content: [B@f4b2263

09:27:22,967 INFO  [stdout]

09:27:22,968 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQQueue

09:27:22,968 INFO  [stdout] Type: HornetQ-DEST

09:27:22,968 INFO  [stdout] Content: [B@60da5686

09:27:22,969 INFO  [stdout]

 

 

Reply to this message by going to Community

Start a new discussion in JBoss AS7 Development at Community