servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject [1/3] git commit: SM-2256: make jms appender async Thanks to Bart for the pull request!
Date Thu, 03 Apr 2014 23:32:13 GMT
Repository: servicemix
Updated Branches:
  refs/heads/master 111751f59 -> 9a139cedf
  refs/heads/servicemix-5.0.x e564caf84 -> 9741d69a9
  refs/heads/servicemix-5.1.x 4e01a14e1 -> f9e5d548f


SM-2256: make jms appender async
Thanks to Bart for the pull request!


Project: http://git-wip-us.apache.org/repos/asf/servicemix/repo
Commit: http://git-wip-us.apache.org/repos/asf/servicemix/commit/9741d69a
Tree: http://git-wip-us.apache.org/repos/asf/servicemix/tree/9741d69a
Diff: http://git-wip-us.apache.org/repos/asf/servicemix/diff/9741d69a

Branch: refs/heads/servicemix-5.0.x
Commit: 9741d69a9751bdb7bce9a95b1859d9c25d598dce
Parents: e564caf
Author: Bart Horré <bart@anova.be>
Authored: Fri Mar 28 15:32:55 2014 +0100
Committer: Gert Vanthienen <gertv@apache.org>
Committed: Thu Apr 3 11:18:23 2014 +0200

----------------------------------------------------------------------
 .../servicemix/logging/jms/JMSAppender.java     | 56 ++++++++++++++++----
 .../resources/OSGI-INF/blueprint/config.xml     | 11 ++--
 .../servicemix/logging/jms/JMSAppenderTest.java |  2 +-
 .../servicemix/logging/jms/MockEvents.java      |  2 +-
 4 files changed, 55 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/servicemix/blob/9741d69a/logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
