Java DSLs for ActiveMQ config and testing

As part of a larger project that I am working on, I needed to define numerous different ActiveMQ configurations inside non-trivial integration tests. Rather than instantiating brokers by hand using the internal ActiveMQ classes, or writing lots of similar-but-different XML configurations, I wrote a pair of Java DSLs to allow me to quickly spin up brokers and proxy them in JUnit using a style that should be familiar to Camel users, and a syntax that ActiveMQ users should instantly understand.

The ActiveMQ DSL is a general purpose utility for instantiating brokers in-line.

BrokerContext context = new BrokerContext(
    ActiveMQBrokers.broker("embeddedBroker").useJmx(false).persistent(false)
        .transportConnectors()
            .transportConnector("openwire", "tcp://0.0.0.0:61616").end()
        .end());

context.start();
// later ...
context.end();

The Test DSL, uses JUnit’s @Rule functionality to manage the lifecycle of these brokers and also allows the creation of proxies that can be used to simulate network interruptions.

public class MyEmbeddedBrokerTest {
    @Rule
    public ProxiedBrokerResource broker = new BrokerResource(
            ActiveMQBrokers.broker("embeddedBroker").useJmx(false).persistent(false)
                .transportConnectors()
                    .transportConnector("openwire", "tcp://0.0.0.0:61616").end()
                    .transportConnector("stomp", "stomp://0.0.0.0:61618").end()
                .end())
            .withProxied("openwire").port(10000); 
            // port to be proxied is looked up by name
            // you can define multiple proxies for the one broker!

    @Test
    public void testNetworkOutages() {
        ConnectionFactory cf =
            new ActiveMQConnectionFactory(
                "failover:(" + broker.getTcpConnectionUri("openwire") + ")"); 
        // returns proxied port 10000
        
        // ...
        SocketProxy socketProxy = broker.getProxy("openwire");
        socketProxy.close(); // network goes down
        // ...
        socketProxy.reopen(); // network comes back up
    }
}

I hope that they might be useful (or at least instructive) to others. The DSLs along with full documentation are available on Github at jkorab/activemq-dsls. Feedback + pull requests most welcome.


Posted

in

by

Tags: