3

REST url パターンに基づいてメッセージをさまざまなサービスにルーティングしたいのですが、myRouteBuilder クラスのルート定義は次のとおりです。

String module1RestUrls = "/client,/price,/client/add,/client/hello";
String module2RestUrls = "/order,/order/invoice,/suppliers";

from("jetty:http://192.168.1.1?matchOnUriPrefix=true&traceEnabled=true")
   .process(customProcessor)
     .choice()
       .when(simple("${module1RestUrls} contains ${header('CamelHttpUri')}"))
           .to("http4://" + module1Url + "?bridgeEndpoint=true")
       .when(simple("${module2RestUrls} contains ${header('CamelHttpUri')}"))
           .to("http4://" + module2Url + "?bridgeEndpoint=true")
       .otherwise()
           .to("http4://" + genericUrl + "?bridgeEndpoint=true");

module1RestUrls と module2RestUrls は現在ハード コードされていますが、後でプロパティ ファイルから読み込むことができます。私の問題は、私が得続けることです

Caused by: org.apache.camel.language.simple.types.SimpleIllegalSyntaxException: Unknown   
function: module1RestUrls at location 0
${module1RestUrls} contains ${header('CamelHttpUri')}
*

誰かがそれを助けることができれば、それは素晴らしいことです。私はググってさまざまな方法を試しましたが、何も機能していないようです。

フルトラックトレース --------------

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: [Body is null]]
at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1358)
at org.apache.camel.builder.SimpleBuilder.createPredicate(SimpleBuilder.java:96)
at org.apache.camel.builder.SimpleBuilder.matches(SimpleBuilder.java:74)
at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:66)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.processor.interceptor.BacklogTracerInterceptor.process(BacklogTracerInterceptor.java:84)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:91)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:391)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:273)
at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:46)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.processor.interceptor.DefaultChannel.process(DefaultChannel.java:335)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:117)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:46)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.processor.UnitOfWorkProcessor.processAsync(UnitOfWorkProcessor.java:150)
at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:117)
at org.apache.camel.processor.RouteInflightRepositoryProcessor.processNext(RouteInflightRepositoryProcessor.java:48)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:139)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:652)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1329)
at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:117)
at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:44)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1300)
at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:117)
at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:44)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1300)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:445)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1038)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:374)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:972)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:359)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:920)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:982)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.camel.language.simple.types.SimpleIllegalSyntaxException: Unknown     function: module1RestUrls at location 0
 ${module1RestUrls} contains ${header('CamelHttpUri')}
 *
at org.apache.camel.language.simple.SimplePredicateParser.parsePredicate(SimplePredicateParser.java:69)
at org.apache.camel.language.simple.SimpleLanguage.createPredicate(SimpleLanguage.java:137)
at org.apache.camel.builder.SimpleBuilder.createPredicate(SimpleBuilder.java:94)
... 61 more
Caused by: org.apache.camel.language.simple.types.SimpleParserException: Unknown     function: module1RestUrls
at org.apache.camel.language.simple.ast.SimpleFunctionExpression.createSimpleExpression(SimpleFunctionExpression.java:187)
at org.apache.camel.language.simple.ast.SimpleFunctionExpression.createExpression(SimpleFunctionExpression.java:40)
at org.apache.camel.language.simple.ast.SimpleFunctionStart.doCreateLiteralExpression(SimpleFunctionStart.java:58)
at org.apache.camel.language.simple.ast.SimpleFunctionStart.createExpression(SimpleFunctionStart.java:48)
at org.apache.camel.language.simple.ast.BinaryExpression.createExpression(BinaryExpression.java:78)
at org.apache.camel.language.simple.SimplePredicateParser.createPredicates(SimplePredicateParser.java:437)
at org.apache.camel.language.simple.SimplePredicateParser.doParsePredicate(SimplePredicateParser.java:118)
at org.apache.camel.language.simple.SimplePredicateParser.parsePredicate(SimplePredicateParser.java:66)
... 63 more
4

