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