Repository: camel
Updated Branches:
  refs/heads/master 50d029ae2 -> 7509c117b


CAMEL-9849: camel-sql - Add support for OutputType=StreamList in the consumer


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7509c117
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7509c117
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7509c117

Branch: refs/heads/master
Commit: 7509c117b80570eb8460c7ea19b0a76030e87854
Parents: 50d029a
Author: Claus Ibsen <[email protected]>
Authored: Sun May 1 08:10:33 2016 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Sun May 1 08:10:33 2016 +0200

----------------------------------------------------------------------
 .../camel/component/sql/DefaultSqlEndpoint.java |   3 +-
 .../apache/camel/component/sql/SqlConsumer.java |  41 ++++++-
 .../SqlConsumerOutputTypeStreamListTest.java    | 120 +++++++++++++++++++
 3 files changed, 156 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7509c117/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
index abcfc95..dfe70ab 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
@@ -95,8 +95,7 @@ public abstract class DefaultSqlEndpoint extends 
DefaultPollingEndpoint {
             + "c) If the outputClass is set, then it will convert the query 
result into an Java bean object by calling all the setters that match the 
column names."
             + "It will assume your class has a default constructor to create 
instance with."
             + "d) If the query resulted in more than one rows, it throws an 
non-unique result exception."
-            + "StreamList can only be used by the producer that streams the 
result of the query using an Iterator. This can be used with the Splitter EIP 
in streaming"
-            + " mode to process the ResultSet in streaming fashion.")
+            + "StreamList streams the result of the query using an Iterator. 
This can be used with the Splitter EIP in streaming mode to process the 
ResultSet in streaming fashion.")
     private SqlOutputType outputType = SqlOutputType.SelectList;
     @UriParam(description = "Specify the full package and class name to use as 
conversion when outputType=SelectOne.")
     private String outputClass;

http://git-wip-us.apache.org/repos/asf/camel/blob/7509c117/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlConsumer.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlConsumer.java
index 74bad30..2c801cf 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlConsumer.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlConsumer.java
@@ -16,9 +16,11 @@
  */
 package org.apache.camel.component.sql;
 
+import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Queue;
@@ -37,7 +39,9 @@ import 
org.springframework.jdbc.core.PreparedStatementCallback;
 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 import org.springframework.jdbc.core.namedparam.SqlParameterSource;
 
+import static org.springframework.jdbc.support.JdbcUtils.closeConnection;
 import static org.springframework.jdbc.support.JdbcUtils.closeResultSet;
+import static org.springframework.jdbc.support.JdbcUtils.closeStatement;
 
 public class SqlConsumer extends ScheduledBatchPollingConsumer {
 
@@ -107,17 +111,33 @@ public class SqlConsumer extends 
ScheduledBatchPollingConsumer {
         pendingExchanges = 0;
 
         final String preparedQuery = 
sqlPrepareStatementStrategy.prepareQuery(resolvedQuery, 
getEndpoint().isAllowNamedParameters(), null);
+
+        // special for processing stream list (batch not supported)
+//        SqlOutputType outputType = getEndpoint().getOutputType();
+//        if (outputType == SqlOutputType.StreamList) {
+//            return pollStreamList(resolvedQuery, preparedQuery);
+//        }
+
+        log.trace("poll: {}", preparedQuery);
         final PreparedStatementCallback<Integer> callback = new 
PreparedStatementCallback<Integer>() {
             @Override
-            public Integer doInPreparedStatement(PreparedStatement 
preparedStatement) throws SQLException, DataAccessException {
+            public Integer doInPreparedStatement(PreparedStatement ps) throws 
SQLException, DataAccessException {
                 Queue<DataHolder> answer = new LinkedList<DataHolder>();
 
                 log.debug("Executing query: {}", preparedQuery);
-                ResultSet rs = preparedStatement.executeQuery();
+                ResultSet rs = ps.executeQuery();
                 SqlOutputType outputType = getEndpoint().getOutputType();
+                boolean closeEager = true;
                 try {
                     log.trace("Got result list from query: {}, outputType={}", 
rs, outputType);
-                    if (outputType == SqlOutputType.SelectList) {
+                    if (outputType == SqlOutputType.StreamList) {
+                        ResultSetIterator data = 
getEndpoint().queryForStreamList(ps.getConnection(), ps, rs);
+                        // only process if we have data
+                        if (data.hasNext()) {
+                            addListToQueue(data, answer);
+                            closeEager = false;
+                        }
+                    } else if (outputType == SqlOutputType.SelectList) {
                         List<?> data = getEndpoint().queryForList(rs, true);
                         addListToQueue(data, answer);
                     } else if (outputType == SqlOutputType.SelectOne) {
@@ -129,15 +149,24 @@ public class SqlConsumer extends 
ScheduledBatchPollingConsumer {
                         throw new IllegalArgumentException("Invalid 
outputType=" + outputType);
                     }
                 } finally {
-                    closeResultSet(rs);
+                    if (closeEager) {
+                        closeResultSet(rs);
+                    }
                 }
 
                 // process all the exchanges in this batch
                 try {
-                    int rows = processBatch(CastUtils.cast(answer));
-                    return rows;
+                    if (answer.isEmpty()) {
+                        // no data
+                        return 0;
+                    } else {
+                        int rows = processBatch(CastUtils.cast(answer));
+                        return rows;
+                    }
                 } catch (Exception e) {
                     throw ObjectHelper.wrapRuntimeCamelException(e);
+                } finally {
+                    closeResultSet(rs);
                 }
             }
         };

http://git-wip-us.apache.org/repos/asf/camel/blob/7509c117/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlConsumerOutputTypeStreamListTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlConsumerOutputTypeStreamListTest.java
 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlConsumerOutputTypeStreamListTest.java
new file mode 100644
index 0000000..d59d141
--- /dev/null
+++ 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlConsumerOutputTypeStreamListTest.java
@@ -0,0 +1,120 @@
+/**
+ * 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.component.sql;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+
+public class SqlConsumerOutputTypeStreamListTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+
+    @Before
+    public void setUp() throws Exception {
+        db = new EmbeddedDatabaseBuilder()
+                
.setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase.sql").build();
+
+        super.setUp();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        db.shutdown();
+    }
+
+    @Test
+    public void testReturnAnIterator() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(1);
+
+        context.startRoute("route1");
+
+        mock.assertIsSatisfied();
+        assertThat(resultBodyAt(mock, 0), instanceOf(Iterator.class));
+    }
+
+    @Test
+    public void testSplit() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(3);
+
+        context.startRoute("route2");
+
+        mock.assertIsSatisfied();
+        assertThat(resultBodyAt(mock, 0), instanceOf(Map.class));
+        assertThat(resultBodyAt(mock, 1), instanceOf(Map.class));
+        assertThat(resultBodyAt(mock, 2), instanceOf(Map.class));
+    }
+
+    @Test
+    public void testSplitWithModel() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(3);
+
+        context.startRoute("route3");
+
+        mock.assertIsSatisfied();
+        assertThat(resultBodyAt(mock, 0), instanceOf(ProjectModel.class));
+        assertThat(resultBodyAt(mock, 1), instanceOf(ProjectModel.class));
+        assertThat(resultBodyAt(mock, 2), instanceOf(ProjectModel.class));
+    }
+
+    private Object resultBodyAt(MockEndpoint result, int index) {
+        return result.assertExchangeReceived(index).getIn().getBody();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                getContext().getComponent("sql", 
SqlComponent.class).setDataSource(db);
+
+                from("sql:select * from projects order by 
id?outputType=StreamList").routeId("route1").noAutoStartup()
+                        .to("log:stream")
+                        .to("mock:result");
+
+                from("sql:select * from projects order by 
id?outputType=StreamList").routeId("route2").noAutoStartup()
+                        .to("log:stream")
+                        .split(body()).streaming()
+                            .to("log:row")
+                            .to("mock:result")
+                        .end();
+
+                from("sql:select * from projects order by 
id?outputType=StreamList&outputClass=org.apache.camel.component.sql.ProjectModel").routeId("route3").noAutoStartup()
+                        .to("log:stream")
+                        .split(body()).streaming()
+                            .to("log:row")
+                            .to("mock:result")
+                        .end();
+            }
+        };
+    }
+}

Reply via email to