联系我们 - 广告服务 - 联系电话:
您的当前位置: > 综合 > > 正文

camel_Apache Camel日志组件示例

来源:CSDN 时间:2023-02-09 08:10:55

camel

Apache Camel日志组件示例


(资料图片)

您要将消息记录到底层的记录机制中,请使用骆驼的log:组件。 Camel使用sfl4j作为记录器API,然后允许您配置记录器实现。 在本文中,我们将使用Log4j作为实际的记录器机制。 让我们从我们的例子开始。

依存关系

您需要添加:

slf4j-api– SLF4J Logger API slf4j-log4j12– Log4j作为记录器的实现

pom.xml:

4.0.0com.javarticles.camelcamelHelloWorld0.0.1-SNAPSHOTorg.apache.camelcamel-core2.15.1org.apache.camelcamel-stream2.15.1org.apache.camelcamel-jms2.15.1org.apache.activemqactivemq-camel5.6.0org.springframeworkspring-context4.1.5.RELEASEorg.apache.camelcamel-spring2.15.1org.slf4jslf4j-api1.7.12org.slf4jslf4j-log4j121.7.12

日志组件URI格式

日志的uri格式:

log:loggingCategory[?options]

您可以使用选项来设置级别或格式选项。 例如:

log:com.javarticles?level=INFO

在我的log4.properties中,root logger记录到文件以及控制台,而它仅记录com.javarticles类别的文件。 log4j.properties:

# Root logger optionlog4j.rootLogger=INFO, file, consolelog4j.logger.com.javarticles=INFO, file# Direct log messages to a log filelog4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=javarticles.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d | %p | %F %L | %m%n # Direct log messages to stdoutlog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.Target=System.outlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d{HH:mm}| %p | %F %L | %m%n

骆驼原木组件示例

CamelLog示例:

package com.javarticles.camel.components;import org.apache.camel.CamelContext;import org.apache.camel.ProducerTemplate;import org.apache.camel.builder.RouteBuilder;import org.apache.camel.impl.DefaultCamelContext;import org.apache.camel.util.jndi.JndiContext;public class CamelLogExample {public static final void main(String[] args) throws Exception {JndiContext jndiContext = new JndiContext();jndiContext.bind("stringUtils", new StringUtils());CamelContext camelContext = new DefaultCamelContext(jndiContext);try {camelContext.addRoutes(new RouteBuilder() {public void configure() {from("direct:logExample")        .log("Before converting to uppercase")        .to("log:?level=INFO&showBody=true").to("bean:stringUtils?method=upperCase").log("After converting to uppercase").to("log:com.javarticles?level=INFO&showAll=true");}});ProducerTemplate template = camelContext.createProducerTemplate();camelContext.start();template.sendBody("direct:logExample", "Log me!");} finally {camelContext.stop();}}}

输出:

