Apache camel在路由

时间:2016-01-05 12:04:58

标签: java apache-camel

代码:

  from("file://files")
  .split()
  .tokenize("\n")
  .split()
  .method(SplitToken.class, "hashTokens")
  .process(new Processor() {
    public void process(Exchange exchange) throws Exception {
    Set<Map.Entry<String, Integer>> oldSet = (Set<Map.Entry<String, Integer>>) exchange.getIn().getBody(Set.class);
  StringBuilder fin = new StringBuilder();
  for (Map.Entry<String, Integer> entry : oldSet) {
fin.append(entry.getValue() + " " + entry.getKey() + "\n");
  }
  exchange.getIn().setBody(fin.toString());
  }
  })
  .to("file://files/Merged.txt");
  }

public class SplitToken {
@SuppressWarnings("unchecked")
public static Set<Map.Entry<String, Integer>> hashTokens(final Exchange exchange, @Body List<String> body) {
    Pattern p = Pattern.compile("(\\w+)\\s(\\d+)");
    HashMap<String, Integer> exrate = new HashMap<String, Integer>();
    for (String line : body) {
        Matcher m = p.matcher(line);
        if (m.find())
            exrate.put(m.group(1), Integer.parseInt(m.group(2)));
    }
    return exrate.entrySet();
}

}

输入数据包含多个具有良好名称的文件及其各自的价格标签。

但它只会返回很多错误:

[org.apache.camel.impl.converter.AnnotationTypeConverterLoader] : Ignoring converter type: org.apache.camel.component.mina.MinaConverter as a dependent class could not be found: java.lang.NoClassDefFoundError: org/apache/mina/common/ByteBuffer
java.lang.NoClassDefFoundError: org/apache/mina/common/ByteBuffer

即使我已经在我的pom.xml

中导入了mina,它也会返回此错误

[org.apache.camel.processor.DefaultErrorHandler] : Redelivery enabled: false on error handler: DefaultErrorHandler[Channel[Splitter[on: BeanExpression[ method: hashTokens] to: Pipeline[[Channel[DelegateSync[main$1$1@5bd7d30f]], Channel[sendTo(Endpoint[file://files/Merged.txt])]]] aggregate: null]]]
]
]


Message History
-----------------------------------------------------------------------------    ----------------------------------------------------------
RouteId              ProcessorId            Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [file://files                                                               ] [        45]
[route1            ] [split1            ] [split[bean{SplitToken, method=hashTokens}]                                    ] [        19]

Exchange
-----------------------------------------------------------------------------    ----------------------------------------------------------
Exchange[
Id                  ID-ALI-PC-8060-1451994155551-0-4
ExchangePattern     InOnly
Headers             {breadcrumbId=ID-ALI-PC-8060-1451994155551-0-1, CamelFileAbsolute=false, CamelFileAbsolutePath=C:\Users\ali25\Documents\IdeaProjects\files\1.txt, CamelFileContentType=text/plain, CamelFileLastModified=1451993202700, CamelFileLength=111, CamelFileName=1.txt, CamelFileNameConsumed=1.txt, CamelFileNameOnly=1.txt, CamelFileParent=files, CamelFilePath=files\1.txt, CamelFileRelativePath=1.txt, CamelRedelivered=false, CamelRedeliveryCounter=0}
BodyType            String
Body                Good1 450
]

Stacktrace
-----------------------------------------------------------------------------    ----------------------------------------------------------
]
at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1658)

[org.apache.camel.processor.DefaultErrorHandler] : Failed delivery for (MessageId: ID-ALI-PC-8060-1451994155551-0-5 on ExchangeId: ID-ALI-PC-8060-1451994155551-0-6). On delivery attempt: 0 caught: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[][Message: Good1 450]
[org.apache.camel.processor.DefaultErrorHandler] : Failed delivery for (MessageId: ID-ALI-PC-8060-1451994155551-0-5 on ExchangeId: ID-ALI-PC-8060-1451994155551-0-6). Exhausted after delivery attempt: 1 caught: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[][Message: Good1 450]

错误只是不断重复。 我不知道为什么会发生这个错误,因为我绝对确定一个集是可迭代的,还是它?

编辑: pom.xml的内容:

<dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>apache-camel</artifactId>
        <version>2.16.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.16.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-stream</artifactId>
        <version>2.16.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.6</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-mina2</artifactId>
        <version>2.16.1</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

我认为问题出在SplitToken课程中。 在第一个拆分器之后,您将获得一个String(文件的一行),而不是一个字符串列表。在这里你收到异常,然后Camel试图将String转换为List<String>,因为你有@Body List<String> body

您不需要任何Mina依赖...