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
018package org.apache.logging.log4j.core.net.mom.jms;
019
020import java.io.BufferedReader;
021import java.io.InputStreamReader;
022import java.nio.charset.Charset;
023
024import org.apache.logging.log4j.core.net.server.JmsServer;
025
026/**
027 * Receives Log Events over a JMS Queue. This implementation expects that all messages will
028 * contain a serialized LogEvent.
029 */
030public class JmsQueueReceiver {
031
032    /**
033     * Main startup for the receiver.
034     * @param args The command line arguments.
035     * @throws Exception if an error occurs.
036     */
037    public static void main(final String[] args) throws Exception {
038        if (args.length != 4) {
039            usage("Wrong number of arguments.");
040        }
041
042        final String qcfBindingName = args[0];
043        final String queueBindingName = args[1];
044        final String username = args[2];
045        final String password = args[3];
046        final JmsServer server = new JmsServer(qcfBindingName, queueBindingName, username, password);
047        server.start();
048
049        final Charset enc = Charset.defaultCharset();
050        final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
051        // Loop until the word "exit" is typed
052        System.out.println("Type \"exit\" to quit JmsQueueReceiver.");
053        while (true) {
054            final String line = stdin.readLine();
055            if (line == null || line.equalsIgnoreCase("exit")) {
056                System.out.println("Exiting. Kill the application if it does not exit "
057                    + "due to daemon threads.");
058                server.stop();
059                return;
060            }
061        }
062    }
063
064
065    private static void usage(final String msg) {
066        System.err.println(msg);
067        System.err.println("Usage: java " + JmsQueueReceiver.class.getName()
068            + " QueueConnectionFactoryBindingName QueueBindingName username password");
069        System.exit(1);
070    }
071}