create list of activemq queue - activemq

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.

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.

To create a list of queues, you have to create that list, then create each queue individually from the session object.
Queue q = session.createQueue("someQueueName")
This, however, does not really "create" a queue in that sense, since a queue is a persistent "thing" in the ActiveMQ process/server. This will only create a reference to an ActiveMQ queue given an identifier/name.
I'm not sure why you need ten queues right up. Typically, you have one queue per event type or use case (or similar), then use concurrent consumers to process in parallel.
But of course, you can always do somethings similar by a simple for loop, creating one queue at a time and attaching them to an arraylist. Note that you cannot get type safe queues with only Event objects in them.
You can send ObjectMessages with events though. Just create one:
Event event = createEvent(..); // Given "Event" is serializable (need to be able to persist it).
Message m = session.createObjectMessage(event);
// Send message as usual in ActiveMQ.
It might be that you need to rethink one or a few things in your code when converting from BlockingQueues to persistent ActiveMQ queues.

Related

Creating queues dynamical on MassTransit

I have a particular scenario with RabbitMQ that needs to have dynamically created queues and binds to exchanges, that are also dynamically created (not by me). This creation and binding is triggered by a new SignalR subscription.
This issue: https://github.com/MassTransit/MassTransit/issues/398 is about it, but I still don't know the answer.
Seems that mass transit is not very flexible on creating things on the move.
How can I achieve this? What if I stop the bus and recreate all the queues and bindings plus the new one, and start the bus again?
Thanks in advance.
Receive endpoints can be connected via the bus, as shown in the documentation.
For example:
var handle = bus.ConnectReceiveEndpoint("queue-name", x =>
{
x.Consumer<SomeConsumer>();
})
// the code below waits for the receive endpoint to be ready
// and throws an exception if a fault occurs
var ready = await handle.Ready;

Clear existing messages in activemq queue, just after starting application using activemq-cpp

I have an application which is acting as a consumer for a queue in activemq. This application is written on c++ and using activemq-cpp to get the services of activemq.
I want to achieve is when my application goes down and again comes up, it should first delete all the messages which gets populated in queue during the time my application is down that is it should first delete all the old messages in queue and then starts receiving new messages.
Is there any way to achieve this using activemq-cpp?
If you cast your Connection instance to an ActiveMQConnection there is a destroyDestination method that will remove the destination from the broker and all messages if there are no active subscriptions when called, otherwise it will throw an exception so be prepared for that. A small code snippet follows.
ActiveMQConnection* connection =
dynamic_cast<ActiveMQConnection*>( cmsConnection );
try {
connection->destroyDestination(destination);
} catch(Exception& ex) {
}

do i need to close the activemq connection if using pooledconnectionfactory

i am using activemq PooledConnectionFactory to create connection. I am creating threads and each thread would have its own connection, session and producer.
I have two queries:
1. Do i need to close connection,session, producer myself in code or pooledConnectionFactory would do it once the message sending is successful by producer.
2. creating connection for every thread (eventually for each message) would be a performance bottleneck. Is it possible to have only one connection with many sessions in it (or there should be one-to-one mapping between session and connection, I think I read this somewhere on activemq website)
Any help would be appreciated.
You need to use the code just as you would any other JMS Connection, Session, and Producer. There's not magic to detect when your thread is done with it, you need to close it which will return it to the pool. You can use only one Connection and take many sessions from it, but you need to close them so that they go back to the pool to be handed out to others on demand.

ActiveMQ producer creation and inactivity periods

I have a question relating ActiveMQ and producers.
Should I create a producer for every sending of a message? Or use the same one all the time? Is there a performance impact by creating a producer for every sending?
Also the connection gets down after some period of inactivity, but I don't know if it's related to this, any advices?
Yes, there is a small performance impact in creating a producer, especially if the broker is located on another machine (the clients needs to talk to broker to create a producer).
In the rest of this answer I assume you use Java/JMS to talk with AMQ.
If you have a very trivial program, you could of course "re use" your producers, create them with a "NULL" destination, and set the destination when sending.
What you could do to make it easy is to use the PooledConnectionFactory which pools connections, sessions and producers. I think that wrapper class will help you.
Actually, you could use the PooledConnectionFactory like this (psuedocode):
cf = new PooledConnectionFactory(myOriginalConnectionFactory)
sendMessage(cf)
sendMessage(cf)
sendMessage(cf)
SendMessage(connectionFactory)
conn = connectionFactory.CreateConnection
sess = conn.CreateSession
prod = sess.createProducer
msg = sess.createMessage
prod.send(msg)
prod.close
sess.close
conn.close
This means you don't have to worry about closed/open sessions, connections etc. This is the way the widely used JmsTemplate from Spring Framework works (and of course works a lot better with pooled/cached resources).
Also look at this page for performance tips and tricks.

How can I programatically purge an ActiveMQ queue using the Apache.NMS API?

I need the ability to purge a queue programatically using Apache.NMS (C#). I've been looking through the NMS API, but see no such capability. Does it exist?
There isn't a direct way to flush a Queue from the NMS API, that's more of a management function. You can cast a IConnection instance into an Apahce.NMS.ActiveMQ.Connection and then call DeleteDestination. This would work if there were no consumers on the Queue but will throw an exception if there are.
Not exactly sure what you mean by "flush" a queue (delete all messages?) but you can manage messages by setting the session transactional:
ISession consumerSession = = connection.CreateSession(AcknowledgementMode.Transactional);
Then you can use either:
//will remove message from queue on success
consumerSession.Commit();
or:
//on failure, back on queue
consumerSession.Rollback();