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 javax.jms.JMSException;
20  import javax.jms.Queue;
21  import javax.jms.QueueConnection;
22  import javax.jms.QueueConnectionFactory;
23  import javax.jms.QueueReceiver;
24  import javax.jms.QueueSession;
25  import javax.jms.Session;
26  import javax.naming.Context;
27  import javax.naming.InitialContext;
28  import javax.naming.NamingException;
29  import java.io.BufferedReader;
30  import java.io.InputStreamReader;
31  
32  /**
33   * Receives Log Events over a JMS Queue. This implementation expects that all messages will
34   * contain a serialized LogEvent.
35   */
36  public class JMSQueueReceiver extends AbstractJMSReceiver {
37  
38      /**
39       * Constructor.
40       * @param qcfBindingName The QueueConnectionFactory binding name.
41       * @param queueBindingName The Queue binding name.
42       * @param username The userid to connect to the queue.
43       * @param password The password to connect to the queue.
44       */
45      public JMSQueueReceiver(final String qcfBindingName, final String queueBindingName, final String username,
46                              final String password) {
47  
48          try {
49              final Context ctx = new InitialContext();
50              QueueConnectionFactory queueConnectionFactory;
51              queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
52              final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
53              queueConnection.start();
54              final QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
55              final Queue queue = (Queue) ctx.lookup(queueBindingName);
56              final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
57              queueReceiver.setMessageListener(this);
58          } catch (final JMSException e) {
59              logger.error("Could not read JMS message.", e);
60          } catch (final NamingException e) {
61              logger.error("Could not read JMS message.", e);
62          } catch (final RuntimeException e) {
63              logger.error("Could not read JMS message.", e);
64          }
65      }
66  
67      /**
68       * Main startup for the receiver.
69       * @param args The command line arguments.
70       * @throws Exception if an error occurs.
71       */
72      public static void main(final String[] args) throws Exception {
73          if (args.length != 4) {
74              usage("Wrong number of arguments.");
75          }
76  
77          final String qcfBindingName = args[0];
78          final String queueBindingName = args[1];
79          final String username = args[2];
80          final String password = args[3];
81  
82          new JMSQueueReceiver(qcfBindingName, queueBindingName, username, password);
83  
84          final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
85          // Loop until the word "exit" is typed
86          System.out.println("Type \"exit\" to quit JMSQueueReceiver.");
87          while (true) {
88              final String s = stdin.readLine();
89              if (s.equalsIgnoreCase("exit")) {
90                  System.out.println("Exiting. Kill the application if it does not exit "
91                      + "due to daemon threads.");
92                  return;
93              }
94          }
95      }
96  
97  
98      private static void usage(final String msg) {
99          System.err.println(msg);
100         System.err.println("Usage: java " + JMSQueueReceiver.class.getName()
101             + " QueueConnectionFactoryBindingName QueueBindingName username password");
102         System.exit(1);
103     }
104 }