I want to connect a log4j JMSAppender
to an ActiveMQ queue over Apache Camel, so that both are endpoints along a route. Furthermore, I'd like to have all the config done in Java (not XML).
The log4j javadoc here and this SO question both show great examples of how to set up such a connection sans Camel (via JNDI), but they are not quite what I am looking for.
I know this is possible through the activemq-camel
Camel component, and in fact that component's tutorial page shows how to set up a pooled connection factory to a broker URL. Unfortunately (for me) its all in XML.
I am struggling with:
- Trying to figure out how to turn their XML examples (link above) into Java code; and
- How to tie everything together so that the
JMSAppender
publishes messages over Camel to the correct queue; this involves endpoint creation and routebuilding
Here is my best attempt:
Somewhere I will need an init()
-type method to set up my CamelContext
:
CamelContext context = new DefaultCamelContext();
context.addComponent("log4j-jms-appender", new LogComponent()); // ????
context.addComponent("activemq",
activeMQComponent("vm://localhost?broker.persistent=false")); // ???
context.addRoutes(new RouteBuilder() {
public void configure() {
from("log4j-jms-appender").to("activemq:queue:log-queue");
}
});
Now, there are already several problems with this code - and I may have even gone down the wrong path entirely. As you can see, I'm struggling to add the endpoint components correctly. I'm also completely unsure of what the string "vm://
" protocol is or what it stands for. All the other code examples I see involve ActiveMQ utilizing the tcp
protocol.
Putting it altogether, it would just be nice to write a log4j Logger
and Appender
like this:
Logger logger = Logger.getLogger("foo.bar");
JMSAppender jmsAppender = configureJMSAppender();
logger.addAppender(jmsAppender);
...and then have all of its log messages placed on the ActiveMQ log-queue
via Camel (and not JNDI/Java).
Thanks in advance for any and all help!