我无法在JMeter的JSR223脚本中使用Java流,
我尝试过Java stream
List<String> myList =
Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
例外
2019-04-22 13:44:49,808 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script9.groovy: 6: unexpected token: -> @ line 6, column 15.
.filter(s -> s.startsWith("c"))
^
1 error
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script9.groovy: 6: unexpected token: -> @ line 6, column 15.
.filter(s -> s.startsWith("c"))
^
1 error
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:187) ~[groovy-all-2.4.16.jar:2.4.16]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]
我尝试过groovy stream
def sample = ['Groovy', 'Gradle', 'Grails', 'Spock'] as String[] def result = sample.stream() // Use stream() on array objects .filter { s -> s.startsWith('Gr') } .map { s -> s.toUpperCase() } .toList() // toList() added to Stream by Groovy
例外:
019-04-22 13:42:40,345 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.String;.stream() is applicable for argument types: () values: []
Possible solutions: grep(), size(), sort(), sum(), grep(), head()
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.String;.stream() is applicable for argument types: () values: []
Possible solutions: grep(), size(), sort(), sum(), grep(), head()
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_191]
还有beanshell isn't supporting streams
beanshell仍不支持Lambda表达式
答案 0 :(得分:1)
Groovy不支持lambda
,但是您可以将Closure
替换为lambda,如下所示:
List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList.stream()
.filter{s -> s.startsWith("c")}
.map{s-> s.toUpperCase()}
.sorted()
.forEach{s->println s};
输出:
C1
C2
第二种情况给您带来了错误,因为该示例是一个数组,并且stream()
在数组上不可用。您可以通过Arrays.stream(array)
获得流。
import java.util.stream.Collectors;
def sample = ['Groovy', 'Gradle', 'Grails', 'Spock'] as String[]
def result = Arrays.stream(sample )
.filter { s -> s.startsWith('Gr') }
.map { s -> s.toUpperCase() }
.collect(Collectors.toList())