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.Session;
021    import javax.jms.Topic;
022    import javax.jms.TopicConnection;
023    import javax.jms.TopicConnectionFactory;
024    import javax.jms.TopicSession;
025    import javax.jms.TopicSubscriber;
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 Topic messages that contain LogEvents. This implementation expects that all messages
034     * are serialized log events.
035     */
036    public class JMSTopicReceiver extends AbstractJMSReceiver {
037    
038        /**
039         * Constructor.
040         * @param tcfBindingName The TopicConnectionFactory binding name.
041         * @param topicBindingName The Topic binding name.
042         * @param username The userid to connect to the topic.
043         * @param password The password to connect to the topic.
044         */
045        public JMSTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
046                                final String password) {
047            try {
048                final Context ctx = new InitialContext();
049                TopicConnectionFactory topicConnectionFactory;
050                topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
051                final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
052                topicConnection.start();
053                final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
054                final Topic topic = (Topic) ctx.lookup(topicBindingName);
055                final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
056                topicSubscriber.setMessageListener(this);
057            } catch (final JMSException e) {
058                logger.error("Could not read JMS message.", e);
059            } catch (final NamingException e) {
060                logger.error("Could not read JMS message.", e);
061            } catch (final 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(final String[] args) throws Exception {
072            if (args.length != 4) {
073                usage("Wrong number of arguments.");
074            }
075    
076            final String tcfBindingName = args[0];
077            final String topicBindingName = args[1];
078            final String username = args[2];
079            final String password = args[3];
080    
081            new JMSTopicReceiver(tcfBindingName, topicBindingName, username, password);
082    
083            final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
084            // Loop until the word "exit" is typed
085            System.out.println("Type \"exit\" to quit JMSTopicReceiver.");
086            while (true) {
087                final 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        private static void usage(final String msg) {
097            System.err.println(msg);
098            System.err.println("Usage: java " + JMSTopicReceiver.class.getName()
099                + " TopicConnectionFactoryBindingName TopicBindingName username password");
100            System.exit(1);
101        }
102    }