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