Search code examples
javajmsactivemq-classicmessagebroker

What is the message broker for in this ActiveMQ example?


I'm new to ActiveMQ and tested this implementation for request/response.

I run Apache ActiveMQ on the same machine.

It works as expected, however I wonder, what the Messagebroker is used for in the server code:

    try {
        //This message broker is embedded
        BrokerService broker = new BrokerService();
        broker.setPersistent(false);
        broker.setUseJmx(false);
        broker.addConnector(messageBrokerUrl);
        broker.start();
    } catch (Exception e) {
        //Handle the exception appropriately
    }

When I remove this block completely, it works anyway, so what is it for? I thought, that a message broker is implemented in the ActiveMQ which I started before and which is used to read from the queue. Why do I need an "embedded" broker here?


Solution

  • It helps to understand that ActiveMQ is essentially "just a library". You can run it as a stand alone server (as you are on your machine), or embed it within Java code, unit test, or a Java app. The example on that webpage uses an embedded ActiveMQ broker (server-side) to provide an all-in-one view of the message flow.

    Since you are running a stand alone ActiveMQ broker on the same machine, that embedded broker is most likely not being used-- since only one process can listen on port 61616 at a time per machine.