title: Spring boot - sharing embedded JMS broker with separate service
body: <p>I have two services which should communicate with each other via ActiveMQ. Everything works nicely when I have the receiver and sender in one service but when I split them, I got a strange activemq exception.</p>
  
  <p>This is configuration of service A:</p>
  
  <pre><code>@EnableScheduling
  @SpringBootApplication
  @EnableJms
  public class App extends SpringBootServletInitializer {
  
    private static final Logger log = LoggerFactory.getLogger(App.class);
  
    @Autowired
    private static JmsTemplate jms;
  
    public static void main(String[] args) {
      SpringApplication.run(App.class, args);
    }
  
  }
  </code></pre>
  
  <p>Sending messages:</p>
  
  <pre><code>  @Autowired
    private JmsTemplate jms;
  
    public void sendTicket(Reservation reservation) {
      log.debug("---------------sending message----------------");
      // Send a message
      jms.send("mailbox-destination", new MessageCreator() {
        public ObjectMessage createMessage(Session session) throws JMSException {
          ObjectMessage message = session.createObjectMessage();
          message.setObject(reservation);
          return message;
        }
      });
    }
  </code></pre>
  
  <p>The JMS is configured as in-memory queue:</p>
  
  <pre><code>spring.activemq.in-memory=true
  spring.activemq.pooled=false
  </code></pre>
  
  <p>Service B is similar but id does not define the JmsContainerFactory. It only has the receiver:</p>
  
  <pre><code>@Component
  public class Receiver {
  
    private static final Logger log = LoggerFactory.getLogger(Receiver.class);
  
    /**
     * Receive a message with reservation and print it out as a e-ticket.
     * 
     * @param reservation
     */
    @JmsListener(destination = "mailbox-destination")
    public void receiveMessage(Reservation reservation) {
      log.info("Received &lt;" + reservation.getId() + "&gt;");
    }
  }
  </code></pre>
  
  <p>Service A have JMS and ActiveMQ broker as maven dependencies:</p>
  
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
              &lt;artifactId&gt;activemq-broker&lt;/artifactId&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Service B has only the JMS dependency.</p>
  
  <p>Could you give me a hint how to share the bean between both services and send messages between them? I am completely new to this topic.</p>
  
  <p>I am getting the following exception when receiving a message</p>
  
  <pre><code>org.springframework.jms.UncategorizedJmsException: Uncategorized exception occured during JMS processing; nested exception is javax.jms.JMSException: Could not create Transport. Reason: javax.management.InstanceAlreadyExistsException: org.apache.activemq:type=Broker,brokerName=localhost
  </code></pre>
  
  <p>EDIT: If I remove the broker dependency from one of the services, the Tomcat does not even start:</p>
  
  <pre><code>java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
      at java.util.concurrent.FutureTask.report(FutureTask.java:122)
      at java.util.concurrent.FutureTask.get(FutureTask.java:192)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:917)
      at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:625)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
  Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: org.apache.catalina.LifecycleException: A child container failed during start
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:925)
      at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 6 common frames omitted
  </code></pre>