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.MessageConsumer;
 import  javax.jms.ObjectMessage;
 import  javax.jms.Queue;
 import  javax.jms.QueueConnection;
 import  javax.jms.QueueConnectionFactory;
 import  javax.jms.QueueSession;
 import  javax.jms.Session;
A simple application that consumes logging events sent by a JMSQueueAppender.

Ceki Gülcü
 public class JMSQueueSink 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 qcfBindingName = args[0];
     String queueBindingName = 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 JMSQueueSink(qcfBindingNamequeueBindingNameusernamepassword);
     BufferedReader stdin = new BufferedReader(new InputStreamReader(.));
     // Loop until the word "exit" is typed
     ..println("Type \"exit\" to quit JMSQueueSink.");
     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 JMSQueueSink(String qcfBindingNameString queueBindingName,
       String usernameString password) {
     try {
       Properties env = new Properties();
       Context ctx = new InitialContext(env);
       QueueConnectionFactory queueConnectionFactory;
       queueConnectionFactory = (QueueConnectionFactory) lookup(ctx,
       ..println("Queue Cnx Factory found");
       Queue queue = (Queue) ctx.lookup(queueBindingName);
       ..println("Queue found: " + queue.getQueueName());
       QueueConnection queueConnection = queueConnectionFactory
      ..println("Queue Connection created");
      QueueSession queueSession = queueConnection.createQueueSession(false,
      MessageConsumer queueConsumer = queueSession.createConsumer(queue);
      ..println("Queue 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 "
            + JMSQueueSink.class.getName()
            + " QueueConnectionFactoryBindingName QueueBindingName Username Password");
New to GrepCode? Check out our FAQ X