我是否尝试在换行符上拆分消息并使用以下脚本:
def mesType = "";
def lines = message.getPayload().split("\n");
if ( lines[0][0..6] == '123456' || lines[1][0..6] == '123456') {
mesType = "MES1";
}
if ( lines[0][0..7] == '654321' || lines[1][0..7] == '654321') {
mesType = "MES2";
}
if ( lines[0][0..7] == '234561' || lines[1][0..7] == '234561') {
mesType = "MES3";
}
message.setProperty('mesType', mesType);
return message.getPayload();
Run Code Online (Sandbox Code Playgroud)
但是当我使用它时,我的日志文件中出现以下错误:
groovy.lang.MissingMethodException: No signature of method: [B.split() is applicable for argument types: (java.lang.String) values: {"\n"} (javax.script.ScriptException)
Run Code Online (Sandbox Code Playgroud)
当我将分割线更改为以下内容时:
def lines = message.getPayload().toString().split("\n");
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,该数组是OutOfBound,所以看起来它仍然没有在换行符上做任何事情.
(message.getPayload)中的消息是来自文件系统的消息,并且包含换行符.它看起来像这样:
ABCDFERGDSFF
123456SDFDSFDSFDSF
JGHJGHFHFH
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?使用Mule 2.X收集消息
看起来像message.payload返回一个byte[]你需要回到一个字符串:
def lines = new String( message.payload, 'UTF-8' ).split( '\n' )
Run Code Online (Sandbox Code Playgroud)
应该得到它:-)
另外,我更喜欢写这样的东西:
def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' ).take( 2 ).with { lines ->
switch( lines ) {
case { it.any { line -> line.startsWith( '123456' ) } } : 'MES1' ; break
case { it.any { line -> line.startsWith( '654321' ) } } : 'MES2' ; break
case { it.any { line -> line.startsWith( '234561' ) } } : 'MES3' ; break
default :
''
}
}
Run Code Online (Sandbox Code Playgroud)
而不是大量if...else具有远程字符串访问权限的块(即:如果您获得的行只有3个字符,或者有效负载中只有1行,则会失败)
使用Groovy 1.5.6,你会遇到:
def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ].with { lines ->
Run Code Online (Sandbox Code Playgroud)
并保持你的手指交叉它有效载荷至少有2行
或者您将需要引入一种方法来从数组中获取最多2个元素
这可能是with1.5.6中的突破(不确定)...尝试将其展开回原来的状态:
def lines = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ]
def mesType = 'empty'
if( lines.any { line -> line.startsWith( '123456' ) } ) mesType = 'MES1'
else if( lines.any { line -> line.startsWith( '654321' ) } ) mesType = 'MES2'
else if( lines.any { line -> line.startsWith( '234561' ) } ) mesType = 'MES3'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12522 次 |
| 最近记录: |