log4j with OpenJMS

D

Duane Morin

Anybody got sample code for sending log4j messages specifically to OpenJMS?
In particular I'd like to do it all programmatically, since I'm working with a fairly complex pre-existing build system and would rather not start introducing too many new properties files. I have a log4j.properties file, but I'm trying to avoid having to create a jndi.properties file.

I stuck this code from OpenJMS in my Logging utility (which wraps log4j):

Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "rmi://localhost:1099/");
Context context = new InitialContext(properties);

and log4j.properties has a simple:
log4j.myLogger=DEBUG, JMS1
log4j.appender.JMS1=org.apache.log4j.net.JMSAppender
log4j.appender.JMS1.TopicBindingName=testtopic
log4j.appender.JMS1.TopicConnectionFactoryBindingName=JmsTopicConnectionFactory


I keep getting the error "javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial" which leads me to believe that my block of code is not storing the InitialContext in a way so that log4j can get at it.

Thanks!
 
D

Duane Morin

Never mind, fixed my own error. Once I put InitialContextFactoryName and
ProviderURL into the log4j.properties instead of trying to do it in
code, everything worked fine.

Duane
 
D

Duane Morin

Maybe I'm not done after all. OpenJMS is getting my log events, I can
tell this because I can print their ID and priority. Now I'm trying to
get at the actual data. Log4j sends the message as a LoggingEvent.
So I try this:

public void onMessage(Message message) {
try {
if (message instanceof org.exolab.jms.message.ObjectMessageImpl) {
ObjectMessage o = (ObjectMessage)message;
LoggingEvent l = (LoggingEvent)(o.getObject());
}
} catch (MessageFormatException mf) {
System.err.println(mf);
} catch (JMSException je) {
System.err.println(je);
}


what happens is that I always get the error:

javax.jms.MessageFormatException : org.apache.log4j.spi.LoggingEvent

This occurs on any line that references o.getObject(). I tried printing
it, or getting its class, but can't do it.

getObject() returns a Serializable, so maybe I'm not de-serializing the
reference properly?

Duane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top