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