0

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!

Community
  • 1
  • 1
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

1 Answers1

1

The camel-log component doesn't have a consumer...so you can't wire up a route to consume from the log and send to a queue.

One option is to configure a JMSAppender in the log4j.properties as this page describes...

http://activemq.apache.org/how-do-i-use-log4j-jms-appender-with-activemq.html

Also, vm:// stands for virtual machine transport...it allows you to connect to an ActiveMQ broker running in your VM...

Ben ODay
  • 20,784
  • 9
  • 45
  • 68
  • Thanks Boday - so you're saying it doesn't look like I can have Camel be the "middleman" between my log4j JMSAppender and ActiveMQ - I need to just use a pure log-to-AMQ connection? – IAmYourFaja Feb 01 '12 at 12:02
  • its just one option, you don't need Camel for it...just an AMQ broker – Ben ODay Feb 01 '12 at 16:29
  • Yeah log4j can send log messages directly to a JMS Broker. You do not need Camel in the middle. However if you run Camel in an OSGi container that uses pax-logging, then you can have Camel listen to the logs using the pax-logging Camel component: http://camel.apache.org/pax-logging.html – Claus Ibsen Feb 02 '12 at 04:51