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