apache camel - 将消息警报添加到死信队列

apache camel - add message alert to deadletter queue

String queueA = "rabbitmq://host:5672/queue-a.exchange?queue=queue-a.exchange..etc

from(queueA)
  .routeId("idForQueueA")
  .onException(Exception.class)
    .maximumRedeliveries(0)
    // .processRef("sendEmailAlert")  * not sure this belongs here*
    .to(deadLetterQueueA)
    .useOriginalMessage()
  .end()
    .processRef("dataProcessing")
    .processRef("dataExporting")
  .end();

Explaining the code above:

消息取自queueA。在各种过程成功后,消息被消费。如果失败,则将其添加到死信队列 "deadLetterQueueA"。这一切正常。

我的问题是

When messages arrive in the deadletter queue I want to add alerts so we know to do something about it... How could I to add an email alert when a message arrives in the dead letter queue. I dont want to lose the original message if the alert fails - nor do I want the alert to consume the message.


我的想法是.. 我需要在出现异常时拆分消息以便将其发送到两个不同的队列?一个用于警报,然后发出电子邮件警报,然后自行消耗。然后一个用于死信队列,只是站点在那里?但是我不确定该怎么做?

您可以使用 multicast(详细信息 here)拆分一条消息以发送到多个端点:

.useOriginalMessage().multicast().to(deadLetterQueueA, "smtp://username@host:port?options")

这使用 here 中描述的 camel 邮件组件端点。或者,您可以在 to 之后继续处理消息。所以像:

.useOriginalMessage()
.to(deadLetterQueueA)
.transform().simple("Hi <name>, there has been an error on the object ${body.toString}")
.to("smtp://username@host:port?options")

如果您有多个收件人,您可以使用 recipients list

public class EmailListBean {
    @RecipientList
    public String[] emails() {
        return new String[] {"smtp://joe@host:port?options",
                "smtp://fred@host:port?options"};
    }
}

.useOriginalMessage()
.to(deadLetterQueueA)
.transform().simple("...")
.bean(EmailListBean.class)

在等待人工处理消息时,请小心使用 JMS 队列来存储消息。我不知道你得到什么样的消息流量。我假设如果您想为每次失败发送一封电子邮件,那不是很多。但我通常会对这种事情保持警惕,并选择使用日志记录或数据库持久化来存储错误结果,并且只使用 JMS 错误队列来通知其他进程或消费者错误或安排重试.

您可以通过两种方式执行此操作,但根据您的消息量,您可能不希望在每条失败的消息上都发送电子邮件。

您可以使用 AndyN 提供的解决方案,或者您可以使用 Advisory Topics ActiveMQ.Advisory.MessageDLQd.Queue.* ,每当有消息进入 DLQ 时,该主题的入队计数将增加 1 。通过监控队列深度,您现在可以根据发生的错误数量发送邮件。

如果你想在生产者端做。您可以使用 AndyN

提供的任何一种解决方案