001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.net;
018    
019    import javax.jms.JMSException;
020    import javax.jms.Queue;
021    import javax.jms.QueueConnection;
022    import javax.jms.QueueConnectionFactory;
023    import javax.jms.QueueReceiver;
024    import javax.jms.QueueSession;
025    import javax.jms.Session;
026    import javax.naming.Context;
027    import javax.naming.InitialContext;
028    import javax.naming.NamingException;
029    import java.io.BufferedReader;
030    import java.io.InputStreamReader;
031    import java.nio.charset.Charset;
032    
033    /**
034     * Receives Log Events over a JMS Queue. This implementation expects that all messages will
035     * contain a serialized LogEvent.
036     */
037    public class JMSQueueReceiver extends AbstractJMSReceiver {
038    
039        /**
040         * Constructor.
041         * @param qcfBindingName The QueueConnectionFactory binding name.
042         * @param queueBindingName The Queue binding name.
043         * @param username The userid to connect to the queue.
044         * @param password The password to connect to the queue.
045         */
046        public JMSQueueReceiver(final String qcfBindingName, final String queueBindingName, final String username,
047                                final String password) {
048    
049            try {
050                final Context ctx = new InitialContext();
051                QueueConnectionFactory queueConnectionFactory;
052                queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
053                final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
054                queueConnection.start();
055                final QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
056                final Queue queue = (Queue) ctx.lookup(queueBindingName);
057                final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
058                queueReceiver.setMessageListener(this);
059            } catch (final JMSException e) {
060                logger.error("Could not read JMS message.", e);
061            } catch (final NamingException e) {
062                logger.error("Could not read JMS message.", e);
063            } catch (final RuntimeException e) {
064                logger.error("Could not read JMS message.", e);
065            }
066        }
067    
068        /**
069         * Main startup for the receiver.
070         * @param args The command line arguments.
071         * @throws Exception if an error occurs.
072         */
073        public static void main(final String[] args) throws Exception {
074            if (args.length != 4) {
075                usage("Wrong number of arguments.");
076            }
077    
078            final String qcfBindingName = args[0];
079            final String queueBindingName = args[1];
080            final String username = args[2];
081            final String password = args[3];
082    
083            new JMSQueueReceiver(qcfBindingName, queueBindingName, username, password);
084    
085            final Charset enc = Charset.defaultCharset();
086            final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
087            // Loop until the word "exit" is typed
088            System.out.println("Type \"exit\" to quit JMSQueueReceiver.");
089            while (true) {
090                final String s = stdin.readLine();
091                if (s.equalsIgnoreCase("exit")) {
092                    System.out.println("Exiting. Kill the application if it does not exit "
093                        + "due to daemon threads.");
094                    return;
095                }
096            }
097        }
098    
099    
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    }