Could not start 2 embedded active-mq on different

2020-01-29 18:06发布

问题:

I have 2 spring-boot applications. Each application has embedded active mq broker.

I need to have 2 applications working on the same PC in parallel but it doesn't work now. First application always starts successfully:

2018-02-02 11:48:20.095  INFO 7660 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@53045c6c: startup date [Fri Feb 02 11:48:20 MSK 2018]; root of context hierarchy
2018-02-02 11:48:20.923  INFO 7660 --- [  JMX connector] o.a.a.broker.jmx.ManagementContext       : JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:7777/jmxrmi
2018-02-02 11:48:20.923  INFO 7660 --- [           main] o.apache.activemq.broker.BrokerService   : Using Persistence Adapter: KahaDBPersistenceAdapter[D:\work\arbitrage_robot\root\activemq-data\localhost\KahaDB]
2018-02-02 11:48:21.231  INFO 7660 --- [           main] o.a.a.store.kahadb.MessageDatabase       : KahaDB is version 6
2018-02-02 11:48:21.260  INFO 7660 --- [           main] o.a.a.store.kahadb.plist.PListStoreImpl  : PListStore:[D:\work\arbitrage_robot\root\activemq-data\localhost\tmp_storage] started
2018-02-02 11:48:21.395  INFO 7660 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.2 (localhost, ID:ntkachev-60101-1517561301266-0:1) is starting
2018-02-02 11:48:21.441  INFO 7660 --- [           main] o.a.a.t.TransportServerThreadSupport     : Listening for connections at: tcp://127.0.0.1:61617?broker.persistent=false
2018-02-02 11:48:21.442  INFO 7660 --- [           main] o.a.activemq.broker.TransportConnector   : Connector tcp://127.0.0.1:61617?broker.persistent=false started
2018-02-02 11:48:21.442  INFO 7660 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.2 (localhost, ID:ntkachev-60101-1517561301266-0:1) started
2018-02-02 11:48:21.442  INFO 7660 --- [           main] o.apache.activemq.broker.BrokerService   : For help or more information please see: http://activemq.apache.org
2018-02-02 11:48:23.002  INFO 7660 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-02-02 11:48:23.021  INFO 7660 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2018-02-02 11:48:23.024  INFO 7660 --- [           main] o.a.activemq.broker.TransportConnector   : Connector vm://localhost started
2018-02-02 11:48:23.051  INFO 7660 --- [           main] pack.Application                 : Started Application in 3.337 seconds (JVM running for 3.922)

but second freezes and print:

2018-02-02 11:48:30.450  INFO 17008 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@53045c6c: startup date [Fri Feb 02 11:48:30 MSK 2018]; root of context hierarchy
2018-02-02 11:48:31.296  INFO 17008 --- [           main] o.apache.activemq.broker.BrokerService   : Using Persistence Adapter: KahaDBPersistenceAdapter[D:\work\app\root\activemq-data\localhost\KahaDB]
2018-02-02 11:48:31.297  INFO 17008 --- [  JMX connector] o.a.a.broker.jmx.ManagementContext       : JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
2018-02-02 11:48:31.300  INFO 17008 --- [           main] o.a.activemq.store.SharedFileLocker      : Database activemq-data\localhost\KahaDB\lock is locked by another server. This broker is now in slave mode waiting a lock to be acquired

Both applications have approximately the same configuration:

@SpringBootApplication
public class Application {

  @Bean
  public BrokerService broker() throws Exception {
    BrokerService broker = new BrokerService();
    broker.addConnector("tcp://localhost:61616?broker.persistent=false");
    return broker;
  }

  @JmsListener(destination = "robotCommand")
  public void listen(String in) {
      System.out.println(in);
  }

and following dependencies:

   <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-kahadb-store</artifactId>
        <version>5.15.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-broker</artifactId>
        <version>5.15.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
        <version>1.5.10.RELEASE</version>
    </dependency>

How can I fix this(application hanging on startup)?

回答1:

your config broker.addConnector("tcp://localhost:61616?broker.persistent=false"); is wrong, this is can be done only if you create a broker using ActiveMQConnectionFactory, http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html#HowdoIembedaBrokerinsideaConnection-UsingActiveMQConnectionFactory

Using ActiveMQConnectionFactory
An embedded broker can also be created using an ActiveMQConnectionFactory and using a vm connector as a uri. e.g.
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

try

 @Bean
  public BrokerService broker() throws Exception {
    BrokerService broker = new BrokerService();
    broker.addConnector("tcp://localhost:61616");
    broker.setPersistent(false);
    return broker;
  }