gpt4 book ai didi

apache-camel - 如何提取 Apache Camel URI 参数

转载 作者:行者123 更新时间:2023-12-04 04:12:13 25 4
gpt4 key购买 nike

有谁知道如何从 Camel URI 中提取参数?
我有一个这样定义的路线

from("SOME_URI")
.to("SOME_URI")
.to("bean:myBean?method=myMethod&myParameter1=val1&myParameter2=val2")

我想像这样在“myMethod”中提取参数1和参数2(我在Grails中实现 Camel )
def myMethod(def inBody, Exchange exchange){
String parameter1 = extractParameter('myParameter1')
String parameter2 = extractParameter('myParameter2')

...//rest of code

return something
}

提前致谢!

最佳答案

主要答案

你可以从交易所得到你想要的东西:

exchange.getFromEndpoint()

将返回由“SOME_URI”定义的端点,并且:
exchange.getFromEndpoint().getEndpointUri()

将返回“SOME_URI”的字符串值

这意味着您的代码可能会变成:
def myMethod(def inBody, Exchange exchange){
def uri = exchange?.fromEndpoint?.endpointUri

if(uri) {
String parameter1 = extractParameter(uri, 'myParameter1')
String parameter2 = extractParameter(uri, 'myParameter2')

//...rest of code
}

return something
}

/*
* do any kind of processing you want here to manipulate the string
* and return the parameter. This code should work just fine in grails
*/
def extractParameter(String uri, String parameterName) {
def m = uri =~ "${parameterName}=([^&]+)"
return m.find() ? m[0][1] : null
}

如果首选 Java 等效项,则应执行相同的操作:
private static String extractParameter(String uri, String parameterName) {
Matcher m = Pattern.compile(parameterName + "=([^&]+)").matcher(uri);
return m.find() ? m.group(1) : null
}

选择

另请注意,根据您究竟要完成什么,更好的方法可能是使用 fromF DSL 直接向您的路线提供参数。这样,您就可以在代码中使用这些参数,而不必担心之后提取它们。

下面的代码片段取自 Camel Documentation of FromF .
fromF("file://%s?include=%s", path, pattern).toF("mock:%s", result);

关于apache-camel - 如何提取 Apache Camel URI 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13835288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com