View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.jms;
18  
19  import java.io.BufferedReader;
20  import java.io.InputStreamReader;
21  import java.nio.charset.Charset;
22  
23  import javax.jms.JMSException;
24  import javax.jms.Session;
25  import javax.jms.Topic;
26  import javax.jms.TopicConnection;
27  import javax.jms.TopicConnectionFactory;
28  import javax.jms.TopicSession;
29  import javax.jms.TopicSubscriber;
30  import javax.naming.Context;
31  import javax.naming.InitialContext;
32  import javax.naming.NamingException;
33  
34  /**
35   * Receives Topic messages that contain LogEvents. This implementation expects that all messages
36   * are serialized log events.
37   */
38  public class JmsTopicReceiver extends AbstractJmsReceiver {
39  
40      /**
41       * Constructor.
42       * @param tcfBindingName The TopicConnectionFactory binding name.
43       * @param topicBindingName The Topic binding name.
44       * @param username The userid to connect to the topic.
45       * @param password The password to connect to the topic.
46       */
47      public JmsTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
48                              final String password) {
49          try {
50              final Context ctx = new InitialContext();
51              TopicConnectionFactory topicConnectionFactory;
52              topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
53              final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
54              topicConnection.start();
55              final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
56              final Topic topic = (Topic) ctx.lookup(topicBindingName);
57              final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
58              topicSubscriber.setMessageListener(this);
59          } catch (final JMSException e) {
60              logger.error("Could not read JMS message.", e);
61          } catch (final NamingException e) {
62              logger.error("Could not read JMS message.", e);
63          } catch (final RuntimeException e) {
64              logger.error("Could not read JMS message.", e);
65          }
66      }
67  
68      /**
69       * Main startup for the receiver.
70       * @param args The command line arguments.
71       * @throws Exception if an error occurs.
72       */
73      public static void main(final String[] args) throws Exception {
74          if (args.length != 4) {
75              usage("Wrong number of arguments.");
76          }
77  
78          final String tcfBindingName = args[0];
79          final String topicBindingName = args[1];
80          final String username = args[2];
81          final String password = args[3];
82  
83          new JmsTopicReceiver(tcfBindingName, topicBindingName, username, password);
84  
85          final Charset enc = Charset.defaultCharset();
86          final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
87          // Loop until the word "exit" is typed
88          System.out.println("Type \"exit\" to quit JmsTopicReceiver.");
89          while (true) {
90              final String line = stdin.readLine();
91              if (line == null || line.equalsIgnoreCase("exit")) {
92                  System.out.println("Exiting. Kill the application if it does not exit "
93                      + "due to daemon threads.");
94                  return;
95              }
96          }
97      }
98  
99      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 }