Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 import  javax.jms.JMSException;
 import  javax.jms.ObjectMessage;
 import  javax.jms.Session;
 import  javax.jms.Topic;
 import  javax.jms.TopicConnection;
 import  javax.jms.TopicConnectionFactory;
 import  javax.jms.TopicSession;
 import  javax.jms.TopicSubscriber;
A simple application that consumes logging events sent by a JMSTopicAppender.

Ceki Gülcü
 public class JMSTopicSink implements javax.jms.MessageListener {
   private Logger logger = (Logger)LoggerFactory.getLogger(JMSTopicSink.class);
   static public void main(String[] argsthrows Exception {
     if (args.length < 2) {
       usage("Wrong number of arguments.");
     String tcfBindingName = args[0];
     String topicBindingName = args[1];
     String username = null;
     String password = null;
     if (args.length == 4) {
       username = args[2];
       password = args[3];
     LoggerContext loggerContext = (LoggerContext) LoggerFactory
     new ContextInitializer(loggerContext).autoConfig();
     new JMSTopicSink(tcfBindingNametopicBindingNameusernamepassword);
     BufferedReader stdin = new BufferedReader(new InputStreamReader(.));
     // Loop until the word "exit" is typed
     ..println("Type \"exit\" to quit JMSTopicSink.");
     while (true) {
       String s = stdin.readLine();
       if (s.equalsIgnoreCase("exit")) {
         ..println("Exiting. Kill the application if it does not exit "
             + "due to daemon threads.");
   public JMSTopicSink(String tcfBindingNameString topicBindingName,
       String usernameString password) {
     try {
       Properties env = new Properties();
       Context ctx = new InitialContext(env);
       TopicConnectionFactory topicConnectionFactory;
       topicConnectionFactory = (TopicConnectionFactory) lookup(ctx,
       ..println("Topic Cnx Factory found");
       Topic topic = (Topic) ctx.lookup(topicBindingName);
       ..println("Topic found: " + topic.getTopicName());
       TopicConnection topicConnection = topicConnectionFactory
      ..println("Topic Connection created");
      TopicSession topicSession = topicConnection.createTopicSession(false,
      TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
      ..println("Topic Connection started");
    } catch (Exception e) {
      .error("Could not read JMS message."e);
  public void onMessage(javax.jms.Message message) {
    ILoggingEvent event;
    try {
      if (message instanceof ObjectMessage) {
        ObjectMessage objectMessage = (ObjectMessage) message;
        event = (ILoggingEventobjectMessage.getObject();
        Logger log = (Logger) LoggerFactory.getLogger(event.getLoggerName());
      } else {
        .warn("Received message is of type " + message.getJMSType()
            + ", was expecting ObjectMessage.");
    } catch (JMSException jmse) {
      .error("Exception thrown while processing incoming message."jmse);
  protected Object lookup(Context ctxString name)
      throws NamingException {
    try {
      return ctx.lookup(name);
    } catch (NameNotFoundException e) {
      .error("Could not find name [" + name + "].");
      throw e;
  static void usage(String msg) {
        .println("Usage: java "
            + JMSTopicSink.class.getName()
            + " TopicConnectionFactoryBindingName TopicBindingName Username Password");
New to GrepCode? Check out our FAQ X