You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by an...@apache.org on 2014/07/14 17:11:38 UTC

svn commit: r1610424 - in /tomee/tomee/branches/tomee-1.7.x/container/openejb-core: ./ src/main/java/org/apache/openejb/log/ src/main/java/org/apache/openejb/util/ src/test/java/org/apache/openejb/core/stateless/

Author: andygumbrecht
Date: Mon Jul 14 15:11:38 2014
New Revision: 1610424

URL: http://svn.apache.org/r1610424
Log:
Asynchronous logging.

Added:
    tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/LogStreamAsync.java
Modified:
    tomee/tomee/branches/tomee-1.7.x/container/openejb-core/pom.xml
    tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/log/SingleLineFormatter.java
    tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/Logger.java
    tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java

Modified: tomee/tomee/branches/tomee-1.7.x/container/openejb-core/pom.xml
URL: http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/pom.xml?rev=1610424&r1=1610423&r2=1610424&view=diff
==============================================================================
--- tomee/tomee/branches/tomee-1.7.x/container/openejb-core/pom.xml (original)
+++ tomee/tomee/branches/tomee-1.7.x/container/openejb-core/pom.xml Mon Jul 14 15:11:38 2014
@@ -306,7 +306,7 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
-          <forkMode>pertest</forkMode>
+          <reuseForks>false</reuseForks>
           <testNGArtifactName>none:none</testNGArtifactName>
           <argLine> <!-- "-javaagent:${basedir}/target/openejb-javaagent-${project.version}.jar" -->
             -Dopenejb.classloader.forced-skip=org.apache.openejb.jee.,org.apache.openejb.api.
@@ -687,7 +687,7 @@
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <configuration>
-              <forkMode>pertest</forkMode>
+            <reuseForks>false</reuseForks>
               <argLine>"-javaagent:${basedir}/target/openejb-javaagent-${project.version}.jar" -Xdebug
                 -Xnoagent -Djava.compiler=NONE
                 -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005

Modified: tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/log/SingleLineFormatter.java
URL: http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/log/SingleLineFormatter.java?rev=1610424&r1=1610423&r2=1610424&view=diff
==============================================================================
--- tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/log/SingleLineFormatter.java (original)
+++ tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/log/SingleLineFormatter.java Mon Jul 14 15:11:38 2014
@@ -31,7 +31,7 @@ public class SingleLineFormatter extends
         final StringBuilder sbuf = new StringBuilder();
         sbuf.append(record.getLevel().getLocalizedName());
         sbuf.append(" - ");
