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;
18  
19  import java.io.BufferedReader;
20  import java.io.InputStreamReader;
21  import java.nio.charset.Charset;
22  import javax.jms.JMSException;
23  import javax.jms.Session;
24  import javax.jms.Topic;
25  import javax.jms.TopicConnection;
26  import javax.jms.TopicConnectionFactory;
27  import javax.jms.TopicSession;
28  import javax.jms.TopicSubscriber;
29  import javax.naming.Context;
30  import javax.naming.InitialContext;
31  import javax.naming.NamingException;
32  
33  /**
34   * Receives Topic messages that contain LogEvents. This implementation expects that all messages
35   * are serialized log events.
36   */
37  public class JMSTopicReceiver extends AbstractJMSReceiver {
38  
39      /**
40       * Constructor.
41       * @param tcfBindingName The TopicConnectionFactory binding name.
42       * @param topicBindingName The Topic binding name.
43       * @param username The userid to connect to the topic.
44       * @param password The password to connect to the topic.
45       */
46      public JMSTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
47                              final String password) {
48          try {
49              final Context ctx = new InitialContext();
50              TopicConnectionFactory topicConnectionFactory;
51              topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
52              final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
53              topicConnection.start();
54              final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
55              final Topic topic = (Topic) ctx.lookup(topicBindingName);
56              final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
57              topicSubscriber.setMessageListener(this);
58          } catch (final JMSException e) {
59              logger.error("Could not read JMS message.", e);
60          } catch (final NamingException e) {
61              logger.error("Could not read JMS message.", e);
62          } catch (final RuntimeException e) {
63              logger.error("Could not read JMS message.", e);
64          }
65      }
66  
67      /**
68       * Main startup for the receiver.
69       * @param args The command line arguments.
70       * @throws Exception if an error occurs.
71       */
72      public static void main(final String[] args) throws Exception {
73          if (args.length != 4) {
74              usage("Wrong number of arguments.");
75          }
76  
77          final String tcfBindingName = args[0];
78          final String topicBindingName = args[1];
79          final String username = args[2];
80          final String password = args[3];
81  
82          new JMSTopicReceiver(tcfBindingName, topicBindingName, username, password);
83  
84          final Charset enc = Charset.defaultCharset();
85          final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
86          // Loop until the word "exit" is typed
87          System.out.println("Type \"exit\" to quit JMSTopicReceiver.");
88          while (true) {
89              final String line = stdin.readLine();
90              if (line == null || line.equalsIgnoreCase("exit")) {
91                  System.out.println("Exiting. Kill the application if it does not exit "
92                      + "due to daemon threads.");
93                  return;
94              }
95          }
96      }
97  
98      private static void usage(final String msg) {
99          System.err.println(msg);
100         System.err.println("Usage: java " + JMSTopicReceiver.class.getName()
101             + " TopicConnectionFactoryBindingName TopicBindingName username password");
102         System.exit(1);
103     }
104 }