Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How queue is create in ActiveMQ?

Tags:

java

jms

activemq

Here is the code snippet from ActiveMQ HelloWorld Example for creating queue using ActiveMQ

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");

As per ActiveMQ docs

This facility is provided for the rare cases where clients need to dynamically manipulate queue identity

It looks like developer should not create queue with createQueue. If yes, how should a developer should create a queue? should he created by ui or some other means instead of doing it programattically?

Then it further says

this method is not for creating the physical queue. The physical creation of queues is an administrative task and is not to be initiated by the JMS API.

I did not get what does above statement mean? As per my understanding, developer should cteate queue manually.Th either thru web ui or command prompt. createQueue method will just return the object associated with queue created manually?

like image 615
user3198603 Avatar asked Feb 26 '14 17:02

user3198603


2 Answers

In ActiveMQ you do not have to create destinations before they can be used. The ActiveMQ broker creates the physical resources associated with a destination on demand, so if you call createQueue() on a JMS session, it will create the queue for you if it does not already exist.

See: http://activemq.apache.org/how-do-i-create-new-destinations.html

like image 198
Jaap Avatar answered Oct 13 '22 15:10

Jaap


Think on your JMS provider as a database which you can't issue administrative commands, such as "create table" or "drop table".

Someone has to perform these administrative tasks for you, so your client code can access those tables and perform selects, updates, deletes, etc.

JMS is like this. The JMS API does not allow you to create a new queue, only to access an existent one and add things to it (producer) or remove things from it (consumer).

So who creates the queue? If you're running for example an embedded JMS instance in some application server, for example, the queues are defined in a configuration file and the container has the responsability to create the necessary structures on startup so you cna use them.

Or if you are using a JMS standalone server, then the JMS implementation has its own API to do such things of course, but among different JMS providers, this procedure is not standand.

In the same way, think how SQL is a standard to allow someone to do quite the same things with different DBMSes, but at the same time, there's no standard for how your're going to administer these DBMSes.

I think the method "createQueue()" is a bad name, because it's not creating a queue, but it's creating a Destination (which is actually what it returns). A Destination is a logical abstraction of the queue where you can plug a consumer or a producer. But that's it. Just a reference to an existent queue.

Now answering your question :-) for example, using tomee+, which is a tomcat + JEE libraries, inclusing activeMQ, you can run an embedded JMS instance and use it just like this

http://tomee.apache.org/jms-resources-and-mdb-container.html

So, how the queue is created? It's created by a configuration file :-)

like image 28
Leo Avatar answered Oct 13 '22 13:10

Leo