如何使用 groovy 将 EDIFACT 消息(包含多个特殊字符和多行)放入变量中?

How can I put an an EDIFACT message (containing multiple special characters and multiple lines) in a variable using groovy?

我有一条 EDIFACT 消息,看起来像这样:

AAA:+.? '
ABC+ABCD:1+ABCDEFG:ZZ+ABCDEF:ZZ+123456:2042+12345678901++ABCD'
DEF+ABCD+LH+FVKJUB+20000:2042+Y1234567+UN+D:21B'
GHI+1+ABCD:D:11A:AA:ABCD+ABCD12345678901123ABC123456'
JKL+745'
HHH+TN:IIAA891011213531235BNM422244:::001'
CCC+NT+++ABCDEFGHIJKLMNOPQRS'
STU+00123456789012:UF+0000000000:GY'
VXY+50+MI1234+++MI'
AAA+235+ABC'
BBB+200:3202062000:301'
FFF+90+USA'
BBB+232:2101051135:201'
CCC+FF+++AaBaBa001:TEST1'
DDD+3++G'
EEE+329:711013'
FFF+178+XXX'
FFF+179+YYY'
GGG+2+ZZZ'
HHH+BXG:ABCDEF'
HHH+ABC:12AB3E01234E8UD8'
III+P:110:111+100000001'
EEE+36:281105'
FFF+91+ASD'
VVV+50:2'
XXX+0011+1'
YYY+1+U0123456'
ZZZ+1+U1234560002'

使用 Jmeter JSR223 采样器,我需要将它作为负载发送到 IBM MQ 消息队列。

我试图将此消息放入一个变量中,以便我可以传递它,但由于它包含的所有特殊字符,我遇到了错误。

def sessInboundQueue = System.getProperties().get("SessionInbound")
def destinationInboundQueue = System.getProperties().get("DestinationInbound")
def producer = sessInboundQueue.createProducer(destinationInboundQueue) 

def payload = "AAA:+.? '
ABC+ABCD:1+ABCDEFG:ZZ+ABCDEF:ZZ+123456:2042+12345678901++ABCD'
DEF+ABCD+LH+FVKJUB+20000:2042+Y1234567+UN+D:21B'
GHI+1+ABCD:D:11A:AA:ABCD+ABCD12345678901123ABC123456'
JKL+745'
HHH+TN:IIAA891011213531235BNM422244:::001'
CCC+NT+++ABCDEFGHIJKLMNOPQRS'
STU+00123456789012:UF+0000000000:GY'
VXY+50+MI1234+++MI'
AAA+235+ABC'
BBB+200:3202062000:301'
FFF+90+USA'
BBB+232:2101051135:201'
CCC+FF+++AaBaBa001:TEST1'
DDD+3++G'
EEE+329:711013'
FFF+178+XXX'
FFF+179+YYY'
GGG+2+ZZZ'
HHH+BXG:ABCDEF'
HHH+ABC:12AB3E01234E8UD8'
III+P:110:111+100000001'
EEE+36:281105'
FFF+91+ASD'
VVV+50:2'
XXX+0011+1'
YYY+1+U0123456'
ZZZ+1+U1234560002'"
def msg = sessInboundQueue.createTextMessage(payload)

这是错误:

    Script87.groovy: 9: Unexpected input: '"' @ line 9, column 15.
   def payload = "AAA:+.? '

groovy 中的多行字符串必须用三重引号引起来:

GString(支持插值)

def payload = """AAA:+.? '
ABC+ABCD:1+ABCDEFG:ZZ+ABCDEF:ZZ+123456:2042+12345678901++ABCD'
DEF+ABCD+LH+FVKJUB+20000:2042+Y1234567+UN+D:21B'
...
"""

字符串(纯 java 字符串)

def payload = '''AAA:+.? '
ABC+ABCD:1+ABCDEFG:ZZ+ABCDEF:ZZ+123456:2042+12345678901++ABCD'
DEF+ABCD+LH+FVKJUB+20000:2042+Y1234567+UN+D:21B'
...
'''

你需要escape special characters喜欢引号、换行等

类似于:

def payload = "AAA:+.? '\n" +
        "ABC+ABCD:1+ABCDEFG:ZZ+ABCDEF:ZZ+123456:2042+12345678901++ABCD'\n" +
        "DEF+ABCD+LH+FVKJUB+20000:2042+Y1234567+UN+D:21B'\n" +
        "GHI+1+ABCD:D:11A:AA:ABCD+ABCD12345678901123ABC123456'\n" +
        "JKL+745'\n" +
        "HHH+TN:IIAA891011213531235BNM422244:::001'\n" +
        "CCC+NT+++ABCDEFGHIJKLMNOPQRS'\n" +
        "STU+00123456789012:UF+0000000000:GY'\n" +
        "VXY+50+MI1234+++MI'\n" +
        "AAA+235+ABC'\n" +
        "BBB+200:3202062000:301'\n" +
        "FFF+90+USA'\n" +
        "BBB+232:2101051135:201'\n" +
        "CCC+FF+++AaBaBa001:TEST1'\n" +
        "DDD+3++G'\n" +
        "EEE+329:711013'\n" +
        "FFF+178+XXX'\n" +
        "FFF+179+YYY'\n" +
        "GGG+2+ZZZ'\n" +
        "HHH+BXG:ABCDEF'\n" +
        "HHH+ABC:12AB3E01234E8UD8'\n" +
        "III+P:110:111+100000001'\n" +
        "EEE+36:281105'\n" +
        "FFF+91+ASD'\n" +
        "VVV+50:2'\n" +
        "XXX+0011+1'\n" +
        "YYY+1+U0123456'\n" +
        "ZZZ+1+U1234560002'\n"

应该能帮到你。

您可能还对IBM MQ testing with JMeter - Learn How文章感兴趣