English 中文(简体)
这种处理阿帕奇骆驼空体的手法能更优雅吗?
原标题:Can this handling of a null body in Apache Camel be more elegant?

我新到骆驼, 并尝试学习语言和最佳做法。 我正在撰写网络服务, 需要处理多个不同的错误案例。 以下是我的错误处理和路由 :

onException(JsonParseException.class).inOut("direct:syntaxError").handled(true);
onException(UnrecognizedPropertyException.class).inOut("direct:syntaxError").handled(true);

// Route service through direct to allow testing.
from("servlet:///service?matchOnUriPrefix=true").inOut("direct:service");
from("direct:service")
    .choice()
        .when(body().isEqualTo(null))
            .inOut("direct:syntaxError")
        .otherwise()
            .unmarshal().json(lJsonLib, AuthorizationParameters.class).inOut("bean:mybean?method=serviceMethod").marshal().json(lJsonLib);

如你所见, 我有一个特殊的处理方式( 基于内容的路线) 来处理与一个空体的请求 。 有更优雅的方法可以处理吗? 我写了几种这样的服务, 看起来它们可能更干净一些 。

最佳回答

您可以使用截击器, 如截取时间等, 来检查空bod, 因为这里有一个例子 : < a href="http:// camel.apache. org/interphict" >http:// camel.apache. org/intercept

然后用停止来表示没有进一步的处理 :

interceptFrom("servlet*").when(body().isNull()).to("direct:syntaxError").stop();
问题回答

使用 body ().isNull () 表达方式在基于内容的路径中将 null message 改为 < a href=> http://www. incentrial Inclutionpatterns.com/DeadLetterChannel.html" rel=“noreferr" >Dead Letter Channel 。请注意,重订到 DLC 的信件仍然包含信头,以便您能够轻松分析稍后交付失败的原因。

choice().
   when(body().isNull()).to("jms:deadLetterChannel").
   otherwise().to("jms:regularProcessing").
endChoice();

您可以使用一个检查无效的豆子, 并在无效的情况下丢弃一个例外。 这样您就可以在处理例外时处理这个案子 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签