Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create list of activemq queue

my existing code which uses BlockingQueue creates a list of BlockingQueue (like private List> queues;) in which I can put messages to process. However due to persistence issue we plan to shift to activemq. Can anyone please help me if we can get a list of activemq queue (in java program not from configuration file). I know that I can use createQueue on session to create a single instance of the queue but I want list of queue like done for BlockingQueue.

Any help would be much appreciated.

like image 290
Manglesh Avatar asked Mar 12 '26 22:03

Manglesh


1 Answers

You can get a list of the available queues using DestinationSource from your connection.

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();

edit: to create a queue take a look at ActiveMQ Hello world sample link What the code does there is creating a connection to an activeMQ-broker embedded in the jvm

// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();

// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");

The thing that might not be obvious with above code is that the line:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

will not only setup a connection to a broker, but embedded a broker inside the connection if there isn't one already. Explained at the bottom of this page

That feature can be turned of using (you need a broker, but if you want to set it up in other way):

ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");

I really like ActiveMQ, but it offers a lot more than persistence, so things might seem a little overly complex when doing the simple things. But hope that will not scare you.

like image 91
Aksel Willgert Avatar answered Mar 17 '26 02:03

Aksel Willgert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!