View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net;
18  
19  import java.io.BufferedReader;
20  import java.io.InputStreamReader;
21  import java.nio.charset.Charset;
22  import javax.jms.JMSException;
23  import javax.jms.Queue;
24  import javax.jms.QueueConnection;
25  import javax.jms.QueueConnectionFactory;
26  import javax.jms.QueueReceiver;
27  import javax.jms.QueueSession;
28  import javax.jms.Session;
29  import javax.naming.Context;
30  import javax.naming.InitialContext;
31  import javax.naming.NamingException;
32  
33  /**
34   * Receives Log Events over a JMS Queue. This implementation expects that all messages will
35   * contain a serialized LogEvent.
36   */
37  public class JMSQueueReceiver extends AbstractJMSReceiver {
38  
39      /**
40       * Constructor.
41       * @param qcfBindingName The QueueConnectionFactory binding name.
42       * @param queueBindingName The Queue binding name.
43       * @param username The userid to connect to the queue.
44       * @param password The password to connect to the queue.
45       */
46      public JMSQueueReceiver(final String qcfBindingName, final String queueBindingName, final String username,
47                              final String password) {
48  
49          try {
50              final Context ctx = new InitialContext();
51              QueueConnectionFactory queueConnectionFactory;
52              queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
53              final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
54              queueConnection.start();
55              final QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
56              final Queue queue = (Queue) ctx.lookup(queueBindingName);
57              final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
58              queueReceiver.setMessageListener(this);
59          } catch (final JMSException e) {
60              logger.error("Could not read JMS message.", e);
61          } catch (final NamingException e) {
62              logger.error("Could not read JMS message.", e);
63          } catch (final RuntimeException e) {
64              logger.error("Could not read JMS message.", e);
65          }
66      }
67  
68      /**
69       * Main startup for the receiver.
70       * @param args The command line arguments.
71       * @throws Exception if an error occurs.
72       */
73      public static void main(final String[] args) throws Exception {
74          if (args.length != 4) {
75              usage("Wrong number of arguments.");
76          }
77  
78          final String qcfBindingName = args[0];
79          final String queueBindingName = args[1];
80          final String username = args[2];
81          final String password = args[3];
82  
83          new JMSQueueReceiver(qcfBindingName, queueBindingName, username, password);
84  
85          final Charset enc = Charset.defaultCharset();
86          final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
87          // Loop until the word "exit" is typed
88          System.out.println("Type \"exit\" to quit JMSQueueReceiver.");
89          while (true) {
90              final String line = stdin.readLine();
91              if (line == null || line.equalsIgnoreCase("exit")) {
92                  System.out.println("Exiting. Kill the application if it does not exit "
93                      + "due to daemon threads.");
94                  return;
95              }
96          }
97      }
98  
99  
100     private static void usage(final String msg) {
101         System.err.println(msg);
102         System.err.println("Usage: java " + JMSQueueReceiver.class.getName()
103             + " QueueConnectionFactoryBindingName QueueBindingName username password");
104         System.exit(1);
105     }
106 }