关于动态 Apache 骆驼路线/上下文的设计问题 [英] Design question on dynamic Apache camel routes/context
问题描述
我们有 ActiveMQ,系统中发生的事件会发布到它上面.该项目涉及用户将实体添加到他们的监视列表中,每当这些实体上有事件时,我都希望向感兴趣的参与者发送电子邮件.
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.
用例大致可以理解为某人对目录中的产品信息页面表示感兴趣,并且每当该产品发生任何活动(价格下降、好评等)时,都会发送一封电子邮件.我将这种交互建模为 Camel 路线.
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.
因此,例如,如果用户在此产品的评分等于 5 时说给我发电子邮件,那么以下路由将添加到骆驼上下文中:
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.
我的一些问题是:
这是一种可以接受的创建动态路由的方式吗?我正在考虑的一种选择是使用收件人列表.但是我还没有想出一个解决方案,它可以优雅地将消息路由到将返回收件人列表的 bean.例如对于上面解释的情况,bean 是否会有一堆 if-else 来查看要返回的收件人列表?
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?
camelcontext 有一种从 xml 文件加载路由的方法,但没有方法来持久化现有的路由.持久化这些动态创建的路由的最简单(且有效)的方法是什么?camel-users list 中的这个主题总结了我的要求.
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.
推荐答案
鉴于订阅要求的动态特性,您应该使用数据库来存储信息,而不是尝试创建动态路由.这是一种更具可扩展性/更合适的技术使用......
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...
那么你就可以只需要一个静态路由或者一个POJO消费者(见下文) 可以使用简单的 POJO bean 处理产品更新消息(bean-binding 可以提供帮助, 等等).然后 POJO bean 将负责查询数据库以找到所有感兴趣"的用户并使用 camel- 发送电子邮件邮件
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...
}
}
这篇关于关于动态 Apache 骆驼路线/上下文的设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!