From f653433c7ef08456e9096983ad72c4b3cc05eb91 Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Sat, 19 Nov 2011 00:51:41 +0530 Subject: [PATCH] Initial commit for the JDBC project --- basic/jdbc/README.txt | 62 ++++++++++++++ basic/jdbc/pom.xml | 83 +++++++++++++++++++ .../integration/samples/jdbc/Gender.java | 36 ++++++++ .../integration/samples/jdbc/Person.java | 64 ++++++++++++++ .../jdbc/src/main/resources/Person_Derby.sql | 6 ++ .../jdbc/OutboundChannelAdapterTest.java | 61 ++++++++++++++ .../resources/jdbcOutboundAdapterConfig.xml | 38 +++++++++ .../jdbc/src/test/resources/log4j.properties | 8 ++ 8 files changed, 358 insertions(+) create mode 100644 basic/jdbc/README.txt create mode 100644 basic/jdbc/pom.xml create mode 100644 basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Gender.java create mode 100644 basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Person.java create mode 100644 basic/jdbc/src/main/resources/Person_Derby.sql create mode 100644 basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundChannelAdapterTest.java create mode 100644 basic/jdbc/src/test/resources/jdbcOutboundAdapterConfig.xml create mode 100644 basic/jdbc/src/test/resources/log4j.properties diff --git a/basic/jdbc/README.txt b/basic/jdbc/README.txt new file mode 100644 index 00000000..436bd1e8 --- /dev/null +++ b/basic/jdbc/README.txt @@ -0,0 +1,62 @@ +The project contains sample JDBC adapters using Spring integration +JDBC Adapters are used to connectthe database systems from Spring integration application + + +###########################Setting up derby########################### + +If you intend to use some other database, skip this section and goto outbound channel adapters section + +1. Goto http://db.apache.org/derby/derby_downloads.html and download the latest version of derby DB. +When this sample was developed 10.8.2.2 was the latest version. If you however choose to use +a newer or older version, update the property value for property "derbyclient.driver.version" in pom.xml +to the right version you are using. + +2. Extract the downloaded jar file + +3. Go to folder bin in the extracted derby folder and execute startNetworkServer (.bat for windows) +This should start the Derby server. By default it starts listening on port 1527 + +4. The simple in built client "ij" can be used to create the required table in derby. + +5. Execute ij (.bat for windows) + +6. run the command "connect 'jdbc:derby://localhost:1527/TestDatabase;create=true';" for first time +subequently use "connect 'jdbc:derby://localhost:1527/TestDatabase';" + +7. Execute the create table script from the file Person_Derby.sql + +The above steps should setup derby for use. +You can use ij to execute the select commands to query the table data. + + +We have the following examples in this particular Project + +###########################Outbound Channel Adapter###################################### + +This adapter is used to insert/update records in a table based on the values contained +in the message payload that is received over the channel to the adapter. + +The following are necessary for configuring the adapter +1. The datasource or the JDBC template +2. The the update/insert query to be executed. +3. Optional SQL Parameter source factory + +In our config file "jdbcOutboundAdapterConfig.xml", we have defined an outbound-channel-adapter +for the datasource of derby database. Some instructions given to setup derby database. +You are free to use any other databae, however you need to appropriately change the datasource +details defined in the config + +The adapter does the following. +1. It creates a message with payload of type org.springframework.integration.samples.jdbc.Person +2. This message is sent over the channel outboundJdbcChannelOne. +3. Messages over this channel are delivered to the outbound-channel-adapter to execute the given insert. +4. The provided org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory +is used to get an instance of org.springframework.jdbc.core.namedparam.SqlParameterSource. That is +used to resolve the given named parameter to the value. In our case the SpEL is executed to yield +a value that will be inserted. See the config for more details. + +For executing the program and see the results, execute the junit test case +org.springframework.integration.samples.jdbc.OutboundChannelAdapterTest + +Query the database for table Person to see the result. + diff --git a/basic/jdbc/pom.xml b/basic/jdbc/pom.xml new file mode 100644 index 00000000..4844672d --- /dev/null +++ b/basic/jdbc/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + org.springframework.integration.samples + jdbc + 2.0.0 + Spring Integration JDBC Adapter test packages + jar + + 2.0.5.RELEASE + 3.0.6.RELEASE + 1.2.16 + 4.7 + 10.8.2.2 + + + + org.apache.derby + derbyclient + ${derbyclient.driver.version} + + + org.springframework.integration + spring-integration-jdbc + ${spring.integration.version} + + + org.springframework + spring-test + ${spring.test.version} + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + log4j + log4j + ${log4j.version} + + + + junit + junit + ${junit.version} + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + -Xlint:all + true + false + + + + + + + repository.springframework.maven.release + Spring Framework Maven Release Repository + http://maven.springframework.org/release + + + repository.springframework.maven.milestone + Spring Framework Maven Milestone Repository + http://maven.springframework.org/milestone + + + repository.springframework.maven.snapshot + Spring Framework Maven Snapshot Repository + http://maven.springframework.org/snapshot + + + \ No newline at end of file diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Gender.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Gender.java new file mode 100644 index 00000000..6d8a751a --- /dev/null +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Gender.java @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed 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.springframework.integration.samples.jdbc; + +/** + * Represents the gender of the person + * @author Amol Nayak + * + */ +public enum Gender { + + MALE("M"),FEMALE("F"); + + private String identifier; + + private Gender(String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } +} diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Person.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Person.java new file mode 100644 index 00000000..52bd2b97 --- /dev/null +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Person.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed 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.springframework.integration.samples.jdbc; + +import java.util.Date; + +/** + * A simple POJO representing a Person + * @author Amol Nayak + * + */ +public class Person { + + private String name; + private Gender gender; + private Date dateOfBirth; + + + /** + * Gets the name of the person + * @return + */ + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + /** + * Gets the gender of the person + * @return + */ + public Gender getGender() { + return gender; + } + public void setGender(Gender gender) { + this.gender = gender; + } + + /** + * Gets the date of birth of the person + * @return + */ + public Date getDateOfBirth() { + return dateOfBirth; + } + public void setDateOfBirth(Date dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } +} diff --git a/basic/jdbc/src/main/resources/Person_Derby.sql b/basic/jdbc/src/main/resources/Person_Derby.sql new file mode 100644 index 00000000..2d0d6cb7 --- /dev/null +++ b/basic/jdbc/src/main/resources/Person_Derby.sql @@ -0,0 +1,6 @@ +create table Person ( + id integer primary key not null generated always as identity (start with 1, increment by 1), + name varchar(100) not null, + gender varchar(1) not null, + dateOfBirth date +); \ No newline at end of file diff --git a/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundChannelAdapterTest.java b/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundChannelAdapterTest.java new file mode 100644 index 00000000..eb9b8987 --- /dev/null +++ b/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundChannelAdapterTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed 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.springframework.integration.samples.jdbc; + +import java.util.Calendar; + +import org.apache.log4j.Logger; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.Message; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * The test class for jdbc outbound channel adapter + * @author Amol Nayak + * + */ +@ContextConfiguration("classpath:jdbcOutboundAdapterConfig.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class OutboundChannelAdapterTest { + + private Logger logger = Logger.getLogger(OutboundChannelAdapterTest.class); + @Autowired + @Qualifier("outboundJdbcChannelOne") + private MessageChannel channel; + + @Test + public void insertPersonRecord() { + + logger.info("Creating person Instance"); + Person person = new Person(); + Calendar dateOfBirth = Calendar.getInstance(); + dateOfBirth.set(1980, 0, 1); + person.setDateOfBirth(dateOfBirth.getTime()); + person.setName("Name Of The Person"); + person.setGender(Gender.MALE); + Message message = MessageBuilder.withPayload(person).build(); + channel.send(message); + logger.info("Sent person Instance"); + + } + +} diff --git a/basic/jdbc/src/test/resources/jdbcOutboundAdapterConfig.xml b/basic/jdbc/src/test/resources/jdbcOutboundAdapterConfig.xml new file mode 100644 index 00000000..d84826fa --- /dev/null +++ b/basic/jdbc/src/test/resources/jdbcOutboundAdapterConfig.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/jdbc/src/test/resources/log4j.properties b/basic/jdbc/src/test/resources/log4j.properties new file mode 100644 index 00000000..7aa7d540 --- /dev/null +++ b/basic/jdbc/src/test/resources/log4j.properties @@ -0,0 +1,8 @@ +log4j.rootCategory=DEBUG, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n + + +log4j.category.org.springframework=DEBUG