You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/12/02 19:03:47 UTC

svn commit: r722548 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/component/file/ components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/ components/camel-jpa/src/test/resources/ components/camel-jpa/src/test/resource...

Author: davsclaus
Date: Tue Dec  2 10:03:46 2008
New Revision: 722548

URL: http://svn.apache.org/viewvc?rev=722548&view=rev
Log:
CAMEL-1099: Added jpa based file consumer idempotent repository example and test

Added:
    activemq/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/FileConsumerJpaIdempotentTest.java
    activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml   (contents, props changed)
      - copied, changed from r722385, activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    activemq/camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml
    activemq/camel/trunk/components/camel-jpa/src/test/resources/log4j.properties
    activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?rev=722548&r1=722547&r2=722548&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java Tue Dec  2 10:03:46 2008
@@ -250,8 +250,14 @@
      */
     protected boolean validateFile(File file) {
         if (!matchFile(file)) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("File did not match. Will skip this file: " + file);
+            }
             return false;
         } else  if (endpoint.isIdempotent() && !endpoint.getIdempotentRepository().add(file.getName())) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("FileConsumer is idempotent and the file has been consumed before. Will skip this file: " + file);
+            }
             // skip as we have already processed it
             return false;
         }

Added: activemq/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/FileConsumerJpaIdempotentTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/FileConsumerJpaIdempotentTest.java?rev=722548&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/FileConsumerJpaIdempotentTest.java (added)
+++ activemq/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/FileConsumerJpaIdempotentTest.java Tue Dec  2 10:03:46 2008
@@ -0,0 +1,117 @@
+/**
+ * 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.processor.jpa;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.idempotent.jpa.MessageProcessed;
+import org.apache.camel.spring.SpringCamelContext;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.TransactionCallback;
+import org.springframework.transaction.support.TransactionTemplate;
+
+/**
+ * Unit test using jpa idempotent repository for the file consumer.
+ */
+public class FileConsumerJpaIdempotentTest extends ContextTestSupport {
+
+    protected static final String SELECT_ALL_STRING = "select x from " + MessageProcessed.class.getName() + " x where x.processorName = ?1";
+    protected static final String PROCESSOR_NAME = "FileConsumer";
+
+    protected ApplicationContext applicationContext;
+    protected JpaTemplate jpaTemplate;
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        applicationContext = new ClassPathXmlApplicationContext("org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml");
+        return SpringCamelContext.springCamelContext(applicationContext);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        cleanupRepository();
+        deleteDirectory("target/idempotent");
+        template.sendBodyAndHeader("file://target/idempotent/", "Hello World", FileComponent.HEADER_FILE_NAME, "report.txt");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("file://target/idempotent/?idempotent=true&idempotentRepositoryRef=jpaStore&moveNamePrefix=done/").to("mock:result");
+            }
+        };
+    }
+
+    protected void cleanupRepository() {
+        jpaTemplate = (JpaTemplate)applicationContext.getBean("jpaTemplate", JpaTemplate.class);
+
+        TransactionTemplate transactionTemplate = new TransactionTemplate();
+        transactionTemplate.setTransactionManager(new JpaTransactionManager(jpaTemplate.getEntityManagerFactory()));
+        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
+
+        transactionTemplate.execute(new TransactionCallback() {
+            public Object doInTransaction(TransactionStatus arg0) {
+                List list = jpaTemplate.find(SELECT_ALL_STRING, PROCESSOR_NAME);
+                for (Object item : list) {
+                    jpaTemplate.remove(item);
+                }
+                jpaTemplate.flush();
+                return Boolean.TRUE;
+            }
+        });
+    }
+
+    public void testFileConsumerJpaIdempotent() throws Exception {
+        // consume the file the first time
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedMessageCount(1);
+
+        assertMockEndpointsSatisfied();
+
+        Thread.sleep(1000);
+
+        // reset mock and set new expectations
+        mock.reset();
+        mock.expectedMessageCount(0);
+
+        // move file back
+        File file = new File("target/idempotent/done/report.txt");
+        File renamed = new File("target/idempotent/report.txt");
+        file = file.getAbsoluteFile();
+        file.renameTo(renamed.getAbsoluteFile());
+
+        // should NOT consume the file again, let 2 secs pass to let the consumer try to consume it but it should not
+        Thread.sleep(2000);
+        assertMockEndpointsSatisfied();
+    }
+
+
+}

