ActiveMQ configuration with Spring Boot

2019-02-09 05:09发布

问题:

I use ActiveMQ as Embedded with Spring Boot. It seems the Broker is created trough an ActiveMQConnectionFactory. I understand that the way to configure the broker is to set parameters in the query with broker. as described here : http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html

I would like to setup some features about the DLQ, so it's in the destinationPolicy attribute, but the attribute type is not a simple type but a complex type, how can I write the query parameter to disable DLQ, please ?

回答1:

Good question. The properties on the vm-transport for auto-broker creation are great, but only up to a point which I think you have hit.

My suggestion is that you define the broker configuration as you normally would have done in XML and then just refer to this xml in the URI. Destination policies are indeed a complex structure and I don't see how it would be a good idea to define them with simple query params even if it was possible.

vm://localhost?brokerConfig=xbean:activemq.xml 


回答2:

Complementing @Petter and @April answers, below the same solutions but with more complete samples:

1. Petter solution, import activemq.xml at connnection factory url

build.gradle

ext {
    springBootVersion = "1.5.3.RELEASE"
    activeMQVersion = "5.14.5"
}

dependencies {

    compile("org.springframework.boot:spring-boot-starter-activemq:${springBootVersion}")
    compile("org.apache.activemq:activemq-broker:${activeMQVersion}")

    testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
    testCompile group: 'org.apache.activemq', name: 'activemq-spring', version: "${activeMQVersion}"
    testCompile("junit:junit:4.12")

}

src/main/resources/activemq.xml

<beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:amq="http://activemq.apache.org/schema/core"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
                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.4.0.xsd
        ">
    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="broker1" persistent="false" >
        <transportConnectors>
            <transportConnector name="vm" uri="vm://broker1"/>
        </transportConnectors>
    </broker>
</beans>

Config.java

@EnableJms
@SpringBootApplication
@EnableAutoConfiguration
@Configuration
public class Config {}

application.properties

spring.activemq.broker-url=vm://broker1?brokerConfig=xbean:activemq.xml

2. April solution, import activemq.xml at Spring Configuration

Just remove application.properties then add @ImportResource("classpath:activemq.xml") entry to Config.java

Config.java

@EnableJms
@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ImportResource("classpath:activemq.xml")
public class Config {}


回答3:

I had this problem and solved it by using a spring configuration file. In my case, I wanted to configure my broker to persist.

I added the needed libs in my pom: including activemq-broker, activemq-spring, spring-jms (and in my case, activemq-leveldb-store).

My spring xml file looked like this:

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

 <broker xmlns="http://activemq.apache.org/schema/core" brokerName="xyz">
      <persistenceAdapter>
        <levelDB directory="activemq-data"/>
      </persistenceAdapter>

    <transportConnectors>
      <transportConnector uri="vm:localhost?persistent=true" />
    </transportConnectors>

  </broker>
</beans>

And I registered the spring file in one of my configuration classes:

@ImportResource("activemq-spring.xml")

That did the job.

I tried the xbeans solution first, but I got stuck because I was missing some xbeans classes, and I didn't know if it was a version thing or what. I'm using activemq 5.12.1