Apache Came错误由于必须在以下方面指定ref:进程?

时间:2019-05-13 12:57:19

标签: exception apache-camel

我想做的是阅读邮件收件箱,并在“主题”行中包含“错误”字样的电子邮件中寻找邮件。

如果在主题行中发现没有包含“ ERROR”一词的电子邮件,则必须进入onException流程。

直到我想尝试处理,然后出现错误为止,这种工作都是可行的。

public class MailRouteBuilder extends RouteBuilder {

   @Autowired
   ExceptionMailProcessor exceptionMailProcessor;

   @Override
   public void configure() throws Exception {
      String mailRouteStr = "imap://host.......";

      List<String> tmpKeywords = new ArrayList<>();
      tmpKeywords.add("ERROR"):

      onException(MailRouteException.class, RuntimeException.class).log(LoggingLevel.ERROR, "EXCEPTION in MAIL")
                .process(exceptionMailProcessor)
                .end();

      from(mailRouteStr).routeId("mymail")
             .validate(header("subject").in(tmpKeywords))
             .log("Sending to Mail Queue")
             .to("{{jmsMailIn}}")
             .log("Done with Mail Queue")
             .end();
    }
.
.
.

我的ExceptionMailProcessor类。

@Component
public class ExceptionMailProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {

        String messageBody;
        Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);

    }
}

错误:

org.apache.camel.FailedToCreateRouteException: Failed to create route mymail at: >>> OnException[[class MailRouteException, class java.lang.RuntimeException] -> [Log[EXCEPTION in MAIL], process[Processor@0x0]]] <<< in route: Route(mymail)[[From[imap://mai... because of ref must be specified on: process[Processor@0x0]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1352)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:212)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1140)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3735)
    at org.apache.camel.impl.DefaultCamelContext.addRouteDefinitions(DefaultCamelContext.java:1072)
    at org.apache.camel.builder.RouteBuilder.populateRoutes(RouteBuilder.java:478)
    at org.apache.camel.builder.RouteBuilder.addRoutesToCamelContext(RouteBuilder.java:391)
    at org.apache.camel.impl.DefaultCamelContext$3.call(DefaultCamelContext.java:1024)
    at org.apache.camel.impl.DefaultCamelContext$3.call(DefaultCamelContext.java:1021)
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3267)
    at org.apache.camel.impl.DefaultCamelContext.addRoutes(DefaultCamelContext.java:1021)
    at com.agile.net.international.mc360.routing.routebuilder.process.mail.MailRouteProcess.lambda$checkMailDataSourceRoutes$0(MailRouteProcess.java:135)
    at java.util.HashMap.forEach(HashMap.java:1289)
    at com.agile.net.international.mc360.routing.routebuilder.process.mail.MailRouteProcess.checkMailDataSourceRoutes(MailRouteProcess.java:131)
    at com.agile.net.international.mc360.routing.routebuilder.process.mail.MailRouteProcess.process(MailRouteProcess.java:60)
    at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197)
    at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:79)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.IllegalArgumentException: ref must be specified on: process[Processor@0x0]
    at org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:340)
    at org.apache.camel.model.ProcessDefinition.createProcessor(ProcessDefinition.java:99)
    at org.apache.camel.model.ProcessorDefinition.createProcessor(ProcessorDefinition.java:518)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:481)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:448)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:186)
    at org.apache.camel.model.OnExceptionDefinition.addRoutes(OnExceptionDefinition.java:210)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1349)
    ... 22 more

错误中的类MailRouteProcess正是我构建imap字符串并将其添加到骆驼上下文中的地方。

我想要的只是当路由失败时将其转到ExceptionMailProcessor。在那里,我将做一些事情,并且实际的邮件消息必须标记为“已读”,并且不再进行处理。

1 个答案:

答案 0 :(得分:1)

与其尝试在ExceptionMailProcessor的实例中自动装配,不如在DSL中将其引用为bean。通常对于Camel中的处理器,我使用Spring Configuration类将处理器注册为bean,然后在DSL中引用它们:

@Configuration
public class AppConfig {

  @Bean(name = "ExceptionMailProcessor ")
  public ExceptionMailProcessor exceptionMailProcessor() {
    return new ExceptionMailProcessor();
  }
}

然后在您的Camel DSL中,您可以像这样引用它:

onException(MailRouteException.class, RuntimeException.class).log(LoggingLevel.ERROR, "EXCEPTION in MAIL")
                .to("bean:ExceptionMailProcessor")
                .end();