Modified: activemq/camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml?rev=722548&r1=722547&r2=722548&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml (original)
+++ activemq/camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml Tue Dec  2 10:03:46 2008
@@ -57,6 +57,7 @@
     </properties>
   </persistence-unit>
 
+    <!-- START SNIPPET: e1 -->
   <persistence-unit name="idempotentDb" transaction-type="RESOURCE_LOCAL">
     <class>org.apache.camel.processor.idempotent.jpa.MessageProcessed</class>
 
@@ -67,4 +68,5 @@
       <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
     </properties>
   </persistence-unit>
+    <!-- END SNIPPET: e1 -->
 </persistence>

Modified: activemq/camel/trunk/components/camel-jpa/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jpa/src/test/resources/log4j.properties?rev=722548&r1=722547&r2=722548&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jpa/src/test/resources/log4j.properties (original)
+++ activemq/camel/trunk/components/camel-jpa/src/test/resources/log4j.properties Tue Dec  2 10:03:46 2008
@@ -18,9 +18,9 @@
 #
 # The logging properties used for eclipse testing, We want to see debug output on the console.
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
 
-log4j.logger.org.apache.camel=WARN
+log4j.logger.org.apache.camel=DEBUG
 log4j.logger.org.springframework=WARN
 #log4j.logger.org.apache.activemq=DEBUG
 
@@ -29,3 +29,9 @@
 log4j.appender.out.layout=org.apache.log4j.PatternLayout
 log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
 #log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+
+# File appender
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.file.file=target/camel-jpa-test.log
\ No newline at end of file

Copied: activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml (from r722385, activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml?p2=activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml&p1=activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml&r1=722385&r2=722548&rev=722548&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml (original)
+++ activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml Tue Dec  2 10:03:46 2008
@@ -19,20 +19,17 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
-  <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
-    <property name="transactionManager">
-      <bean class="org.springframework.orm.jpa.JpaTransactionManager">
-        <property name="entityManagerFactory" ref="entityManagerFactory"/>
-      </bean>
-    </property>
-  </bean>
+  <import resource="spring.xml"/>
 
-  <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
-    <property name="entityManagerFactory" ref="entityManagerFactory"/>
-  </bean>
-
-  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
-    <property name="persistenceUnitName" value="idempotentDb"/>
-  </bean>
+    <!-- START SNIPPET: jpaStore -->
+    <!-- we define our jpa based idempotent repository we want to use in the file consumer -->
+    <bean id="jpaStore" class="org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository">
+        <!-- Here we refer to the spring jpaTemplate -->
+        <constructor-arg index="0" ref="jpaTemplate"/>
+        <!-- This 2nd parameter is the name  (= a cateogry name).
+             You can have different repositories with different names -->
+        <constructor-arg index="1" value="FileConsumer"/>
+    </bean>
+    <!-- END SNIPPET: jpaStore -->
 
 </beans>

Propchange: activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml?rev=722548&r1=722547&r2=722548&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml (original)
+++ activemq/camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml Tue Dec  2 10:03:46 2008
@@ -19,20 +19,24 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
-  <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
-    <property name="transactionManager">
-      <bean class="org.springframework.orm.jpa.JpaTransactionManager">
-        <property name="entityManagerFactory" ref="entityManagerFactory"/>
-      </bean>
-    </property>
-  </bean>
+    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
+        <property name="transactionManager">
+            <bean class="org.springframework.orm.jpa.JpaTransactionManager">
+                <property name="entityManagerFactory" ref="entityManagerFactory"/>
+            </bean>
+        </property>
+    </bean>
 
-  <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
-    <property name="entityManagerFactory" ref="entityManagerFactory"/>
-  </bean>
+    <!-- START SNIPPET: e1 -->
+    <!-- this is standard spring JPA configuration -->
+    <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
+        <property name="entityManagerFactory" ref="entityManagerFactory"/>
+    </bean>
 
-  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
-    <property name="persistenceUnitName" value="idempotentDb"/>
-  </bean>
+    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
+        <!-- we use idempotentDB as the persitence unit name defined in the persistence.xml file -->
+        <property name="persistenceUnitName" value="idempotentDb"/>
+    </bean>
+    <!-- END SNIPPET: e1 -->
 
 </beans>