apache camel
Apache Camel日志组件示例
您要将消息记录到底层的记录机制中,请使用骆驼的log:
组件。 Camel使用sfl4j
作为记录器API,然后允许您配置记录器实现。 在本文中,我们将使用Log4j作为实际的记录器机制。 让我们从我们的例子开始。
依存关系
您需要添加:
-
slf4j-api
– SLF4J Logger API -
slf4j-log4j12
– Log4j作为记录器的实现
pom.xml:
4.0.0
com.javarticles.camel
camelHelloWorld
0.0.1-SNAPSHOT
org.apache.camel
camel-core
2.15.1
org.apache.camel
camel-stream
2.15.1
org.apache.camel
camel-jms
2.15.1
org.apache.activemq
activemq-camel
5.6.0
org.springframework
spring-context
4.1.5.RELEASE
org.apache.camel
camel-spring
2.15.1
org.slf4j
slf4j-api
1.7.12
org.slf4j
slf4j-log4j12
1.7.12
日志组件URI格式
日志的uri格式:
log:loggingCategory[?options]
您可以使用选项来设置级别或格式设置选项。 例如:
log:com.javarticles?level=INFO
在我的log4.properties中,root logger记录到文件以及控制台,而它仅记录com.javarticles
类别的文件。 log4j.properties:
# Root logger option
log4j.rootLogger=INFO, file, console
log4j.logger.com.javarticles=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=javarticles.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d | %p | %F %L | %m%n
# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.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 seconds
12:09| INFO | MarkerIgnoringBase.java 95 | Before converting to uppercase
12:09| INFO | MarkerIgnoringBase.java 95 | Exchange[ExchangePattern: InOnly, BodyType: String, Body: Log me!]
12:09| INFO | MarkerIgnoringBase.java 95 | After converting to uppercase
12: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 uppercase
14:28| INFO | MarkerIgnoringBase.java 95 | Log me!
14:28| INFO | MarkerIgnoringBase.java 95 | After converting to uppercase
14: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.872
19: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.455
19: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
翻译自: https://www.javacodegeeks.com/2015/05/apache-camel-log-component-examples.html
apache camel