13 Replies Latest reply on Jun 26, 2006 11:24 AM by Peter Johnson

    Problem locating an MBeanServer

    David Zarfl Newbie

      Hi,
      I have trouble with the following line of code:

      MBeanServer server = MBeanServerLocator.locate();

      when executing the method it is in I get the following error:
      java.util.NoSuchElementException
      at java.util.AbstractList$Itr.next(AbstractList.java:427)
      at org.jboss.mx.util.MBeanServerLocator.locate(MBeanServerLocator.java:79)
      at org.jboss.mx.util.MBeanServerLocator.locate(MBeanServerLocator.java:92)
      at trail.SE.client.Client.main(Client.java:30)


      The result of the code would be used for getting a stub to a ServiceBean

      any help would be grealty appreciated,
      cheers
      deus machinarum

        • 1. Re: Problem locating an MBeanServer
          Peter Johnson Master

          Are you trying to get the mbean server for JBoss? If so, I usually use the following code:

          Hashtable env = new Hashtable();
          String factory = "org.jnp.interfaces.NamingContextFactory";
          env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
          String url1 = "jnp://localhost:1099";
          env.put(Context.PROVIDER_URL, url1);
          Context ctx = new InitialContext(env);
          MBeanServerConnection mconn = (MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");


          • 2. Re: Problem locating an MBeanServer
            Dimitris Andreadis Master

            It seems he's trying to locate the local mbeanserver, which doesn't exist.

            • 3. Re: Problem locating an MBeanServer
              David Zarfl Newbie

              @PeterJ:
              Executing your code leads to the following:

              javax.naming.CommunicationException [Root exception is java.lang.ClassNotFoundException: org.jboss.jmx.adaptor.rmi.RMIAdaptor (no security manager: RMI class loader disabled)]
              at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)
              at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
              at javax.naming.InitialContext.lookup(InitialContext.java:351)
              at trail.SE.client.Client.main(Client.java:37)
              Caused by: java.lang.ClassNotFoundException: org.jboss.jmx.adaptor.rmi.RMIAdaptor (no security manager: RMI class loader disabled)
              at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:531)
              at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
              at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
              at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
              at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1494)
              at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1457)
              at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1693)
              at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
              at java.rmi.MarshalledObject.get(MarshalledObject.java:135)
              at org.jnp.interfaces.MarshalledValuePair.get(MarshalledValuePair.java:72)
              at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:652)
              ... 3 more


              any ideas on how i could activate the security manager?

              It seems he's trying to locate the local mbeanserver, which doesn't exist.

              To be perfectly honest I'm not sure what exactly I should be trying to connect to. At the end of the day I want my client to be able to connect to a server running on a different machine, currently my JBoss Server runs on the same machine but in a different vm. The weird thing is SessionBeans in the client work immediately (looking them up via the InitialContext)

              Is there a difference in the client code between connecting on a local machine and to a remote machine?

              thx for your help


              • 5. Re: Problem locating an MBeanServer
                Peter Johnson Master

                The "no security manager" error sure looked familiar, but it took me some time to find it. I ran into the same error when I wrote mbean client code that I ran on my Linux box attempting to connect to JBoss running on my Windows box. Here is what I did. I added the following lines before the code I posted:

                if (System.getSecurityManager() == null) {
                 System.setSecurityManager(new RMISecurityManager());
                }


                In addition, I changed the JAVA_HOME/jre/lib/security/java.policy file to read:

                grant {
                 permission java.security.AllPermission;
                };


                As you probably notice, this is not the correct way to set this, unless you don't care about security (it was fine for me, I only needed to demo a prototype after which I restored the original java.policy file). If you google RMISecurityManager you will get many hits that will tell you how to configure the security properly.

                • 6. Re: Problem locating an MBeanServer
                  David Zarfl Newbie

                  Here's my current code :

                  try{
                  
                   if (System.getSecurityManager() == null) {
                   System.setSecurityManager(new RMISecurityManager());
                   }
                   Hashtable env = new Hashtable();
                   String factory = "org.jnp.interfaces.NamingContextFactory";
                   env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                   String url1 = "jnp://localhost:1099";
                   env.put(Context.PROVIDER_URL, url1);
                   Context ctx = new InitialContext(env);
                   MBeanServerConnection mconn = (MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
                   }
                   catch(NamingException e){e.getMessage(); e.printStackTrace();}


                  with the normal/default java.policy file i get the following error:
                  javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: java.security.AccessControlException: access denied (java.net.SocketPermission 230.0.0.4 connect,accept,resolve) [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)]]
                  at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1414)
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:594)
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
                  at javax.naming.InitialContext.lookup(InitialContext.java:351)
                  at trail.SE.client.Client.main(Client.java:42)
                  Caused by: javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)]
                  at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:269)
                  at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1385)
                  ... 4 more
                  Caused by: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
                  at java.security.AccessControlContext.checkPermission(AccessControlContext.java:264)
                  at java.security.AccessController.checkPermission(AccessController.java:427)
                  at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
                  at java.lang.SecurityManager.checkConnect(SecurityManager.java:1034)
                  at java.net.Socket.connect(Socket.java:501)
                  at java.net.Socket.connect(Socket.java:457)
                  at java.net.Socket.<init>(Socket.java:365)
                  at java.net.Socket.<init>(Socket.java:265)
                  at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:84)
                  at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:77)
                  at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:239)
                  ... 5 more


                  This seems to me to be a Problem in configuring the JBoss Server, as it seems to get the request and just deny access.
                  Any ideas on how to configure that?

                  switching to
                  grant {
                   permission java.security.AllPermission;
                  };


                  I get :
                  javax.naming.CommunicationException [Root exception is java.lang.ClassNotFoundException: org.jboss.jmx.adaptor.rmi.RMIAdaptor]
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
                  at javax.naming.InitialContext.lookup(InitialContext.java:351)
                  at trail.SE.client.Client.main(Client.java:42)
                  Caused by: java.lang.ClassNotFoundException: org.jboss.jmx.adaptor.rmi.RMIAdaptor
                  at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                  at java.security.AccessController.doPrivileged(Native Method)
                  at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                  at java.lang.Class.forName0(Native Method)
                  at java.lang.Class.forName(Class.java:242)
                  at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:707)
                  at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:651)
                  at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:588)
                  at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
                  at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
                  at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
                  at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1494)
                  at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1457)
                  at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1693)
                  at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299)
                  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
                  at java.rmi.MarshalledObject.get(MarshalledObject.java:135)
                  at org.jnp.interfaces.MarshalledValuePair.get(MarshalledValuePair.java:72)
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:652)
                  ... 3 more


                  In both cases I am using the
                  -Djava.security.policy=C:\Sun\AppServer\jdk\jre\lib\security\java.policy


                  switch on the java command.

                  thx for your help

                  • 7. Re: Problem locating an MBeanServer
                    Peter Johnson Master

                    The org.jboss.jmx.adaptor.rmi.RMIAdaptor class is in these jar files, all found in jboss_home/client:

                    * jbossall-client.jar
                    * jbossjmx-ant.jar
                    * jmx-invoker-adaptor-client.jar

                    Include one of these in your classpath (I use jbossall-client).

                    • 8. Re: Problem locating an MBeanServer
                      David Zarfl Newbie

                      Thank you so much for pointing that out.
                      One more problem remains:
                      How do i get my stub?
                      Currently I'm using the following code: (World being the ManagementInterface of the Bean I want to use)

                      if (System.getSecurityManager() == null) {
                       System.setSecurityManager(new RMISecurityManager());
                       }
                       Hashtable env = new Hashtable();
                       String factory = "org.jnp.interfaces.NamingContextFactory";
                       env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                       String url1 = "jnp://localhost:1099";
                       env.put(Context.PROVIDER_URL, url1);
                       Context ctx = new InitialContext(env);
                       MBeanServerConnection mconn = (MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
                      
                      
                       world = (World) MBeanProxyExt.create(
                       World.class,
                       "trail:service=world");


                      which does not compile do to a MalformedObjectNameException.
                      I would actually like to use the create methode that takes a third argument(an MBeanServer). Do you happen to know how I can get the MBeanServer from the MBeanServerConnection(which is actually a sub interface of the latter)

                      How do you get acces to your Beans?

                      Again, thank you so much.

                      • 9. Re: Problem locating an MBeanServer
                        Peter Johnson Master

                        See http://www.jboss.com/index.html?module=bb&op=viewtopic&t=84303 for a complete program. The mbean name I used was based on my data source, but you could subsistutue DefaultDS for ProductDS and it should work. Or pick a name from the jmx-console.

                        • 10. Re: Problem locating an MBeanServer
                          David Zarfl Newbie

                          Hi,
                          executing

                          if (System.getSecurityManager() == null) {
                           System.setSecurityManager(new RMISecurityManager());
                           }
                           Hashtable env = new Hashtable();
                           String factory = "org.jnp.interfaces.NamingContextFactory";
                           env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                           String url1 = "jnp://localhost:1099";
                           env.put(Context.PROVIDER_URL, url1);
                           Context ctx = new InitialContext(env);
                           MBeanServerConnection mconn = (MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
                           ObjectName name = new ObjectName("jboss.jca:name=ds/DefaultDS,service=ManagedConnectionPool");
                           Object val = mconn.getAttribute(name, "InUseConnectionCount");
                           System.out.println(name + "\n\tInUseConnectionCount=" + val);


                          leads to:
                          javax.management.InstanceNotFoundException: jboss.jca:name=ds/DefaultDS,service=ManagedConnectionPool is not registered.
                          at org.jboss.mx.server.registry.BasicMBeanRegistry.get(BasicMBeanRegistry.java:523)
                          at org.jboss.mx.server.MBeanServerImpl.getAttribute(MBeanServerImpl.java:550)
                          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                          at java.lang.reflect.Method.invoke(Method.java:585)
                          at org.jboss.jmx.connector.invoker.InvokerAdaptorService.invoke(InvokerAdaptorService.java:266)
                          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                          at java.lang.reflect.Method.invoke(Method.java:585)
                          at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
                          at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
                          at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
                          at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
                          at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
                          at org.jboss.jmx.connector.invoker.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:108)
                          at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
                          at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
                          at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
                          at org.jboss.invocation.jrmp.server.JRMPProxyFactory.invoke(JRMPProxyFactory.java:179)
                          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                          at java.lang.reflect.Method.invoke(Method.java:585)
                          at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
                          at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
                          at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
                          at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
                          at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
                          at org.jboss.invocation.jrmp.server.JRMPInvoker$MBeanServerAction.invoke(JRMPInvoker.java:819)
                          at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:420)
                          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                          at java.lang.reflect.Method.invoke(Method.java:585)
                          at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
                          at sun.rmi.transport.Transport$1.run(Transport.java:153)
                          at java.security.AccessController.doPrivileged(Native Method)
                          at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
                          at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
                          at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
                          at java.lang.Thread.run(Thread.java:595)


                          I'm not sure I made myself quite clear on what i want to do. I want to obtain a reference to a Bean of my own, deployed in JBoss named WorldMBean which extends the interface World.

                          What exactly are the lines

                          ObjectName name = new ObjectName("jboss.jca:name=ds/DefaultDS,service=ManagedConnectionPool");
                           Object val = mconn.getAttribute(name, "InUseConnectionCount");
                           System.out.println(name + "\n\tInUseConnectionCount=" + val);

                          supposed to do?

                          I was originally trying to adapt the code found in the trail at:
                          http://trailblazer.demo.jboss.com/EJB3Trail/serviceobjects/jmx/code/calculator.jsp.html

                          only I'm using a java client not a jsp. I'm not sure if the code in the jsp is only supposed to work locally because of:
                          MBeanServer server = MBeanServerLocator.locate();


                          If so is there a simple way to get a reference to my MBeanServer on the same machine but not in the same VM?

                          Thanks for your help

                          • 11. Re: Problem locating an MBeanServer
                            Peter Johnson Master

                            Oops, my instructions were not completely correct. The correct data source name is DefaultDS, not ds/DefaultDS.

                            The easiest way to find out the name of the mbean you are looking for it to use the jmx-console: http://localhost:8080/jmx-console. Then you simple replace the object name text with the mbean name you want.

                            As for the three lines of code you asked about:

                            ObjectName name .... converts the string representation of the mbean name into an object. An mbean name consists of a namespace (jboss.jca), followed by a colon, and then followed by a comma-separated lists of name/value pairs that can appear in any order. What the name/value pairs are and mean depends on the writer of the mbean.

                            Object val .... get the value of an attribute (InUseConnectionCount) from the mbean.

                            System.out. .... of course prints out the mbean name and the value of the attribute.

                            The code I provided works in a stand-alone client and can access a remote JBoss server instance, just change "localhost" in the url to the desired host name.

                            • 12. Re: Problem locating an MBeanServer
                              David Zarfl Newbie

                              ok, I get that now

                              How would I be able to do something like:
                              Beanname.methodxyz();

                              e.g.:
                              world.tell();

                              i.e. How can I get a reference?

                              Sorry if that is already explained and I'm just missing it.

                              • 13. Re: Problem locating an MBeanServer
                                Peter Johnson Master

                                You cannot get a reference. Instead, all operations on an mbean must be done through the MBeanServerConnection instance. Thus, to make the example method call, you would write:

                                ObjectName name = new ObjectName("--name of your 'world' mbean--");
                                mconn.invoke(name, "tell", null, null);


                                See http://java.sun.com/j2ee/1.4/docs/api/javax/management/MBeanServerConnection.html for the methods on MBeanServerConnection.