进程内 ActiveMQ 生产者/消费者示例?

发布于 2024-07-16 08:48:49 字数 543 浏览 7 评论 0原文

我正在研究使用 ActiveMQ 作为我的嵌入式进程内消息队列 应用程序,但我对如何启动这样的应用程序有点困惑 向上。 我的设想是这样的(当然是伪代码):

configureBroker ()
broker.start ()

createProducer (broker)
producer.start ()

for each desired consumer
    createConsumer (broker)
    consumer.start ()

waitForSignal ()
signalProducerShutdown ()

waitForEmptyQueues ()
signalConsumerShutdown ()

broker.stop ()

我试图组装一个简单的版本,但我不知道如何编写 生产者和消费者以让他们永远工作的方式,或者 直到被告知退出。 做这个的最好方式是什么? 我专门谈论线程方面; 我需要/想要在自己的线程中生成什么,等等...

我对基于消息队列的应用程序完全陌生,所以请详细说明您的示例。

I'm investigating using ActiveMQ as an embedded in-process message queue in my
application, but I'm a bit stuck on how I go about starting such an application
up. I envision it like so (pseudocode, of course):

configureBroker ()
broker.start ()

createProducer (broker)
producer.start ()

for each desired consumer
    createConsumer (broker)
    consumer.start ()

waitForSignal ()
signalProducerShutdown ()

waitForEmptyQueues ()
signalConsumerShutdown ()

broker.stop ()

I've tried to assemble a simple version of this, but I'm stuck on how to write
the producers and consumers in such a way as to have them work forever, or
until told to quit. What is the best way to do this? I'm speaking specifically about the threading aspect; what do I need/want to spawn off in its own thread, etc...

I'm completely new to message queue based applications, so please be verbose with your examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-07-23 08:48:49

当您指定 ActiveMQConnectionFactory 时,您可以指定“vm://”,其中 name 是代理的虚拟机内特定名称,它将在虚拟机内启动代理。

例如,

String broker = "vm://stackOverflowTest";
ActiveMQConnectionFactory connectionFactory = 
        new ActiveMQConnectionFactory(broker);
Connection amqcon = connectionFactory.createConnection();
amqcon.start();

从那里您可以创建生产者或消费者,就像通过网络一样。 只要您对代理使用相同的名称,您就可以打开多个线程/代码/与同一个虚拟机实例进行对话。

该解决方案仅允许与VM通信,不开放任何外部端口。 我假设这就是您正在寻找的,因为您说您想要嵌入式进程内队列。

When you specify the ActiveMQConnectionFactory, you can specify "vm://" where name is the intra-vm specific name of your broker and it will start the broker within the VM.

For example,

String broker = "vm://stackOverflowTest";
ActiveMQConnectionFactory connectionFactory = 
        new ActiveMQConnectionFactory(broker);
Connection amqcon = connectionFactory.createConnection();
amqcon.start();

From there you can create your producers or consumers the same as if it was over the network. As long as you use the same name for the broker, you can have multiple threads / code open / talk to the same VM instance.

This solution only allows communication with the VM, it does not open any external ports. I'm assuming this is what you were looking for since you said you wanted embedded, in-process queues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文