【问题标题】:Can Multiple Requests overwrite Flow Variables currently in use?多个请求可以覆盖当前使用的流变量吗?
【发布时间】:2017-11-02 18:18:35
【问题描述】:

我无法理解我是否在我的流程上下文中使用了正确的变量。

我有一个通过 HTTP 端点公开的流,在这个流中我存储有关传入请求的数据,然后继续使用包含流/子流中数据的流变量。

我的问题是,如果我有 2 个请求同时进入,它们基本上会覆盖流变量,导致流链稍后出现问题吗?

我想避免请求 1 设置流变量,然后处理数据并继续引用已被请求 2 中的数据覆盖的流变量。

这是如何工作的?流中的每个条目是否相互独立?

【问题讨论】:

  • 问题标题具有误导性..您将如何区分 request1 和 request2?他们都是同一个请求吗?
  • 它们将通过单个端点进入,请求的内容无关紧要,假设它们都是同时到来的有效请求,它们会覆盖使用的流变量吗在模块化流程设计中?
  • 我问的原因是它们是否可以区分,您可以轻松过滤...内容不必不同,其他参数如标题或其他参数也可以区分它们

标签: mule mule-studio


【解决方案1】:

如果我有 2 个请求同时进来,他们基本上会是 覆盖流变量导致流链稍后出现问题

。变量存储在消息中。对于每个 HTTP 请求,都会创建一条新消息。

您可以使用以下应用轻松地自己尝试一下:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
    <flow name="foo">
        <http:listener config-ref="user-httpListenerConfig" path="/{test}" doc:name="HTTP"/>
        <set-variable variableName="testVariable" value="#[message.inboundProperties['http.uri.params']['test']]" doc:name="testVariable"/>
        <foreach collection="#[[1,2,3,4,5,6,7,8,9,0]]" doc:name="For Each">
            <logger message="#[flowVars.testVariable]" level="INFO" doc:name="Logger"/>
            <scripting:component doc:name="Groovy">
                <scripting:script engine="Groovy">
                <![CDATA[Thread.sleep(1000)
return payload]]></scripting:script>
            </scripting:component>
        </foreach>
    </flow>
</mule>

这是此应用程序在 AnypointStudio 中的外观:

只需在浏览器的一个标签中调用http://localhost:8081/foo,在另一个标签中调用http://localhost:8081/bar。您会在日志中看到foobar 交替打印。

【讨论】:

    【解决方案2】:

    流变量是实例变量,每次都会为新请求创建。因此,多个请求不能覆盖流变量,因为每个请求都有自己的实例变量。

    【讨论】:

      【解决方案3】:

      “我的问题是,如果我有 2 个请求同时进入,它们基本上会覆盖流变量,导致流链稍后出现问题吗?”
      是的,每个正确的,每个新请求都会创建新的流实例,并且每个新请求都会初始化变量。
      每个请求都是相互独立的,除非您使用对象存储之类的某种存储来保留请求离开位置的流变量值。
      使用像对象存储这样的内存存储,您可以为每个新请求保留变量值,否则每个新请求都会在流程中创建新的变量实例

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-22
        • 1970-01-01
        • 1970-01-01
        • 2016-08-06
        • 1970-01-01
        • 1970-01-01
        • 2019-03-02
        • 2021-01-06
        相关资源
        最近更新 更多