You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by João Nuno Silva <jn...@gmail.com> on 2014/12/30 16:21:43 UTC

Programmatically create destinations in broker (not client-side handles)

Hello,

I'm using ActiveMQ 5.10.0 in Java 8. I'm not using any container, just
*two* simple Java SE applications on the same machine. My goal is to have
*one* application launch an *embedded* broker and have the other connect to
it using tcp. I am perfectly fine with using ActiveMQ specific code, I
don't need to be vendor agnostic.

The difficulty I'm facing is that I need to *ensure a topic gets created on
the broker* when I call my API create topic method. I need this method to
work on both applications, the one that started the broker, and the one
that just connects to it. What I'm currently doing is:


// Instantiate the broker service
BrokerService brokerService = new BrokerService();
brokerService.setPersistent(false);

// Start the broker, this fails on the 2nd jvm with a bind exception
(address already in use) which is fine
brokerService.addConnector("tcp://localhost:61616");
brokerService.start();
brokerService.waitUntilStarted();

ConnectionFactory connFactory = new
ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connFactory.createConnection();
connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic clientSideTopic = session.createTopic("foo");

ConnectionContext connectionContext =
BrokerSupport.getConnectionContext(brokerService.getBroker());

// Create the topic on the broker!
brokerService.getBroker().addDestination(connectionContext,
clientSideTopic, true);


The problem is that on the 2nd process, the one that should not instantiate
the broker, because 1st process already did, I cannot do
brokerService.getBroker() because I don't really have a broker...

How can I get an instance of a remote BrokerService or some other way to
achieve immediate topic creation on the broker?

BTW, not that it matters to the question, but the reason I need this is
because this is an implementation of an existing API which demands these
particular semantics.

Thanks in advance!