title: Fetching kafka mbeans using jmx with java
body: <p>I'm trying to get mbeans of Kafka using Java code ( consumer or producer ) like they appear in jconsole
  In jconsole I can see the name of the mbean ( like : <code>objetName:kafka.consumer:type=consumer-fetch-manager-metrics,client-id=consumer-1</code>)</p>
  <p>so here is my code :</p>
  <pre><code>JMXServiceURL(&quot;service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi&quot;);
  </code></pre>
  <p>where port is like in the  config file ( <code>server.properties</code>)  that I set (<strong>I set port in Kafka config file ( SET JMX_PORT=9098</strong> )
  , I also tried to use my Kafka port  ( 9092) but than I get address in use -</p>
  <pre><code> bean=new ObjectName(&quot;kafka.consumer:type=consumer-coordinator-metrics,client-id=consumer-1&quot;);
  MBeanInfo info= connection.getMBeanInfo(bean);
  </code></pre>
  <p>I get InstanceNotFoundException</p>
  <p>when I  use this  code :</p>
  <pre><code>domains=connection.getDomains();
  </code></pre>
  <p>I get all the domains except kafka.consumer ( i.e <code>java.nio,java.util.logging , java.lang</code> .. etc )
  but not the <code>kafka.server</code> even though I can see the <code>kafka.consumer</code> mbeans in Jconsole ..</p>
  <p>but, I don't know how to access the JMXServiceURL:
  I tried to give the folloiwing : new</p>
  <p>I also tries this :</p>
  <pre><code>MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  </code></pre>
  <p>but after I get the server bean , I cannot see Kafka mbeans (just other custom mbeans that i created or others but not Kafka mbeans )</p>
  <p>I think that Kafka exposes the port to jconsole in some port which I cannot figure it</p>
  <p>Any ideas ?</p>