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