----------------------------------------------------------------------
diff --git a/logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
b/logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
index ccc2006..b765e0f 100644
--- a/logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
+++ b/logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
@@ -22,14 +22,17 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.jms.*;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 public class JMSAppender implements PaxAppender {
-
+    private static final String PACKAGE = JMSAppender.class.getPackage().getName();
     private static final transient Logger LOG = LoggerFactory.getLogger(JMSAppender.class);
 
     private static final String DEFAULT_EVENT_FORMAT = "default";
     private static final String LOGSTASH_EVENT_FORMAT = "logstash";
 
+    private boolean serviceAvailable;
 
     private ConnectionFactory jmsConnectionFactory;
     private Connection connection;
@@ -39,21 +42,56 @@ public class JMSAppender implements PaxAppender {
 
     private LoggingEventFormat format = new DefaultLoggingEventFormat();
 
+    private  ExecutorService executor = Executors.newSingleThreadExecutor();
+
     public void close() {
         closeJMSResources();
     }
-
-    public void doAppend(PaxLoggingEvent paxLoggingEvent) {
+    public void onBind(ConnectionFactory service){
+        jmsConnectionFactory = service;
         try {
-            // Send message to the destination
-            TextMessage message = getOrCreateSession().createTextMessage();
-            message.setText(format.toString(paxLoggingEvent));
-            getOrCreatePublisher().send(message);
+            connection = getOrCreateConnection();
+            session = getOrCreateSession();
+            producer = getOrCreatePublisher();
+            serviceAvailable = true;
         } catch (JMSException e) {
-            LOG.warn("Exception caught while sending log event - reinitializing JMS resources
to recover", e);
-            closeJMSResources();
+            serviceAvailable = false;
+        }
+    }
+    public void onUnbind(ConnectionFactory service){
+        serviceAvailable = false;
+        closeJMSResources();
+    }
 
+    public void doAppend(final PaxLoggingEvent paxLoggingEvent) {
+        if (exclude(paxLoggingEvent) || !serviceAvailable) {
+            return;
         }
+            Runnable worker = new Runnable() {
+                public void run() {
+                    if(serviceAvailable){
+                        try {
+                            // Send message to the destination
+                            TextMessage message = getOrCreateSession().createTextMessage();
+                            message.setText(format.toString(paxLoggingEvent));
+                            MessageProducer producer = getOrCreatePublisher();
+                            producer.send(message);
+                        } catch (JMSException e) {
+                            LOG.warn("Exception caught while sending log event - reinitializing
JMS resources to recover", e);
+                            close();
+                        }
+                    }
+                }
+            };
+            executor.execute(worker);
+    }
+
+    private static boolean exclude(PaxLoggingEvent event) {
+        return startsWith(event.getLoggerName(), PACKAGE);
+    }
+
+    private static boolean startsWith(String string, String start) {
+        return string != null && string.startsWith(start);
     }
 
     public void setJmsConnectionFactory(ConnectionFactory jmsConnectionFactory) {

http://git-wip-us.apache.org/repos/asf/servicemix/blob/9741d69a/logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
----------------------------------------------------------------------
diff --git a/logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml b/logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
index eafbc91..a507f8e 100644
--- a/logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
+++ b/logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
@@ -22,19 +22,20 @@
     <cm:property-placeholder persistent-id="org.apache.servicemix.logging.jms" update-strategy="reload">
         <cm:default-properties>
             <cm:property name="destinationName" value="Logging.Events"/>
-            <cm:property name="format" value="default"/>
+            <cm:property name="format" value="logstash"/>
         </cm:default-properties>
     </cm:property-placeholder>
 
-    <!-- Need OSGI JMS Connection Factory Service exposed  -->
-    <reference id="jmsConnectionFactory" interface="javax.jms.ConnectionFactory" filter="(transacted=false)"
/>
-
     <bean id="appender" class="org.apache.servicemix.logging.jms.JMSAppender" destroy-method="close">
-        <property name="jmsConnectionFactory" ref="jmsConnectionFactory"/>
         <property name="destinationName" value="${destinationName}" />
         <property name="format" value="${format}"/>
     </bean>
 
+    <!-- Need OSGI JMS Connection Factory Service exposed  -->
+    <reference-list id="jmsConnectionFactory" interface="javax.jms.ConnectionFactory"
filter="(transacted=false)">
+        <reference-listener ref="appender" bind-method="onBind" unbind-method="onUnbind"
/>
+    </reference-list>
+
     <service ref="appender" interface="org.ops4j.pax.logging.spi.PaxAppender">
         <service-properties>
             <entry key="org.ops4j.pax.logging.appender.name" value="JMSLogAppender"/>

http://git-wip-us.apache.org/repos/asf/servicemix/blob/9741d69a/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
----------------------------------------------------------------------
diff --git a/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
b/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
index 18acb2e..20b75b7 100644
--- a/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
+++ b/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
@@ -48,8 +48,8 @@ public class JMSAppenderTest extends CamelTestSupport {
     @Before
     public void setupBrokerAndAppender() throws Exception {
         appender = new JMSAppender();
-        appender.setJmsConnectionFactory(new ActiveMQConnectionFactory(broker.getVmConnectorURI().toString()
+ "?create=false"));
         appender.setDestinationName(EVENTS_TOPIC);
+        appender.onBind(new ActiveMQConnectionFactory(broker.getVmConnectorURI().toString()
+ "?create=false"));
     }
 
     @AfterClass

http://git-wip-us.apache.org/repos/asf/servicemix/blob/9741d69a/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
----------------------------------------------------------------------
diff --git a/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
b/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
index 8efd7de..21dbd66 100644
--- a/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
+++ b/logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
@@ -27,7 +27,7 @@ import org.ops4j.pax.logging.spi.PaxLoggingEvent;
  */
 public class MockEvents {
 
-    public static final String LOGGER_NAME = MockEvents.class.getName();
+    public static final String LOGGER_NAME = "my.test.logger";
     public static final String LOG_MESSAGE = "Important message about your application!";
 
     public static final String LOG_PROPERTY_ID = "property.id";


Mime
View raw message