-        sbuf.append(formatMessage(record));
+        sbuf.append(this.formatMessage(record));
         sbuf.append(SEP);
         if (exception) {
             try {

Added: tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/LogStreamAsync.java
URL: http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/LogStreamAsync.java?rev=1610424&view=auto
==============================================================================
--- tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/LogStreamAsync.java (added)
+++ tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/LogStreamAsync.java Mon Jul 14 15:11:38 2014
@@ -0,0 +1,222 @@
+/*
+ * 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.openejb.util;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class LogStreamAsync implements LogStream, Closeable {
+
+    private static final LinkedBlockingQueue<Message> log = new LinkedBlockingQueue<Message>();
+    private static final Thread t = new Thread(new Consumer(LogStreamAsync.log), "LogStreamAsync.Thread");
+    private final LogStream ls;
+
+    private enum level {
+        fatal,
+        error,
+        warn,
+        info,
+        debug,
+        quit,
+    }
+
+    public LogStreamAsync(final LogStream ls) {
+        this.ls = ls;
+
+        if (!t.isAlive()) {
+            t.setDaemon(true);
+            t.start();
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        LogStreamAsync.log.clear();
+        try {
+            LogStreamAsync.log.put(new Message(this.ls, level.quit, ""));
+        } catch (final InterruptedException e) {
+            //Ignore
+        }
+    }
+
+    @Override
+    public boolean isFatalEnabled() {
+        return ls.isFatalEnabled();
+    }
+
+    @Override
+    public void fatal(final String message) {
+        this.log(level.fatal, message);
+    }
+
+    @Override
+    public void fatal(final String message, final Throwable t) {
+        this.log(level.fatal, message, t);
+    }
+
+    @Override
+    public boolean isErrorEnabled() {
+        return ls.isErrorEnabled();
+    }
+
+    @Override
+    public void error(final String message) {
+        this.log(level.error, message);
+    }
+
+    @Override
+    public void error(final String message, final Throwable t) {
+        this.log(level.error, message, t);
+    }
+
+    @Override
+    public boolean isWarnEnabled() {
+        return ls.isWarnEnabled();
+    }
+
+    @Override
+    public void warn(final String message) {
+        this.log(level.warn, message);
+    }
+
+    @Override
+    public void warn(final String message, final Throwable t) {
+        this.log(level.warn, message, t);
+    }
+
+    @Override
+    public boolean isInfoEnabled() {
+        return ls.isInfoEnabled();
+    }
+
+    @Override
+    public void info(final String message) {
+        this.log(level.info, message, null);
+    }
+
+    @Override
+    public void info(final String message, final Throwable t) {
+        this.log(level.info, message, null);
+    }
+
+    @Override
+    public boolean isDebugEnabled() {
+        return ls.isDebugEnabled();
+    }
+
+    @Override
+    public void debug(final String message) {
+        this.log(level.debug, message);
+    }
+
+    @Override
+    public void debug(final String message, final Throwable t) {
+        this.log(level.debug, message, t);
+    }
+
+    public void log(final level l, final String s) {
+        this.log(l, s, null);
+    }
+
+    public void log(final level l, final String s, final Throwable t) {
+        try {
+            LogStreamAsync.log.put(new Message(this.ls, l, s, t));
+        } catch (final InterruptedException e) {
+            //Ignore
+        }
+    }
+
+    private static class Message {
+
+        private final LogStream ls;
+        private final level l;
+        private final String s;
+        private final Throwable t;
+
+        private Message(final LogStream ls, final level l, final String s) {
+            this(ls, l, s, null);
+        }
+
+        private Message(final LogStream ls, final level l, final String s, final Throwable t) {
+            this.ls = ls;
+            this.l = l;
+            this.s = s;
+            this.t = t;
+        }
+    }
+
+    private static class Consumer implements Runnable {
+
+        private final BlockingQueue<Message> queue;
+
+        private Consumer(final BlockingQueue<Message> queue) {
+            this.queue = queue;
+        }
+
+        public void run() {
+
+            try {
+                Message msg;
+                while (!level.quit.equals((msg = queue.take()).l)) {
+                    final Throwable t = msg.t;
+
+                    if (null != t) {
+                        switch (msg.l) {
+                            case fatal:
+                                msg.ls.fatal(msg.s, t);
+                                break;
+                            case error:
+                                msg.ls.error(msg.s, t);
+                                break;
+                            case warn:
+                                msg.ls.warn(msg.s, t);
+                                break;
+                            case info:
+                                msg.ls.info(msg.s, t);
+                                break;
+                            case debug:
+                                msg.ls.debug(msg.s, t);
+                                break;
+                        }
+                    } else {
+                        switch (msg.l) {
+                            case fatal:
+                                msg.ls.fatal(msg.s);
+                                break;
+                            case error:
+                                msg.ls.error(msg.s);
+                                break;
+                            case warn:
+                                msg.ls.warn(msg.s);
+                                break;
+                            case info:
+                                msg.ls.info(msg.s);
+                                break;
+                            case debug:
+                                msg.ls.debug(msg.s);
+                                break;
+                        }
+                    }
+                }
+            } catch (final InterruptedException e) {
+                //Exit
+            }
+        }
+    }
+}

Modified: tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/Logger.java
URL: http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/Logger.java?rev=1610424&r1=1610423&r2=1610424&view=diff
==============================================================================
--- tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/Logger.java (original)
+++ tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/util/Logger.java Mon Jul 14 15:11:38 2014
@@ -40,6 +40,7 @@ public class Logger {
     }
 
     // don't return the instance since it needs to stay private but export which one is used to allow integration with other libs (as tomcat ;))
+    @SuppressWarnings("UnusedDeclaration")
     public static String delegateClass() {
         if (logStreamFactory == null) {
             throw new IllegalStateException("Call this method after having configured the logger");
@@ -271,8 +272,8 @@ public class Logger {
 
     public Logger(final LogCategory category, final LogStream logStream, final String baseName) {
         this.category = category;
-        this.logStream = logStream;
         this.baseName = baseName;
+        this.logStream = new LogStreamAsync(logStream);
     }
 
     public static Logger getInstance(final LogCategory category, final Class clazz) {
@@ -388,6 +389,7 @@ public class Logger {
         return logStream.isWarnEnabled();
     }
 
+    @SuppressWarnings("UnusedDeclaration")
     public boolean isLevelEnable(final String level) {
         if ("info".equals(level.toLowerCase())) {
             return isInfoEnabled();

Modified: tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java?rev=1610424&r1=1610423&r2=1610424&view=diff
==============================================================================
--- tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java (original)
+++ tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java Mon Jul 14 15:11:38 2014
@@ -93,8 +93,7 @@ public class StatelessInstanceManagerPoo
 
     public void testStatelessBeanRelease() throws Exception {
 
-
-        final int count = 10; //Strict pool can starve on more than 10
+        final int count = 60;
         final CountDownLatch invocations = new CountDownLatch(count);
         final InitialContext ctx = new InitialContext();
         final Runnable counterBeanLocal = new Runnable() {
@@ -122,10 +121,12 @@ public class StatelessInstanceManagerPoo
             thread.start();
         }
 
-        final boolean success = invocations.await(20000, TimeUnit.MILLISECONDS);
+        final boolean success = invocations.await(30000, TimeUnit.MILLISECONDS);
 
         assertTrue("invocations timeout -> invocations.getCount() == " + invocations.getCount(), success);
 
+        Thread.sleep(2000);
+
         assertEquals(count, discardedInstances.get());
 
     }
@@ -243,8 +244,6 @@ public class StatelessInstanceManagerPoo
     @Stateless
     public static class CounterBean implements Counter, RemoteCounter {
 
-
-
         private final int count;
 
         public CounterBean() {
@@ -262,7 +261,7 @@ public class StatelessInstanceManagerPoo
         public void explode(final CountDownLatch latch) {
             discardedInstances.incrementAndGet();
             try {
-                throw new NullPointerException("Test expected this null pointer");
+                throw new NullPointerException("Test expected this null pointer: " + latch.getCount());
             } finally {
                 latch.countDown();
             }