Repository: logging-log4j2 Updated Branches: refs/heads/master 6a4be621e -> aff6553cd
Reduce code duplication in jms servers. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/aff6553c Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/aff6553c Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/aff6553c Branch: refs/heads/master Commit: aff6553cd7dcfd11d70096a2e0831a0a4c077468 Parents: 6a4be62 Author: Matt Sicker <[email protected]> Authored: Thu Feb 25 13:12:02 2016 -0600 Committer: Matt Sicker <[email protected]> Committed: Thu Feb 25 13:12:07 2016 -0600 ---------------------------------------------------------------------- .../core/net/mom/jms/AbstractJmsReceiver.java | 48 ++++++++++++++++++++ .../core/net/mom/jms/JmsQueueReceiver.java | 45 ++++-------------- .../core/net/mom/jms/JmsTopicReceiver.java | 44 ++++-------------- .../log4j/core/net/server/JmsServer.java | 23 ++++++++++ 4 files changed, 91 insertions(+), 69 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aff6553c/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/AbstractJmsReceiver.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/AbstractJmsReceiver.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/AbstractJmsReceiver.java new file mode 100644 index 0000000..753fc80 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/AbstractJmsReceiver.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package org.apache.logging.log4j.core.net.mom.jms; + +import org.apache.logging.log4j.core.net.server.JmsServer; + +/** + * Common JMS server functionality. + * + * @since 2.6 + */ +public abstract class AbstractJmsReceiver { + + /** + * Prints out usage information to {@linkplain System#err standard error}. + */ + protected abstract void usage(); + + /** + * Executes a JmsServer with the given command line arguments. + * + * @param args command line arguments + * @throws Exception + */ + protected void doMain(final String... args) throws Exception { + if (args.length != 4) { + usage(); + System.exit(1); + } + final JmsServer server = new JmsServer(args[0], args[1], args[2], args[3]); + server.run(); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aff6553c/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsQueueReceiver.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsQueueReceiver.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsQueueReceiver.java index af5b6c4..dac46d3 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsQueueReceiver.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsQueueReceiver.java @@ -17,55 +17,30 @@ package org.apache.logging.log4j.core.net.mom.jms; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.nio.charset.Charset; - -import org.apache.logging.log4j.core.net.server.JmsServer; - /** * Receives Log Events over a JMS Queue. This implementation expects that all messages will * contain a serialized LogEvent. */ -public abstract class JmsQueueReceiver { +public class JmsQueueReceiver extends AbstractJmsReceiver { + + private JmsQueueReceiver() { + } /** * Main startup for the receiver. + * * @param args The command line arguments. * @throws Exception if an error occurs. */ public static void main(final String[] args) throws Exception { - if (args.length != 4) { - usage("Wrong number of arguments."); - } - - final String qcfBindingName = args[0]; - final String queueBindingName = args[1]; - final String username = args[2]; - final String password = args[3]; - final JmsServer server = new JmsServer(qcfBindingName, queueBindingName, username, password); - server.start(); - - final Charset enc = Charset.defaultCharset(); - final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc)); - // Loop until the word "exit" is typed - System.out.println("Type \"exit\" to quit JmsQueueReceiver."); - while (true) { - final String line = stdin.readLine(); - if (line == null || line.equalsIgnoreCase("exit")) { - System.out.println("Exiting. Kill the application if it does not exit " - + "due to daemon threads."); - server.stop(); - return; - } - } + final JmsQueueReceiver receiver = new JmsQueueReceiver(); + receiver.doMain(args); } - - private static void usage(final String msg) { - System.err.println(msg); + @Override + protected void usage() { + System.err.println("Wrong number of arguments."); System.err.println("Usage: java " + JmsQueueReceiver.class.getName() + " QueueConnectionFactoryBindingName QueueBindingName username password"); - System.exit(1); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aff6553c/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsTopicReceiver.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsTopicReceiver.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsTopicReceiver.java index 3a21f3c..4442ab1 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsTopicReceiver.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/mom/jms/JmsTopicReceiver.java @@ -17,54 +17,30 @@ package org.apache.logging.log4j.core.net.mom.jms; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.nio.charset.Charset; - -import org.apache.logging.log4j.core.net.server.JmsServer; - /** * Receives Topic messages that contain LogEvents. This implementation expects that all messages * are serialized log events. */ -public abstract class JmsTopicReceiver { +public class JmsTopicReceiver extends AbstractJmsReceiver { + + private JmsTopicReceiver() { + } /** * Main startup for the receiver. + * * @param args The command line arguments. * @throws Exception if an error occurs. */ public static void main(final String[] args) throws Exception { - if (args.length != 4) { - usage("Wrong number of arguments."); - } - - final String tcfBindingName = args[0]; - final String topicBindingName = args[1]; - final String username = args[2]; - final String password = args[3]; - final JmsServer server = new JmsServer(tcfBindingName, topicBindingName, username, password); - server.start(); - - final Charset enc = Charset.defaultCharset(); - final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc)); - // Loop until the word "exit" is typed - System.out.println("Type \"exit\" to quit JmsTopicReceiver."); - while (true) { - final String line = stdin.readLine(); - if (line == null || line.equalsIgnoreCase("exit")) { - System.out.println("Exiting. Kill the application if it does not exit " - + "due to daemon threads."); - server.stop(); - return; - } - } + final JmsTopicReceiver receiver = new JmsTopicReceiver(); + receiver.doMain(args); } - private static void usage(final String msg) { - System.err.println(msg); + @Override + protected void usage() { + System.err.println("Wrong number of arguments."); System.err.println("Usage: java " + JmsTopicReceiver.class.getName() + " TopicConnectionFactoryBindingName TopicBindingName username password"); - System.exit(1); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aff6553c/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java index abd0d5e..bc645d1 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java @@ -17,6 +17,10 @@ package org.apache.logging.log4j.core.net.server; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; import java.util.concurrent.atomic.AtomicReference; import javax.jms.JMSException; import javax.jms.Message; @@ -111,4 +115,23 @@ public class JmsServer extends LogEventListener implements MessageListener, Life return state.get() == State.STOPPED; } + /** + * Starts and runs this server until the user types "exit" into standard input. + * + * @throws IOException + */ + public void run() throws IOException { + this.start(); + System.out.println("Type \"exit\" to quit."); + final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset())); + while (true) { + final String line = stdin.readLine(); + if (line == null || line.equalsIgnoreCase("exit")) { + System.out.println("Exiting. Kill the application if it does not exit due to daemon threads."); + this.stop(); + return; + } + } + } + }
