我是否正在尝试用换行符拆分邮件,并使用以下脚本:
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();但是当我使用它时,我在我的日志文件中得到了以下错误:
groovy.lang.MissingMethodException: No signature of method: [B.split() is applicable for argument types: (java.lang.String) values: {"\n"} (javax.script.ScriptException)当我将拆分线更改为以下内容时:
def lines = message.getPayload().toString().split("\n");我得到一个错误,数组是OutOfBound,所以看起来它仍然没有对换行符做任何事情。
传入的消息(message.getPayload)是来自文件系统的消息,并且包含换行符。它看起来是这样的:
ABCDFERGDSFF
123456SDFDSFDSFDSF
JGHJGHFHFH我做错了什么?使用Mule 2.X收集消息
发布于 2013-09-19 22:59:26
看起来像是message.payload返回了一个byte[],您需要它返回到一个字符串中:
def lines = new String( message.payload, 'UTF-8' ).split( '\n' )应该得到它:-)
另外,我倾向于这样写东西:
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 :
''
}
}而不是使用范围字符串访问的大量if...else块(例如:如果您获得的行只有3个字符,或者在有效负载中只有1行,那么您的代码块将失败)
使用Groovy 1.5.6,您会遇到以下问题:
def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ].with { lines ->并祈祷它在有效负载中至少有2行
否则,您将需要引入一个方法来从数组中获取最多2个元素
你能不能试试:
可能是with在1.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'发布于 2018-03-21 23:37:40
def lines = "${message.getPayload()}".split('\n');这种方法也应该起作用。
https://stackoverflow.com/questions/18898130
复制相似问题