简体   繁体   English

使用RabbitMQ作为java工作队列时,应该如何处理并发和瞬态错误?

[英]When using RabbitMQ as a java work queue, how should you handle concurrency and transient errors?

I'm considering setting up a RabbitMQ broker to handle basic task processing for a java web application.我正在考虑设置一个 RabbitMQ 代理来处理 Java Web 应用程序的基本任务处理。 The basic idea is that the producer is a web server that wants to be speedy about processing requests, but also needs to do some data processing.其基本思想是生产者是一个网络服务器,想要快速处理请求,但也需要做一些数据处理。 To accomplish this, it knows about some Job DTOs that can be serialized to AMQP messages, and somewhere out there is a consumer that knows how to process these jobs.为了实现这一点,它知道一些可以序列化为 AMQP 消息的作业 DTO,并且在某个地方有一个消费者知道如何处理这些作业。 Classic.经典的。

After reading the RabbitMQ documentation, I'm still left with a couple of questions.在阅读了 RabbitMQ 文档后,我仍然有几个问题。

  1. I want the consumer application to fully utilize its CPU for processing messages, and I'm wondering if RabbitMQ already provides the worker pool.我希望消费者应用程序能够充分利用其 CPU 来处理消息,我想知道 RabbitMQ 是否已经提供了工作池。 Assuming I wanted 4 worker threads, is it enough to register 4 channels with 1 consumer each on the same connection?假设我想要 4 个工作线程,是否足以在同一个连接上向 1 个消费者注册 4 个通道? In that case, work would be done in handleDelivery() and an ack would be sent if it successfully completes.在这种情况下,将在 handleDelivery() 中完成工作,如果成功完成,则会发送 ack。 Or rather, should I use 1 consumer and manage a pool of workers at my own layer of the application?或者更确切地说,我应该使用 1 个消费者并在我自己的应用程序层管理一个工作人员池吗?

  2. Consumers will be talking to the database, which means transient errors will occur;消费者将与数据库交谈,这意味着会发生暂时性错误; deadlocks, optimistic locking violations, database server restarts, and so on.死锁、乐观锁违规、数据库服务器重启等。 What is the intended behavior if a consumer can't process a job?如果消费者无法处理工作,预期行为是什么? Issue a nack and wait for the broker to resend the job, or delay the ack until it's certain that the job can't be completed?发出 nack 并等待 broker 重新发送作业,或者延迟 ack 直到确定作业无法完成? Or does it not matter?或者没关系?

It's worth noting that jobs generally won't take longer than a few seconds unless the database is restarting or something.值得注意的是,除非数据库正在重新启动或其他原因,否则作业通常不会超过几秒钟。 I appreciate any guidance!我感谢任何指导!

1a. 1a. If you want a fixed number of working threads then you should open several channels and register one consumer per channel.如果您想要固定数量的工作线程,那么您应该打开多个通道并为每个通道注册一个使用者。 RabbitMQ has a single dispatcher thread per channel so that one consumer will block other consumers registered on the same channel. RabbitMQ 每个通道有一个调度程序线程,因此一个消费者将阻止在同一通道上注册的其他消费者。

1b. 1b. If you need a dynamically growing pool of workers then you might consider using spring-rabbit (assuming you implement in Java).如果您需要一个动态增长的工作池,那么您可以考虑使用spring-rabbit (假设您使用 Java 实现)。 Specifically, SimpleMessageListenerContainer manages a pool of consumers growing and shrinking dynamically according to the work load.具体来说, SimpleMessageListenerContainer管理根据工作负载动态增长和收缩的消费者池。 Refer to section "Listeners Concurrency" in Spring AMQP reference guide.请参阅 Spring AMQP 参考指南中的“侦听器并发”部分。

2a. 2a. If you know the worker has failed you can nack and the message will be delivered to another consumer.如果您知道工作人员失败了,您可以确认消息将被传递给另一个消费者。 The message will not be delivered to another consumer until the consumer acknowledged it unless it's "auto ack" mode.除非它是“自动确认”模式,否则在消费者确认消息之前不会将消息传递给另一个消费者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用RabbitMQ java客户端库处理从错误连接中恢复? - How do you handle recovering from a faulty connection using RabbitMQ java client library? java-使用set时如何避免并发和其他错误-hashmap - java- how to avoid concurrency and other errors when using set - hashmap 如何处理java servlet中的并发性 - how to handle concurrency in java servlets Java与数组列表的并发(如何处理?) - Java concurrency with arraylists (How to handle this?) 如何使用Rabbitmq Java客户端从队列中检索Unack消息 - how to retrieve unack message from queue using rabbitmq java client 如何使用Spring Integration Java DSL将消息发送到Rabbitmq队列 - How to send message to the rabbitmq queue using spring integration java DSL 使用链表进行排队-这应该如何工作? - Queue using linked lists - how should this work? 使用java停止队列侦听RabbitMQ - Stop queue listening RabbitMQ using java 您如何处理/记录Java MVC Web应用程序中的错误? - How do you handle/log errors in a Java MVC web application? 我应该在这里使用 java synchronized 来处理并发吗? - Should I use java synchronized to handle concurrency here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM