Java 队列是如何在 ActiveMQ 中创建的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22048537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:15:40  来源:igfitidea点击:

How queue is create in ActiveMQ?

javajmsactivemq

提问by user3198603

Here is the code snippet from ActiveMQ HelloWorld Examplefor creating queue using ActiveMQ

下面是来自ActiveMQ HelloWorld Example的代码片段, 用于使用 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

根据ActiveMQ 文档

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?

看起来开发人员不应该使用 createQueue 创建队列。如果是,开发人员应该如何创建队列?他应该通过 ui 或其他方式创建而不是通过编程方式创建吗?

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.

此方法不适用于创建物理队列。队列的物理创建是一项管理任务,不会由 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?

我没明白上面的说法是什么意思?根据我的理解,开发人员应该手动创建队列。通过 web ui 或命令提示符。createQueue 方法只会返回与手动创建的队列关联的对象?

采纳答案by Leo

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

将您的 JMS 提供程序视为一个数据库,您无法发出管理命令,例如“创建表”或“删除表”。

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).

JMS就是这样。JMS API 不允许您创建新队列,只能访问现有队列并向其中添加内容(生产者)或从中删除内容(消费者)。

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.

那么谁来创建队列呢?例如,如果您在某个应用程序服务器中运行嵌入式 JMS 实例,则队列在配置文件中定义,并且容器有责任在启动时创建必要的结构,以便您可以使用它们。

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.

或者,如果您使用的是 JMS 独立服务器,那么 JMS 实现当然有自己的 API 来做这些事情,但是在不同的 JMS 提供者之间,这个过程不是标准的。

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.

以同样的方式,想想 SQL 如何成为允许某人用不同的 DBMS 做完全相同的事情的标准,但同时,没有关于您将如何管理这些 DBMS 的标准。

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.

我认为方法“createQueue()”是一个不好的名字,因为它不是在创建队列,而是在创建一个目的地(这实际上是它返回的内容)。目的地是队列的逻辑抽象,您可以在其中插入消费者或生产者。但就是这样。只是对现有队列的引用。

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

现在回答你的问题 :-) 例如,使用 tomee+,它是一个 tomcat + JEE 库,包括 activeMQ,你可以运行一个嵌入式 JMS 实例并像这样使用它

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

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

那么,队列是如何创建的呢?它是由配置文件创建的:-)

回答by Jaap

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.

在 ActiveMQ 中,您不必在使用目标之前创建目标。ActiveMQ 代理按需创建与目标关联的物理资源,因此如果您在 JMS 会话上调用 createQueue(),它会为您创建队列(如果该队列尚不存在)。

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

请参阅:http: //activemq.apache.org/how-do-i-create-new-destinations.html