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    
032    /**
033     * Receives Log Events over a JMS Queue. This implementation expects that all messages will
034     * contain a serialized LogEvent.
035     */
036    public class JMSQueueReceiver extends AbstractJMSReceiver {
037    
038        /**
039         * Constructor.
040         * @param qcfBindingName The QueueConnectionFactory binding name.
041         * @param queueBindingName The Queue binding name.
042         * @param username The userid to connect to the queue.
043         * @param password The password to connect to the queue.
044         */
045        public JMSQueueReceiver(final String qcfBindingName, final String queueBindingName, final String username,
046                                final String password) {
047    
048            try {
049                final Context ctx = new InitialContext();
050                QueueConnectionFactory queueConnectionFactory;
051                queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
052                final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
053                queueConnection.start();
054                final QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
055                final Queue queue = (Queue) ctx.lookup(queueBindingName);
056                final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
057                queueReceiver.setMessageListener(this);
058            } catch (final JMSException e) {
059                logger.error("Could not read JMS message.", e);
060            } catch (final NamingException e) {
061                logger.error("Could not read JMS message.", e);
062            } catch (final RuntimeException e) {
063                logger.error("Could not read JMS message.", e);
064            }
065        }
066    
067        /**
068         * Main startup for the receiver.
069         * @param args The command line arguments.
070         * @throws Exception if an error occurs.
071         */
072        public static void main(final String[] args) throws Exception {
073            if (args.length != 4) {
074                usage("Wrong number of arguments.");
075            }
076    
077            final String qcfBindingName = args[0];
078            final String queueBindingName = args[1];
079            final String username = args[2];
080            final String password = args[3];
081    
082            new JMSQueueReceiver(qcfBindingName, queueBindingName, username, password);
083    
084            final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
085            // Loop until the word "exit" is typed
086            System.out.println("Type \"exit\" to quit JMSQueueReceiver.");
087            while (true) {
088                final String s = stdin.readLine();
089                if (s.equalsIgnoreCase("exit")) {
090                    System.out.println("Exiting. Kill the application if it does not exit "
091                        + "due to daemon threads.");
092                    return;
093                }
094            }
095        }
096    
097    
098        private static void usage(final String msg) {
099            System.err.println(msg);
100            System.err.println("Usage: java " + JMSQueueReceiver.class.getName()
101                + " QueueConnectionFactoryBindingName QueueBindingName username password");
102            System.exit(1);
103        }
104    }