camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r570675 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: Exchange.java impl/DefaultExchange.java impl/DefaultUnitOfWork.java spi/Synchronization.java spi/UnitOfWork.java
Date Wed, 29 Aug 2007 07:55:39 GMT
Author: jstrachan
Date: Wed Aug 29 00:55:38 2007
New Revision: 570675

URL: http://svn.apache.org/viewvc?rev=570675&view=rev
Log:
initial addition of a UnitOfWork helper object for implementing oncomplete/onfail hooks for
CAMEL-123

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Synchronization.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/UnitOfWork.java   (with
props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=570675&r1=570674&r2=570675&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Wed Aug 29
00:55:38 2007
@@ -16,6 +16,8 @@
  */
 package org.apache.camel;
 
+import org.apache.camel.spi.UnitOfWork;
+
 import java.util.Map;
 
 /**
@@ -158,4 +160,5 @@
      */
     void copyFrom(Exchange source);
 
+    UnitOfWork getUnitOfWork();
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=570675&r1=570674&r2=570675&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
Wed Aug 29 00:55:38 2007
@@ -16,15 +16,16 @@
  */
 package org.apache.camel.impl;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.util.UuidGenerator;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * A default implementation of {@link Exchange}
  * 
@@ -39,6 +40,7 @@
     private Message fault;
     private Throwable exception;
     private String exchangeId = DefaultExchange.DEFAULT_ID_GENERATOR.generateId();
+    private UnitOfWork unitOfWork;
 
     public DefaultExchange(CamelContext context) {
         this.context = context;
@@ -62,7 +64,6 @@
         setProperties(safeCopy(exchange.getProperties()));
 
         // this can cause strangeness if we copy, say, a FileMessage onto an FtpExchange
with overloaded getExchange() methods etc.
-
         safeCopy(getIn(), exchange, exchange.getIn());
         Message copyOut = exchange.getOut();
         if (copyOut != null) {
@@ -72,13 +73,9 @@
         if (copyFault != null) {
             safeCopy(getFault(), exchange, copyFault);
         }
-
-/*
-        setIn(safeCopy(exchange, exchange.getIn()));
-        setOut(safeCopy(exchange, exchange.getOut()));
-        setFault(safeCopy(exchange, exchange.getFault()));
-*/
         setException(exchange.getException());
+
+        unitOfWork = exchange.getUnitOfWork();
     }
 
     private static void safeCopy(Message message, Exchange exchange, Message that) {
@@ -211,6 +208,10 @@
 
     public void setExchangeId(String id) {
         this.exchangeId = id;
+    }
+
+    public UnitOfWork getUnitOfWork() {
+        return unitOfWork;
     }
 
     /**

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java?rev=570675&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
Wed Aug 29 00:55:38 2007
@@ -0,0 +1,100 @@
+/**
+ *
+ * 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.camel.impl;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.spi.UnitOfWork;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * The default implementation of {@link UnitOfWork}
+ *
+ * @version $Revision: 1.1 $
+ */
+public class DefaultUnitOfWork implements UnitOfWork {
+    private List<Synchronization> synchronizations;
+    private final Exchange exchange;
+    private List<AsyncCallback> asyncCallbacks;
+    private CountDownLatch latch;
+
+    public DefaultUnitOfWork(Exchange exchange) {
+        this.exchange = exchange;
+    }
+
+    public synchronized void addSynchronization(Synchronization synchronization) {
+        if (synchronizations == null) {
+            synchronizations = new ArrayList<Synchronization>();
+        }
+        synchronizations.add(synchronization);
+    }
+
+    public synchronized void removeSynchronization(Synchronization synchronization) {
+        if (synchronizations != null) {
+            synchronizations.remove(synchronization);
+        }
+    }
+
+
+    public void reset() {
+
+    }
+
+    public void doneSynchronous() {
+        if (isSynchronous()) {
+            onComplete();
+        }
+    }
+
+    public boolean isSynchronous() {
+        return asyncCallbacks == null || asyncCallbacks.isEmpty();
+    }
+
+    /**
+     * Register some asynchronous processing step
+     */
+    public synchronized AsyncCallback addAsyncStep() {
+        AsyncCallback answer = new AsyncCallback() {
+            public void done(boolean doneSynchronously) {
+                latch.countDown();    
+            }
+        };
+        if (latch == null) {
+            latch = new CountDownLatch(1);
+        }
+        else {
+            // TODO increment latch!
+        }
+        if (asyncCallbacks == null) {
+            asyncCallbacks = new ArrayList<AsyncCallback>();
+        }
+        asyncCallbacks.add(answer);
+        return answer;
+    }
+    protected synchronized void onComplete() {
+        if (synchronizations != null) {
+            for (Synchronization synchronization : synchronizations) {
+                synchronization.onComplete(exchange);
+            }
+        }
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Synchronization.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Synchronization.java?rev=570675&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Synchronization.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Synchronization.java
Wed Aug 29 00:55:38 2007
@@ -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.camel.spi;
+
+import org.apache.camel.Component;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+/**
+ * Provides a hook for custom {@link Processor} or {@link Component} instances to respond
to
+ * completed or failed processing of an {@link Exchange} rather like Spring's
+ * <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/transaction/support/TransactionSynchronization.html">TransactionSynchronization</a>
+ *
+ * @version $Revision: 1.1 $
+ */
+public interface Synchronization {
+
+    /**
+     * Called when the processing of the message exchange is complete
+     *
+     * @param exchange the excahnge being processed
+     */
+    void onComplete(Exchange exchange);
+
+    /**
+     * Called when the processing of the message exchange has failed for some reason. 
+     * The exception which caused the problem is in {@link Exchange#getException()} and
+     * there could be a fault message via {@link Exchange#getFault()}
+     *
+     * @param exchange the excahnge being processed
+     */
+    void onFailure(Exchange exchange);
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Synchronization.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/UnitOfWork.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/UnitOfWork.java?rev=570675&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/UnitOfWork.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/UnitOfWork.java Wed
Aug 29 00:55:38 2007
@@ -0,0 +1,44 @@
+/**
+ *
+ * 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.camel.spi;
+
+import org.apache.camel.Exchange;
+
+/**
+ * An object representing the unit of work processing an {@link Exchange}
+ * which allows the use of {@link Synchronization} hooks. This object might map one-to-one
with
+ * a transaction in JPA or Spring; or might not.
+ *
+ * @version $Revision: 1.1 $
+ */
+public interface UnitOfWork {
+
+    /**
+     * Adds a synchronization hook
+     *
+     * @param synchronization
+     */
+    void addSynchronization(Synchronization synchronization);
+
+    /**
+     * Removes a synchronization hook
+     *
+     * @param synchronization
+     */
+    void removeSynchronization(Synchronization synchronization);
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/UnitOfWork.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message