10 Replies Latest reply on Aug 4, 2008 11:51 AM by Peter Johnson

    JBoss API usage examples

    Danish Khan Newbie

      Hi guys,

      I am working on an application which will show different performance parameters and usage patterns of JBoss. In this regard, I need to see some code examples that show how to use JBoss API. For example , if I need to know connection pool size through JBoss API, which function will I call, what is the overall setup of such application, how does it talk to the JBoss

      Thanks in advance!

      -Danish

        • 1. Re: JBoss API usage examples
          Peter Johnson Master

          You will want to use JMX. The JMX console will let you access that info from a browser. The twiddle utility will give you that access from the command line. All are referenced in the documentation.

          The only thing the documentation does not mention is exactly which MBeans to observe, but a little exploration using JMX console should reveal them. For example, a database connection pool can be monitored using the MBean named "Jboss.jca:name=xxxDS,service=ManagedConnectionPool" where xxxDS is the JNDI name of the data source.

          • 2. Re: JBoss API usage examples
            Danish Khan Newbie

             

            "PeterJ" wrote:
            You will want to use JMX. The JMX console will let you access that info from a browser. The twiddle utility will give you that access from the command line. All are referenced in the documentation.

            The only thing the documentation does not mention is exactly which MBeans to observe, but a little exploration using JMX console should reveal them. For example, a database connection pool can be monitored using the MBean named "Jboss.jca:name=xxxDS,service=ManagedConnectionPool" where xxxDS is the JNDI name of the data source.


            Thanks peter, I looked at the jmx console. Could you please provide a small concrete application example (the source code) which shows how to use methods being invoked in jmx.

            -Thanks,


            • 3. Re: JBoss API usage examples
              Peter Johnson Master

               

              public static void main(String[] args) throws Exception {
               Context ctx = new InitialContext();
               MBeanServerConnection mconn =(MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
               ObjectName name = new ObjectName("jboss.jca:name=xxxDS,service=ManagedConnectionPool");
               Object val=mconn.getAttribute(name,"InUseConnectionCount");
               System.out.println(name+"\n\tInUseConnectionCount="+val);
              }


              • 4. Re: JBoss API usage examples
                Danish Khan Newbie

                 

                "PeterJ" wrote:
                public static void main(String[] args) throws Exception {
                 Context ctx = new InitialContext();
                 MBeanServerConnection mconn =(MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
                 ObjectName name = new ObjectName("jboss.jca:name=xxxDS,service=ManagedConnectionPool");
                 Object val=mconn.getAttribute(name,"InUseConnectionCount");
                 System.out.println(name+"\n\tInUseConnectionCount="+val);
                }


                -Thanks Peter

                • 5. Re: JBoss API usage examples
                  Danish Khan Newbie

                   

                  "PeterJ" wrote:
                  public static void main(String[] args) throws Exception {
                   Context ctx = new InitialContext();
                   MBeanServerConnection mconn =(MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
                   ObjectName name = new ObjectName("jboss.jca:name=xxxDS,service=ManagedConnectionPool");
                   Object val=mconn.getAttribute(name,"InUseConnectionCount");
                   System.out.println(name+"\n\tInUseConnectionCount="+val);
                  }


                  Hi Peter,

                  upon running this code, I get the following exception..

                  Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
                  at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
                  at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
                  at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
                  at javax.naming.InitialContext.lookup(Unknown Source)
                  at JBossTest.main(JBossTest.java:12)


                  Any idea how to fix it?

                  -Thanks

                  • 6. Re: JBoss API usage examples
                    Peter Johnson Master

                    You do need a jndi.properties file to provide the proper initial context. And the client's classpath must include jbossall-client.jar. Here is the jndi.properties file:

                    java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                    java.naming.provider.url=jnp://localhost:1099
                    java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

                    • 7. Re: JBoss API usage examples
                      Danish Khan Newbie

                      Hi Peter,

                      I developed the application and used the method that you provided, and it worked perfectly on my local machine. However when I am trying to run it on server, it is giving me an exception. I did change the url from local host to server ip in jndi.properties file. I also matched the jboss-service.xml file to see the contents of <mbean code="org.jboss.naming.NamingService"

                      and they are same for my machine and the server.

                      So can you please think of any other reason why its still unable to connect and throwing the following exception?

                      Thanks,
                      -Danish.

                      /******* EXCEPTION START *************/

                      javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
                      at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:11
                      18)
                      at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1197)
                      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:515)
                      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:508)
                      at javax.naming.InitialContext.lookup(InitialContext.java:363)
                      at JBossStateReader.isMonitorItemFine(JBossStateReader.java:46)
                      at JBossStateReader.run(JBossStateReader.java:26)
                      at java.lang.Thread.run(Thread.java:810)
                      Caused by: java.net.SocketTimeoutException: Receive timed out
                      at java.net.PlainDatagramSocketImpl.receive0(Native Method)
                      at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java
                      :181)
                      at java.net.DatagramSocket.receive(DatagramSocket.java:724)
                      at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:10
                      88)


                      /******** EXCEPTION END **************/

                      • 8. Re: JBoss API usage examples
                        Peter Johnson Master

                        Did you start the app server with the -b option? See http://wiki.jboss.org/wiki/JBoss42FAQ

                        • 9. Re: JBoss API usage examples
                          Danish Khan Newbie


                          Hi Peter,

                          This is really weird; the port in jboss-serivce.xml was 1099

                          But when I saw it in jmx console, it was something else.

                          So now its working using that port.

                          Thanks,

                          -Danish.

                          • 10. Re: JBoss API usage examples
                            Peter Johnson Master

                            Sounds like the app server is using the binding service in which case all of the port binding are in a single file, in which case the settings in other locations, like in jboss-service.xml, are ignored.