Restart embedded broker in unit test : VMTransportServer already bound
Asked Answered
S

1

7

I'm trying to write a test that simulates a "broker down" phase. Therefore I want to

  1. start a local broker
  2. send message1
  3. stop the broker
  4. send message2 (which will of course not arrive)
  5. start the broker again
  6. send message3

According to http://activemq.apache.org/how-do-i-restart-embedded-broker.html it is recommended to init a new BrokerService to start the broker again. So the code looks (almost) like this:

private BrokerService _broker;

private void startBroker() throws Exception {
    _broker = new BrokerService(); 
    _broker.addConnector("vm://localhost?broker.persistent=false");
    _broker.start();
    _broker.waitUntilStarted();
}

private void stopBroker() throws Exception {
    _broker.stop();
    _broker.waitUntilStopped();
}

@Test
public void publishMessagesWithServerBreakdownInBetween()
    throws Exception
{
    startBroker();
    ... send and receive message (works fine)
    stopBroker();
    ... send message (fails of course)  
    startBroker(); // this fails with java.io.IOException: VMTransportServer already bound at: vm://localhost?broker.persistent=false
    ... send and receive message
}

The problem is already mentioned as comment in code: The restart of the broker fails due to the error : java.io.IOException: VMTransportServer already bound at: vm://localhost?broker.persistent=false

I found a similar problem at ActiveMQ forum (http://activemq.2283324.n4.nabble.com/VMTransportServer-already-bound-td2364603.html), but in my case the hostname isn't null.

Another idea was to set 2 different broker names, but that also didn't help.

What am I doing wrong?

Shalna answered 19/8, 2013 at 11:55 Comment(1)
Just curious, why do you need to start/stop the broker between tests?Seismo
M
0

You want to control what the VM Transport does by telling it not to try and create a broker for you since you are adding it to an already created broker. The rest is pretty simply then:

public class AMQRestartTest {

    private BrokerService broker;
    private String connectorURI;
    private ActiveMQConnectionFactory factory;

    @Before
    public void startBroker() throws Exception {
        createBroker(true);
        factory = new ActiveMQConnectionFactory("failover://" + connectorURI);
    }

    private void createBroker(boolean deleteAllMessages) throws Exception {
        broker = new BrokerService();
        TransportConnector connector = broker.addConnector("vm://localhost?create=false");

        broker.setPersistent(false);
        broker.start();
        broker.waitUntilStarted();

        connectorURI = connector.getConnectUri().toString();
    }

    @Test(timeout = 60_000)
    public void test() throws Exception {
        Connection connection = factory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("test");
        MessageConsumer consumer = session.createConsumer(queue);
        MessageProducer producer = session.createProducer(queue);

        connection.start();

        broker.stop();
        broker.waitUntilStopped();
        createBroker(false);

        producer.send(session.createTextMessage("help!"));

        Message received = consumer.receive();

        assertNotNull(received);
        assertTrue(received instanceof TextMessage);
    }
}
Margie answered 25/3, 2020 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.