3 に答える 3

3

U2one に感謝します。次のように実行しました。

クラス MyRouteBuilder extends RouteBuilder に以下を追加しました。

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

customProcessor を次のように記述しました。

public void process(Exchange exchange) throws Exception {       
    String[] first1Url = messageSource.getMessage("first1Url", null, null).split(",");
    String[] second1Url= messageSource.getMessage("second1Url", null, null).split(",");

    exchange.getIn().setHeader("firstUrn ", first1Url );
    exchange.getIn().setHeader("secondUrn ", second1Url);

    Map<String, Object> headers = exchange.getIn().getHeaders();     

    exchange.getOut().setHeaders(headers);        
    exchange.getOut().setBody(exchange.getIn().getBody(String.class), String.class);
}

これにより、すべてが適切にルーティングされます。次のように、残りの URL をメッセージ プロパティ ファイルに保存します。

  firstUrn =/client,/client/add,/order,/resource,/others
  secondUrn=/hello,/purchase,/nothing

現在は解決されており、AWS Beanstalk と camel に関する他の問題が調査されています。

--- ただの更新です。より良い解決策は、ラルフによって提案された recipientList パターンを使用することです。

拡張されたRouteBuilderでは、

from("servlet:///?matchOnUriPrefix=true") 
    .process(customPreProcessor) 
        .recipientList(header("urlNew"))
                .process(customPostProcessor);

CustomPreProcessorには、次のものがあります。

public void process(Exchange exchange) throws Exception {

    String CamelHttpUri = (String) exchange.getIn().getHeader("CamelHttpUri");
    String CamelHttpQuery = (String) exchange.getIn().getHeader("CamelHttpQuery");
    String urlReceived = CamelHttpUri;
    StringBuilder urlToMatch = new StringBuilder();
    urlToMatch.append(urlReceived);

    if (!StringUtils.isEmpty(CamelHttpQuery)) {
        urlToMatch.append("?").append(CamelHttpQuery);
    }
    String endpointUrl = getEndPointUrl(serviceUrl, restUrl, urlToMatch.toString());
    }

    if (StringUtils.isEmpty(endpointUrl)) {
        endpointUrl = "http4://" + dummyUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false";
    }

    exchange.getIn().setHeader("CamelHttpUri", urlReceived);
    exchange.getIn().setHeader("CamelHttpQuery", CamelHttpQuery);
    exchange.getIn().setHeader("restUrl", endpointUrl);

    exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    exchange.getOut().setBody(exchange.getIn().getBody(String.class), String.class);
}

private String getEndPointUrl(String serviceUrls, String restUrl, String urlToMatch) {
    List<String> serviceUrlList = Arrays.asList(serviceUrls.split("\\s*,\\s*"));
    for (String urlPattern: serviceUrlList ) {
        Pattern pattern = Pattern.compile(urlPattern);
        Matcher matcher = pattern.matcher(urlToMatch);
        if (matcher.matches()) {
            return "http4://" + restUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false";
        }
    }
    return "";
}

CusomPostProcessor では、結果をクライアントに送信する前に任意のロジックを追加できます....

于 2013-09-20T00:54:48.610 に答える
1

スタックによると、 ${module1RestUrls} をローカル変数に置き換えることはできないようです。次のように表現しようとしましたか。

header('CamelHttpUri').in("/client","/price","/client/add","/client/hello")

これらの変数を外部化する必要がある場合は、プロパティ値から文字列配列を作成し、in(...) 内で使用できます。

String[] values = new String[]{"/client","/price","/client/add","/client/hello"};
...
header('CamelHttpUri').in(myStringArray)

たとえば、Spring を使用して String 内にプロパティ値を挿入し、それを REGEX で解析して String 配列を生成できます。

于 2013-09-09T14:35:29.417 に答える