如何在JMeter中实现异步请求重试及事务控制器内指定请求重复执行?
Hey there! Let’s walk through exactly how to build both of your JMeter scenarios with clear, actionable steps.
This is a classic "polling" pattern for async operations—here's how to implement it:
Step 1: Set up the base structure
Start with aThread Group, then add anHTTP Requestelement configured to hit your async endpoint.Step 2: Add a response validator
Under theHTTP Request, add either a Response Assertion (for plain text/HTML) or JSON Assertion (for JSON responses). Configure it to check for your expected "correct" response content (e.g., a status field like"status": "completed").Step 3: Add a loop control variable
Add aJSR223 PostProcessorright after the assertion. Use this to set a variable that controls whether the loop continues:// Check if all assertions passed def allAssertionsPassed = prev.getAssertionResults().every { !it.isFailure() } // Set variable: "true" = keep looping, "false" = stop vars.put("keep_polling", allAssertionsPassed ? "false" : "true")Step 4: Wrap everything in a While Controller
Drag yourHTTP Request, assertion, and postprocessor into aWhile Controller. Set the controller's condition to${keep_polling}—this will repeat the request until the assertion passes (andkeep_pollingis set tofalse).Optional: Add a delay between polls
To avoid overwhelming your service, add aConstant Timerinside theWhile Controller(before theHTTP Request) with a reasonable delay (e.g., 1000ms for 1 second between requests).
For this scenario, we need to run 70 requests in a transaction, with one specific request repeating only if its response matches your criteria. Here's how:
Step 1: Create the Transaction Controller
Add aTransaction Controllerunder yourThread Group. If you want to include timer/pre-processor durations in the transaction metrics, check the box labeled "Include duration of timer and pre-post processors in generated sample".Step 2: Initialize a control variable
Add aJSR223 PreProcessordirectly under theTransaction Controllerto set up our loop control variable:// Start with "true" to run the target request at least once vars.put("repeat_target", "true")Step 3: Wrap the target request in a While Controller
Add aWhile Controllerinside theTransaction Controller, set its condition to${repeat_target}. Then add your targetHTTP Request(the one that needs conditional repetition) inside thisWhile Controller.Step 4: Add validation for the target request
Under the targetHTTP Request, add a Response Assertion or JSON Assertion to check for the text you want to match.Step 5: Update the loop variable based on validation
Add anotherJSR223 PostProcessorunder the target request to update therepeat_targetvariable:def responseMatches = prev.getAssertionResults().every { !it.isFailure() } // If response matches, keep repeating; if not, stop and move to next requests vars.put("repeat_target", responseMatches ? "true" : "false")Step 6: Add the remaining 69 requests
Place the other 69HTTP Requestelements directly under theTransaction Controller(at the same level as theWhile Controller). These will run immediately after theWhile Controllerstops (either when the response doesn't match, or if you add a max loop limit).Optional: Prevent infinite loops
To avoid repeating the target request forever, modify theWhile Controllercondition to include a max loop count. For example, to cap it at 10 repeats:${__jexl3(${repeat_target} == "true" && ${__counter(TRUE, loop_num)} <= 10,)}
Quick Tips
- Always use Groovy for JSR223 elements—it's the most performant option in JMeter.
- Use
View Results Treeduring debugging to verify that loops and assertions are behaving as expected. - For JSON responses, prefer
JSON Assertionover regex—it's more accurate and easier to maintain.
内容的提问来源于stack exchange,提问作者Bal Kishan

