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 2020/05/19 08:45:48 UTC
[camel] branch master updated: CAMEL-15085: camel-sql - Make it
easier to set custom table name for JdbcMessageIdRepository
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new dc0ddee CAMEL-15085: camel-sql - Make it easier to set custom table name for JdbcMessageIdRepository
dc0ddee is described below
commit dc0ddee57a101d02eea3453be0b6faf13f5e0903
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 19 10:42:51 2020 +0200
CAMEL-15085: camel-sql - Make it easier to set custom table name for JdbcMessageIdRepository
---
.../camel-sql/src/main/docs/sql-component.adoc | 10 ++-
.../idempotent/jdbc/JdbcMessageIdRepository.java | 34 ++++++++-
...JdbcMessageIdRepositoryCustomTableNameTest.java | 82 ++++++++++++++++++++++
.../jdbc/customized-tablename-spring.xml | 52 ++++++++++++++
4 files changed, 173 insertions(+), 5 deletions(-)
diff --git a/components/camel-sql/src/main/docs/sql-component.adoc b/components/camel-sql/src/main/docs/sql-component.adoc
index 9cd130b..b920a59 100644
--- a/components/camel-sql/src/main/docs/sql-component.adoc
+++ b/components/camel-sql/src/main/docs/sql-component.adoc
@@ -541,6 +541,8 @@ your needs:
|createTableIfNotExists |true |Defines whether or not Camel should try to create the table if it
doesn't exist.
+|tableName | CAMEL_MESSAGEPROCESSED | To use a custom table name instead of the default name: CAMEL_MESSAGEPROCESSED.
+
|tableExistsString |SELECT 1 FROM CAMEL_MESSAGEPROCESSED WHERE 1 = 0 |This query is used to figure out whether the table already exists or
not. It must throw an exception to indicate the table doesn't exist.
@@ -565,6 +567,10 @@ It takes two parameter. This first one is the processor name (`String`) and
the second one is the message id (`String`).
|===
+The option `tableName` can be used to use the default SQL queries but with a different table name.
+However if you want to customize the SQL queries then you can configure each of them individually.
+
+
== Using the JDBC based aggregation repository
*Since Camel 2.6*
@@ -587,9 +593,7 @@ You must also set the `deadLetterUri` option so Camel knows where to
send the Exchange when the `maximumRedeliveries` was
hit.
-You can see some examples in the unit tests of camel-sql, for example
-https://svn.apache.org/repos/asf/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/processor/aggregate/jdbc/JdbcAggregateRecoverDeadLetterChannelTest.java[this
-test].
+You can see some examples in the unit tests of camel-sql, for example `JdbcAggregateRecoverDeadLetterChannelTest.java`
=== Database
diff --git a/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepository.java b/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepository.java
index 2e305a9..9438df9 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepository.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepository.java
@@ -31,7 +31,11 @@ import org.springframework.transaction.support.TransactionTemplate;
*/
public class JdbcMessageIdRepository extends AbstractJdbcMessageIdRepository {
+ private static final String DEFAULT_TABLENAME = "CAMEL_MESSAGEPROCESSED";
+
private boolean createTableIfNotExists = true;
+ private String tableName;
+
private String tableExistsString = "SELECT 1 FROM CAMEL_MESSAGEPROCESSED WHERE 1 = 0";
private String createString = "CREATE TABLE CAMEL_MESSAGEPROCESSED (processorName VARCHAR(255), messageId VARCHAR(100), createdAt TIMESTAMP)";
private String queryString = "SELECT COUNT(*) FROM CAMEL_MESSAGEPROCESSED WHERE processorName = ? AND messageId = ?";
@@ -53,7 +57,22 @@ public class JdbcMessageIdRepository extends AbstractJdbcMessageIdRepository {
public JdbcMessageIdRepository(JdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
super(jdbcTemplate, transactionTemplate);
}
-
+
+ @Override
+ protected void doInit() throws Exception {
+ super.doInit();
+
+ if (tableName != null) {
+ // update query strings from default table name to the new table name
+ tableExistsString = tableExistsString.replaceFirst(DEFAULT_TABLENAME, tableName);
+ createString = createString.replaceFirst(DEFAULT_TABLENAME, tableName);
+ queryString = queryString.replaceFirst(DEFAULT_TABLENAME, tableName);
+ insertString = insertString.replaceFirst(DEFAULT_TABLENAME, tableName);
+ deleteString = deleteString.replaceFirst(DEFAULT_TABLENAME, tableName);
+ clearString = clearString.replaceFirst(DEFAULT_TABLENAME, tableName);
+ }
+ }
+
@Override
protected void doStart() throws Exception {
super.doStart();
@@ -121,7 +140,18 @@ public class JdbcMessageIdRepository extends AbstractJdbcMessageIdRepository {
public void setTableExistsString(String tableExistsString) {
this.tableExistsString = tableExistsString;
}
-
+
+ public String getTableName() {
+ return tableName;
+ }
+
+ /**
+ * To use a custom table name instead of the default name: CAMEL_MESSAGEPROCESSED
+ */
+ public void setTableName(String tableName) {
+ this.tableName = tableName;
+ }
+
public String getCreateString() {
return createString;
}
diff --git a/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryCustomTableNameTest.java b/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryCustomTableNameTest.java
new file mode 100644
index 0000000..288a96a
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryCustomTableNameTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.idempotent.jdbc;
+
+import javax.sql.DataSource;
+import java.util.List;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+public class JdbcMessageIdRepositoryCustomTableNameTest extends CamelSpringTestSupport {
+
+ protected static final String SELECT_ALL_STRING = "SELECT messageId FROM FOOBAR WHERE processorName = ?";
+ protected static final String PROCESSOR_NAME = "myProcessorName";
+
+ protected JdbcTemplate jdbcTemplate;
+ protected DataSource dataSource;
+
+ @EndpointInject("mock:result")
+ protected MockEndpoint resultEndpoint;
+
+ @EndpointInject("mock:error")
+ protected MockEndpoint errorEndpoint;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+
+ dataSource = context.getRegistry().lookupByNameAndType("dataSource", DataSource.class);
+ jdbcTemplate = new JdbcTemplate(dataSource);
+ jdbcTemplate.afterPropertiesSet();
+ }
+
+ @Test
+ public void testDuplicateMessagesAreFilteredOut() throws Exception {
+ resultEndpoint.expectedBodiesReceived("one", "two", "three");
+ errorEndpoint.expectedMessageCount(0);
+
+ template.sendBodyAndHeader("direct:start", "one", "messageId", "1");
+ template.sendBodyAndHeader("direct:start", "two", "messageId", "2");
+ template.sendBodyAndHeader("direct:start", "one", "messageId", "1");
+ template.sendBodyAndHeader("direct:start", "two", "messageId", "2");
+ template.sendBodyAndHeader("direct:start", "one", "messageId", "1");
+ template.sendBodyAndHeader("direct:start", "three", "messageId", "3");
+
+ assertMockEndpointsSatisfied();
+
+ // all 3 messages should be in jdbc repo
+ List<String> receivedMessageIds = jdbcTemplate.queryForList(SELECT_ALL_STRING, String.class, PROCESSOR_NAME);
+
+ assertEquals(3, receivedMessageIds.size());
+ assertTrue(receivedMessageIds.contains("1"));
+ assertTrue(receivedMessageIds.contains("2"));
+ assertTrue(receivedMessageIds.contains("3"));
+ }
+
+ @Override
+ protected AbstractApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/processor/idempotent/jdbc/customized-tablename-spring.xml");
+ }
+}
\ No newline at end of file
diff --git a/components/camel-sql/src/test/resources/org/apache/camel/processor/idempotent/jdbc/customized-tablename-spring.xml b/components/camel-sql/src/test/resources/org/apache/camel/processor/idempotent/jdbc/customized-tablename-spring.xml
new file mode 100644
index 0000000..97adaa9
--- /dev/null
+++ b/components/camel-sql/src/test/resources/org/apache/camel/processor/idempotent/jdbc/customized-tablename-spring.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:camel="http://camel.apache.org/schema/spring"
+ xmlns:jdbc="http://www.springframework.org/schema/jdbc"
+ 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.xsd
+ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+ <jdbc:embedded-database id="dataSource" type="DERBY" />
+
+ <!-- START SNIPPET: e1 -->
+ <bean id="messageIdRepository" class="org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository">
+ <constructor-arg ref="dataSource" />
+ <constructor-arg value="myProcessorName" />
+ <property name="tableName" value="FOOBAR" />
+ </bean>
+ <!-- END SNIPPET: e1 -->
+
+ <camel:camelContext>
+ <camel:errorHandler id="deadLetterChannel" type="DeadLetterChannel" deadLetterUri="mock:error">
+ <camel:redeliveryPolicy maximumRedeliveries="0" maximumRedeliveryDelay="0" logStackTrace="false" />
+ </camel:errorHandler>
+
+ <camel:route id="JdbcMessageIdRepositoryTest" errorHandlerRef="deadLetterChannel">
+ <camel:from uri="direct:start" />
+ <camel:idempotentConsumer messageIdRepositoryRef="messageIdRepository">
+ <camel:header>messageId</camel:header>
+ <camel:to uri="mock:result" />
+ </camel:idempotentConsumer>
+ </camel:route>
+ </camel:camelContext>
+</beans>
\ No newline at end of file