关于动态 Apache 骆驼路线/上下文的设计问题

发布于 2024-11-15 09:40:01 字数 884 浏览 3 评论 0原文

我们有 ActiveMQ,系统中发生的事件都会发布到该 ActiveMQ 上。该项目涉及用户将实体添加到他们的监视列表中,每当这些实体上有事件时,我希望向感兴趣的参与者发送一封电子邮件。

该用例大致可以翻译为某人对目录上的产品信息页面表示兴趣,并且每当该产品发生任何活动(价格下降、有正面评价等)时就会发送一封电子邮件。我将这种交互建模为骆驼路线。

因此,例如,如果用户说每当该产品的评级等于 5 时给我发送电子邮件,则以下路由将添加到骆驼上下文中:

from("activemq:topic:events.product.save").filter().xpath("/object[<object id>]/rating").isEqualTo("5").to("email:<user's email>")

同样,如果用户希望在产品有新评论时收到通知,则另一条路由将被创建等等。当每个用户开始添加他们感兴趣的手表时,这可能最终会创建数千条路线。

我的一些问题是:

  • 这是创建动态路由的可接受方式吗?我正在考虑的一种选择是使用收件人列表。但我一直无法想出一个解决方案,能够优雅地将消息路由到返回收件人列表的 bean。例如,对于上面解释的情况,bean 是否会有一堆 if-else 来查看要返回哪个收件人列表?

  • camelcontext有一个从xml文件加载路由的方法,但没有方法来保存现有的路由。保存这些动态创建的路由的最简单(且有效)的方法是什么? camel-users 列表中的这个线程总结了我的要求。

We have ActiveMQ onto which the events that happen in the system are published. The project involves users adding entities to their watch-list and whenever there are events on those entities I would like an email to be sent out to the interested participants.

The use-case roughly translates to some one expressing an interest in a product information page on the catalog and an email being sent whenever any activity happens on that product (price goes down, there is a positive review etc.,). I had modelled this interaction as a Camel route.

So, for example, if the user says email me whenever this product's rating equals 5, then the following route would be added to the camel context:

from("activemq:topic:events.product.save").filter().xpath("/object[<object id>]/rating").isEqualTo("5").to("email:<user's email>")

Similarly if the user wants to be notified whenever there is a new comment on a product, another route would be created and so on. This could potentially, end up creating thousands of routes as each user starts adding their watches of interest.

Some questions that I have are:

  • Is this an acceptable way of creating dynamic routes? One option I am considering is to use recipient lists. But I haven't been able to come up with a solution that would make it elegant to route messages to the bean that would return the recipient list. For example for the case explained above would the bean have a bunch of if-else to see which recipient list to return?

  • The camelcontext has a method to load routes from a xml file but no method to persist the existing routes. What would be simplest (and efficient) way to persist these dynamically created routes? This thread in the camel-users list sums up my request.

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

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

发布评论

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

评论(1

嘿咻 2024-11-22 09:40:01

鉴于订阅需求的动态性质,您应该使用数据库来存储信息,而不是尝试创建动态路由。这是一种更具可扩展性/适当的技术使用...

那么您只需要一个静态路由或 POJO 使用者(见下文),可以使用简单的 POJO bean 处理产品更新消息 (bean-binding 可以提供帮助,等等)。然后 POJO bean 将负责查询数据库以查找所有“感兴趣”的用户并使用 camel- 发送电子邮件邮件

public class NotificationBean {

    @Consume(uri="activemq:topic:events.product.save")
    public void onUpdate(@XPath("/object/id") String id, 
                         @XPath("/object/rating") String rating) {
        //query database for subscriptions for this product ID/rating, etc.
        //for each interested subscriber
            //send email (camel-mail, etc)
    }

    public void addSubscription(String productID, Integer rating, String email) {
        //create/update subscription entry in database, etc...
    }
}

Given the dynamic nature of your subscription requirements, you should use a database to store the information rather than trying to create dynamic routes. This is a much more scalable/appropriate use of technology...

Then you can only need a single static route or a POJO consumer (see below) that can process the product update messages using a simple POJO bean (bean-binding can help, etc). The POJO bean would then be responsible for querying the database to find all "interested" users and send an email using camel-mail

public class NotificationBean {

    @Consume(uri="activemq:topic:events.product.save")
    public void onUpdate(@XPath("/object/id") String id, 
                         @XPath("/object/rating") String rating) {
        //query database for subscriptions for this product ID/rating, etc.
        //for each interested subscriber
            //send email (camel-mail, etc)
    }

    public void addSubscription(String productID, Integer rating, String email) {
        //create/update subscription entry in database, etc...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文