This is an automated email from the ASF dual-hosted git repository. zbendhiba pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus-examples.git
commit 91150d4aa1fa815537b16da00f11f60c7dc754ed Author: James Netherton <[email protected]> AuthorDate: Thu Sep 15 12:58:00 2022 +0100 Convert JDBC XML routes to Java DSL --- .../jdbc/{CamelResource.java => JdbcResource.java} | 15 ++------ .../src/main/java/org/acme/jdbc/JdbcRoutes.java | 34 +++++++++++++++++ .../src/main/resources/application.properties | 9 ++--- .../src/main/resources/routes/camel-routes.xml | 44 ---------------------- 4 files changed, 40 insertions(+), 62 deletions(-) diff --git a/jdbc-datasource/src/main/java/org/acme/jdbc/CamelResource.java b/jdbc-datasource/src/main/java/org/acme/jdbc/JdbcResource.java similarity index 77% rename from jdbc-datasource/src/main/java/org/acme/jdbc/CamelResource.java rename to jdbc-datasource/src/main/java/org/acme/jdbc/JdbcResource.java index f5ea1c7..2496cfa 100644 --- a/jdbc-datasource/src/main/java/org/acme/jdbc/CamelResource.java +++ b/jdbc-datasource/src/main/java/org/acme/jdbc/JdbcResource.java @@ -19,7 +19,6 @@ package org.acme.jdbc; import java.sql.Connection; import java.sql.Statement; -import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Observes; import javax.inject.Inject; @@ -30,26 +29,18 @@ import io.quarkus.runtime.StartupEvent; import org.apache.camel.CamelContext; @ApplicationScoped -public class CamelResource { +public class JdbcResource { @Inject @DataSource("camel-ds") AgroalDataSource dataSource; void startup(@Observes StartupEvent event, CamelContext context) throws Exception { - context.getRouteController().startAllRoutes(); - } - - @PostConstruct - void postConstruct() throws Exception { try (Connection con = dataSource.getConnection()) { try (Statement statement = con.createStatement()) { con.setAutoCommit(true); - try { - statement.execute("drop table camel"); - } catch (Exception ignored) { - } - statement.execute("create table camel (id serial primary key, timestamp varchar(255))"); + statement.execute("DROP TABLE IF EXISTS camel"); + statement.execute("CREATE TABLE camel (id SERIAL PRIMARY KEY, timestamp VARCHAR(255))"); } } } diff --git a/jdbc-datasource/src/main/java/org/acme/jdbc/JdbcRoutes.java b/jdbc-datasource/src/main/java/org/acme/jdbc/JdbcRoutes.java new file mode 100644 index 0000000..c2fb0a2 --- /dev/null +++ b/jdbc-datasource/src/main/java/org/acme/jdbc/JdbcRoutes.java @@ -0,0 +1,34 @@ +/* + * 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.acme.jdbc; + +import org.apache.camel.builder.RouteBuilder; + +public class JdbcRoutes extends RouteBuilder { + @Override + public void configure() throws Exception { + from("timer://insertCamel?period=1000") + .log("Inserting Camel ${messageTimestamp}") + .setBody().simple("INSERT INTO Camel (timestamp) VALUES (${messageTimestamp})") + .to("jdbc:default") + .log("Inserted Camel ${messageTimestamp}") + .setBody().simple("SELECT * FROM Camel") + .to("jdbc:default") + .log("We have ${header[CamelJdbcRowCount]} camels in the database.") + .log("Camels found: ${body}"); + } +} diff --git a/jdbc-datasource/src/main/resources/application.properties b/jdbc-datasource/src/main/resources/application.properties index f840633..b178e02 100644 --- a/jdbc-datasource/src/main/resources/application.properties +++ b/jdbc-datasource/src/main/resources/application.properties @@ -20,16 +20,13 @@ quarkus.banner.enabled = false quarkus.log.file.enable = true -camel.main.routes-include-pattern = file:src/main/resources/routes/camel-routes.xml - - -#Default datastore +#Default DataSource quarkus.datasource.camel-ds.db-kind=h2 -#If you want to have more than one datastore, you can use an identifier as this: +#If you want to have more than one DataSource, you can use an identifier as this: #quarkus.datasource.$identifier.db-kind=h2 #Then use it on the route by name -#<to uri="jdbc:$identifier"/> +#.to("jdbc:$identifier") #Configure the following section to use a maven profile (called prod) #configured database (using postgresql on this case) diff --git a/jdbc-datasource/src/main/resources/routes/camel-routes.xml b/jdbc-datasource/src/main/resources/routes/camel-routes.xml deleted file mode 100644 index f8aa2df..0000000 --- a/jdbc-datasource/src/main/resources/routes/camel-routes.xml +++ /dev/null @@ -1,44 +0,0 @@ -<?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. - ---> - -<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns="http://camel.apache.org/schema/spring" - xsi:schemaLocation=" - http://camel.apache.org/schema/spring - https://camel.apache.org/schema/spring/camel-spring.xsd"> - - <route id="jdbc-datasource-route" - autoStartup="false"> - <from uri="timer://foo?period=1000"/> - <log message="Inserting Camel ${messageTimestamp}"/> - <setBody> - <simple>INSERT INTO Camel (timestamp) VALUES (${messageTimestamp})</simple> - </setBody> - <to uri="jdbc:default"/> - <log message="Inserted camel: ${messageTimestamp}"/> - <setBody> - <simple>SELECT * FROM Camel</simple> - </setBody> - <to uri="jdbc:default"/> - <log message="We have ${header[CamelJdbcRowCount]} camels in the database."/> - <log message="Camels found: ${body}"/> - </route> - -</routes> \ No newline at end of file
