title: Disable jmx in activemq network of brokers (spring, xbean)
body: <p>Since I've struggled a lot with this problem, I am posting my solution.
  Disabling jmx in an activemq network of brokers removes race conditions about the registration of the jmx connector. When starting multiple activemq servers on the same machine:</p>
  <blockquote>
  <p>Failed to start jmx connector: Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]</p>
  </blockquote>
  <p>Another problem with this is, that even if you don't cause a race condition, this exception can still occur. Even when starting one broker after another while waiting for them to initialize properly in between. If one process is run by root as the first instance and the other as a normal user, somehow the user process tries to register its own jmx connector, though there already is one.</p>
  <p>Or another exception which happens when the broker that successfully registered the jmx connector goes down:</p>
  <blockquote>
  <p>Failed to start jmx connector: Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused]</p>
  </blockquote>
  <p>Those exceptions cause the network of brokers to stop working, or to not work at all.
  The trick to disable jmx was, that jmx had to be disabled in the connectionfactory aswell.
  The documentation <a href="http://activemq.apache.org/jmx.html" rel="noreferrer">http://activemq.apache.org/jmx.html</a> does not say that this is needed explicitly. So I had to struggle for 2 days until I found the solution:</p>
  <pre><code>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://activemq.apache.org/schema/core
  http://activemq.apache.org/schema/core/activemq-core-5.3.1.xsd&quot;&gt;
  
  &lt;!-- Spring JMS Template --&gt;
  &lt;bean id=&quot;jmsTemplate&quot; class=&quot;org.springframework.jms.core.JmsTemplate&quot;&gt;
      &lt;constructor-arg ref=&quot;connectionFactory&quot; /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Caching, sodass das jms template überhaupt nutzbar ist in sachen performance --&gt;
  &lt;bean id=&quot;connectionFactory&quot; class=&quot;org.springframework.jms.connection.CachingConnectionFactory&quot;&gt;
      &lt;constructor-arg ref=&quot;amqConnectionFactory&quot; /&gt;
      &lt;property name=&quot;exceptionListener&quot; ref=&quot;jmsExceptionListener&quot; /&gt;
      &lt;property name=&quot;sessionCacheSize&quot; value=&quot;1&quot; /&gt;
  &lt;/bean&gt;
  
  &lt;!--
      Jeder Client verbindet sich mit seinem eigenen broker, broker sind untereinander vernetzt. Nur wenn hier
      nochmals jmx deaktiviert wird, bleibt es auch deaktiviert...
  --&gt;
  &lt;amq:connectionFactory id=&quot;amqConnectionFactory&quot; brokerURL=&quot;vm://broker:default?useJmx=false&quot; /&gt;
  
  &lt;!--
      Broker suchen sich einen eigenen Port und sind gegenseitig verbunden, ergeben dadurch ein Grid. Dies zwar etwas
      langsamer, aber dafür ausfallsicherer. Siehe http://activemq.apache.org/networks-of-brokers.html
  --&gt;
  &lt;amq:broker useJmx=&quot;false&quot; persistent=&quot;false&quot;&gt;
      &lt;!-- Wird benötigt um JMX endgültig zu deaktivieren --&gt;
      &lt;amq:managementContext&gt;
          &lt;amq:managementContext connectorHost=&quot;localhost&quot; createConnector=&quot;false&quot; /&gt;
      &lt;/amq:managementContext&gt;
      &lt;!-- Nun die normale Konfiguration für Network of Brokers --&gt;
      &lt;amq:networkConnectors&gt;
          &lt;amq:networkConnector networkTTL=&quot;1&quot; duplex=&quot;true&quot; dynamicOnly=&quot;true&quot; uri=&quot;multicast://default&quot; /&gt;
      &lt;/amq:networkConnectors&gt;
      &lt;amq:persistenceAdapter&gt;
          &lt;amq:memoryPersistenceAdapter /&gt;
      &lt;/amq:persistenceAdapter&gt;
      &lt;amq:transportConnectors&gt;
          &lt;amq:transportConnector uri=&quot;tcp://localhost:0&quot; discoveryUri=&quot;multicast://default&quot; /&gt;
      &lt;/amq:transportConnectors&gt;
  &lt;/amq:broker&gt;
  
  &lt;/beans&gt;
  </code></pre>
  <p>With this, there is no need to specify -Dcom.sun.management.jmxremote=false for the jvm. Which somehow also didn't work for me, because the connectionfactory started the jmx connector.</p>
  <h1>Edit:</h1>
  <p>Tonys answer brought me to rethinking the configuration and I found a simplified version which works aswell.</p>
  <pre><code>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.apache.org/schema/core
  http://activemq.apache.org/schema/core/activemq-core-5.3.2.xsd&quot;&gt;
  
  &lt;!-- Caching, sodass das jms template überhaupt nutzbar ist in sachen performance --&gt;
  &lt;bean id=&quot;connectionFactory&quot; class=&quot;org.springframework.jms.connection.CachingConnectionFactory&quot;&gt;
      &lt;constructor-arg ref=&quot;amqConnectionFactory&quot; /&gt;
      &lt;property name=&quot;exceptionListener&quot; ref=&quot;jmsExceptionListener&quot; /&gt;
      &lt;property name=&quot;sessionCacheSize&quot; value=&quot;1&quot; /&gt;
  &lt;/bean&gt;
  
  &lt;!--
      Jeder Client verbindet sich mit seinem eigenen broker, broker sind untereinander vernetzt. Nur wenn hier nochmals jmx
      deaktiviert wird, bleibt es auch deaktiviert...
  --&gt;
  &lt;amq:connectionFactory id=&quot;amqConnectionFactory&quot; brokerURL=&quot;vm://default?broker.persistent=false&quot; /&gt;
  
  &lt;!--
      Broker suchen sich einen eigenen Port und sind gegenseitig verbunden, ergeben dadurch ein Grid. Dies zwar etwas
      langsamer, aber dafür ausfallsicherer. Siehe http://activemq.apache.org/networks-of-brokers.html
  --&gt;
  &lt;amq:broker useJmx=&quot;false&quot; persistent=&quot;false&quot;&gt;
      &lt;amq:networkConnectors&gt;
          &lt;amq:networkConnector networkTTL=&quot;1&quot; conduitSubscriptions=&quot;true&quot; duplex=&quot;true&quot; dynamicOnly=&quot;true&quot;
              uri=&quot;multicast://default&quot; /&gt;
      &lt;/amq:networkConnectors&gt;
      &lt;amq:persistenceAdapter&gt;
          &lt;amq:memoryPersistenceAdapter /&gt;
      &lt;/amq:persistenceAdapter&gt;
      &lt;amq:transportConnectors&gt;
          &lt;amq:transportConnector uri=&quot;tcp://localhost:0&quot; discoveryUri=&quot;multicast://default&quot; /&gt;
      &lt;/amq:transportConnectors&gt;
  &lt;/amq:broker&gt;
  </code></pre>