12:09| INFO | DefaultCamelContext.java 2454 | Apache Camel 2.15.1 (CamelContext: camel-1) started in 0.307 seconds12:09| INFO | MarkerIgnoringBase.java 95 | Before converting to uppercase12:09| INFO | MarkerIgnoringBase.java 95 | Exchange[ExchangePattern: InOnly, BodyType: String, Body: Log me!]12:09| INFO | MarkerIgnoringBase.java 95 | After converting to uppercase12:09| INFO | MarkerIgnoringBase.java 95 | Exchange[Id: ID-INMAA1-L1005-54363-1431153589693-0-2, ExchangePattern: InOnly, Properties: {CamelCreatedTimestamp=Sat May 09 12:09:50 IST 2015, CamelMessageHistory=[DefaultMessageHistory[routeId=route1, node=log1], DefaultMessageHistory[routeId=route1, node=to1], DefaultMessageHistory[routeId=route1, node=to2], DefaultMessageHistory[routeId=route1, node=log2], DefaultMessageHistory[routeId=route1, node=to3]], CamelToEndpoint=log://com.javarticles?level=INFO&showAll=true}, Headers: {breadcrumbId=ID-INMAA1-L1005-54363-1431153589693-0-1}, BodyType: String, Body: LOG ME!, Out: null: ]12:09| INFO | DefaultCamelContext.java 2660 | Apache Camel 2.15.1 (CamelContext: camel-1) is shutting down

自定义Exchange格式化程序

如果您在上述日志中注意到,即使对于showBody=true情况,它也会打印有效负载以及与交换相关的属性,例如ExchangePattern和BodyType。 我们可以自定义想要在日志中看到的内容。 让我们看看如何实现它。 通过实现ExchangeFormatter接口来实现自定义格式器类。 从Exchange对象中选择我们要记录的元素。 在我们的自定义交换格式化程序中,我们只想查看有效负载文本,因此format(Exchange)返回的是入站请求消息。 MyExchangeFormatter:

package com.javarticles.camel.components;import org.apache.camel.Exchange;import org.apache.camel.spi.ExchangeFormatter;public class MyExchangeFormatter implements ExchangeFormatter {    public String format(Exchange exchange) {        return exchange.getIn().getBody(String.class);    }}

您需要将foamtter对象与键logFormatter。

jndiContext.bind("logFormatter", new MyExchangeFormatter());

CamelLogExchangeFormatter示例:

package com.javarticles.camel.components;import org.apache.camel.CamelContext;import org.apache.camel.ProducerTemplate;import org.apache.camel.builder.RouteBuilder;import org.apache.camel.impl.DefaultCamelContext;import org.apache.camel.util.jndi.JndiContext;public class CamelLogExchangeFormatterExample {public static final void main(String[] args) throws Exception {JndiContext jndiContext = new JndiContext();jndiContext.bind("stringUtils", new StringUtils());jndiContext.bind("logFormatter", new MyExchangeFormatter());CamelContext camelContext = new DefaultCamelContext(jndiContext);try {camelContext.addRoutes(new RouteBuilder() {public void configure() {from("direct:logExample")        .log("Before converting to uppercase")        .to("log:?level=INFO").to("bean:stringUtils?method=upperCase").log("After converting to uppercase").to("log:com.javarticles?level=INFO");}});ProducerTemplate template = camelContext.createProducerTemplate();camelContext.start();template.sendBody("direct:logExample", "Log me!");} finally {camelContext.stop();}}}

StringUtils:

package com.javarticles.camel.components;public class StringUtils {public String upperCase(String msg) {return msg.toUpperCase();}}

输出:

14:28| INFO | MarkerIgnoringBase.java 95 | Before converting to uppercase14:28| INFO | MarkerIgnoringBase.java 95 | Log me!14:28| INFO | MarkerIgnoringBase.java 95 | After converting to uppercase14:28| INFO | MarkerIgnoringBase.java 95 | LOG ME!

吞吐量记录器示例

消息(本例中为数字)被发送到activemq队列numbers,路由中的下一个目标将每10秒记录一次消息统计信息。 使用groupInterval=10000选项配置间隔。

applicationContext.xml:

CamelThroughputLogger示例:

package com.javarticles.camel.components;import org.apache.camel.CamelContext;import org.apache.camel.ProducerTemplate;import org.apache.camel.spring.SpringCamelContext;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class CamelThroughputLoggerExample {public static final void main(String[] args) throws Exception {ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);try {ProducerTemplate template = camelContext.createProducerTemplate();camelContext.start();for (int i = 0; i<18000; i++) {    template.sendBody("activemq:queue:numbers", i);}Thread.sleep(10000);} finally {camelContext.stop();}}}

输出:

19:04| INFO | MarkerIgnoringBase.java 95 | Received: 281 new messages, with total 281 so far. Last group took: 470 millis which is: 597.872 messages per second. average: 597.87219:04| INFO | MarkerIgnoringBase.java 95 | Received: 14802 new messages, with total 15083 so far. Last group took: 10001 millis which is: 1,480.052 messages per second. average: 1,440.45519:05| INFO | MarkerIgnoringBase.java 95 | Received: 2917 new messages, with total 18000 so far. Last group took: 10000 millis which is: 291.7 messages per second. average: 879.293

下载源代码

这是一个关于骆驼原木组件的例子。 您可以在此处下载源代码: camelLogComponentExamples.zip

camel

责任编辑:

标签:

相关推荐:

精彩放送:

新闻聚焦
Top