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.Session;
025    import javax.jms.Topic;
026    import javax.jms.TopicConnection;
027    import javax.jms.TopicConnectionFactory;
028    import javax.jms.TopicSession;
029    import javax.jms.TopicSubscriber;
030    import javax.naming.Context;
031    import javax.naming.InitialContext;
032    import javax.naming.NamingException;
033    
034    /**
035     * Receives Topic messages that contain LogEvents. This implementation expects that all messages
036     * are serialized log events.
037     */
038    public class JmsTopicReceiver extends AbstractJmsReceiver {
039    
040        /**
041         * Constructor.
042         * @param tcfBindingName The TopicConnectionFactory binding name.
043         * @param topicBindingName The Topic binding name.
044         * @param username The userid to connect to the topic.
045         * @param password The password to connect to the topic.
046         */
047        public JmsTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
048                                final String password) {
049            try {
050                final Context ctx = new InitialContext();
051                TopicConnectionFactory topicConnectionFactory;
052                topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
053                final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
054                topicConnection.start();
055                final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
056                final Topic topic = (Topic) ctx.lookup(topicBindingName);
057                final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
058                topicSubscriber.setMessageListener(this);
059            } catch (final JMSException e) {
060                logger.error("Could not read JMS message.", e);
061            } catch (final NamingException e) {
062                logger.error("Could not read JMS message.", e);
063            } catch (final RuntimeException e) {
064                logger.error("Could not read JMS message.", e);
065            }
066        }
067    
068        /**
069         * Main startup for the receiver.
070         * @param args The command line arguments.
071         * @throws Exception if an error occurs.
072         */
073        public static void main(final String[] args) throws Exception {
074            if (args.length != 4) {
075                usage("Wrong number of arguments.");
076            }
077    
078            final String tcfBindingName = args[0];
079            final String topicBindingName = args[1];
080            final String username = args[2];
081            final String password = args[3];
082    
083            new JmsTopicReceiver(tcfBindingName, topicBindingName, username, password);
084    
085            final Charset enc = Charset.defaultCharset();
086            final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
087            // Loop until the word "exit" is typed
088            System.out.println("Type \"exit\" to quit JmsTopicReceiver.");
089            while (true) {
090                final String line = stdin.readLine();
091                if (line == null || line.equalsIgnoreCase("exit")) {
092                    System.out.println("Exiting. Kill the application if it does not exit "
093                        + "due to daemon threads.");
094                    return;
095                }
096            }
097        }
098    
099        private static void usage(final String msg) {
100            System.err.println(msg);
101            System.err.println("Usage: java " + JmsTopicReceiver.class.getName()
102                + " TopicConnectionFactoryBindingName TopicBindingName username password");
103            System.exit(1);
104        }
105    }