计算Mule流处理的请求 [英] Count the requests processed by a Mule flow

查看:73
本文介绍了计算Mule流处理的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mule ESB 3.4.我想开发一个自定义Java组件,该组件计算一个流接收到多少请求.流程例如像这样:

I'm using Mule ESB 3.4. I want to develop a custom Java component that counts how many requests are received by a flow. The flow would e.g. be something like this:

http inbound-endpoint -> counter -> vm-outbound-endpoint

首先,包含计数器的实际变量应存储在内存中.然后可以将其打印到日志或插入数据库中(目前不重要).我所追求的是一个干净的设计,以后可以重新使用它来存储更​​复杂的应用程序全局状态(基于接收到的消息的数量和内容)并实现更复杂的逻辑.

First of all, the actual variable containing the counter should be stored in memory. Then it might be printed to the log or inserted into a DB (It's not important for now). What I'm after is a clean design that I can later re-use to store a more complex application-global state (based on the number and the content of received messages) and implement a more complex logic.

推荐答案

此问题的解决方案是将计数器变量创建为单例Spring bean(例如,类java.util.concurrent.atomic.AtomicInteger),然后注入再次通过Spring将其转换为自定义的Mule组件.该组件可以用prototype-scope定义,并且可以在每次Message遍历时安全地运行诸如counter.incrementAndGet()之类的东西.这样,计数器将在任何时候保存通过流的消息数量.

A solution to this problem is to create the counter variable as a singleton Spring bean ( e.g. of class java.util.concurrent.atomic.AtomicInteger ) and then inject it into a custom Mule component again via Spring. The component can be defined with prototype-scope, and can safely run something like counter.incrementAndGet() every time a Message traverses it. In that way the counter would -at any time- hold the number of messages that passed through the flow.

由于Spring bean可以是您喜欢的任何POJO,因此可以轻松扩展此设计以允许存储更复杂的状态.当然,存储的对象必须公开线程安全的操作.

Since a Spring bean can be any POJO you like, you can easily extend this design to allow a more complex state to be stored. The stored object has of course to expose thread-safe operations.

这篇关于计算Mule流处理的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