1 Reply Latest reply on Sep 4, 2002 2:15 PM by Kristoffer Gronowski

    Problem with Log4J and JMSAppender

    Mauro Ramon Newbie

      hi all, we got a problem when trying to use Log4J with JMSAppender. We are using Jboss 3.0+Jetty.
      We defined a Topic in jbossmq-destinations-service.xml
      <mbean code="org.jboss.mq.server.jmx.Topic"name="jboss.mq.destination:service=Topic,name=topicLogTest">
      <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager


      We configured Log4J of the following form:
      log4j.category.com.sancorseguros.nova =INFO, JMS

      log4j.appender.JMS=org.apache.log4j.net.JMSAppender
      log4j.appender.JMS.TopicBindingName=topic/topicLogTest
      log4j.appender.JMS.TopicConnectionFactoryBindingName=ConnectionFactory

      Everything seem to work fine for logging. We have create an stand-alone class to subscribe to topic. A fragment of code:
      try {
      InitialContext ctx = new InitialContext();
      TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
      topicCon = tcf.createTopicConnection();
      TopicSession topicSess = topicCon.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
      Topic topic = (Topic) ctx.lookup("topic/topicLogTest");
      TopicSubscriber topicSubscriber = topicSess.createSubscriber(topic);
      TextListener listener = new TextListener();
      topicSubscriber.setMessageListener(listener);
      topicCon.start();
      while (this.start) {
      / /nada
      }
      } catch(Exception e) {
      e.printStackTrace(System.out);
      } finally {
      if (topicCon != null) {
      try {
      topicCon.close();
      } catch(Exception e) {
      e.printStackTrace(System.out);
      }
      }
      }

      TextListener to implement MessageListener.
      public void onMessage(Message message) {
      TextMessage msg = null;
      try {
      if (message instanceof TextMessage) {
      msg = (TextMessage) message;
      System.out.println("Reading message: " +msg.getText());
      } else {
      System.out.println("Message of wrong type: " + message.getClass().getName());
      }
      } catch (JMSException e) {
      System.out.println("JMSException in onMessage(): " +e.toString());
      } catch (Throwable t) {
      System.out.println("Exception in onMessage():" + t.getMessage());
      }
      }

      The problem occur when TextListener try to evaluate message instanceof TextMessage. The class type for Message parameter is org.jboss.mq.SpyObjectMessage and we don't to see the text message.

      We have forgot to configure something???
      Please, any tip are welcome.
      Thanks in advance.
      PD:Sorry for my english please

        • 1. Re: Problem with Log4J and JMSAppender
          Kristoffer Gronowski Newbie

          Hi!

          If I'm not totally wrong then the problem should be that you are expecting a javax.jms.TextMessage while when I looked in the source code of log4j the JMSAppender is using ObjectMessage to transport the event.
          I guess that you should do something like this.

          public void onMessage(Message message) {
          if( message instanceof ObjectMessage ){
          ObjectMessage om = (ObjectMessage) message;
          Object o = message.getObject();

          Download the code from http://jakarta.apache.org/log4j/
          There is very limited information about log4j and JBossmq so I guess one has to rely on the source code.

          Regards, Stoffe!