From f653433c7ef08456e9096983ad72c4b3cc05eb91 Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Sat, 19 Nov 2011 00:51:41 +0530 Subject: [PATCH 1/6] 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 From e15a778874399d8cb3ab282d733c13e52274e99a Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Sat, 19 Nov 2011 00:57:29 +0530 Subject: [PATCH 2/6] added module jdbc to pom.xml --- basic/pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/basic/pom.xml b/basic/pom.xml index 22943de9..d8862d1a 100644 --- a/basic/pom.xml +++ b/basic/pom.xml @@ -16,6 +16,7 @@ ftp helloworld jms + jdbc jmx mail oddeven @@ -27,6 +28,7 @@ ws-outbound-gateway xml xmpp + From 6b6abd92e4a569edd47f40cf8f6a68b0448633a5 Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Thu, 15 Dec 2011 22:47:52 +0530 Subject: [PATCH 3/6] INTSAMPLE-33 Changes --- basic/jdbc/README.md | 30 +++++++++ basic/jdbc/README.txt | 62 ------------------- basic/jdbc/pom.xml | 17 +---- .../integration/samples/jdbc/Gender.java | 16 +++++ .../integration/samples/jdbc/Person.java | 17 +++++ .../spring-integration-context.xml | 46 +++++++++++++- .../jdbc/src/main/resources/Person_Derby.sql | 6 -- .../jdbc/src/main/resources/setup-tables.sql | 1 + .../jdbc/OutboundChannelAdapterTest.java | 61 ------------------ .../resources/jdbcOutboundAdapterConfig.xml | 38 ------------ .../jdbc/src/test/resources/log4j.properties | 8 --- 11 files changed, 110 insertions(+), 192 deletions(-) delete mode 100644 basic/jdbc/README.txt delete mode 100644 basic/jdbc/src/main/resources/Person_Derby.sql delete mode 100644 basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundChannelAdapterTest.java delete mode 100644 basic/jdbc/src/test/resources/jdbcOutboundAdapterConfig.xml delete mode 100644 basic/jdbc/src/test/resources/log4j.properties diff --git a/basic/jdbc/README.md b/basic/jdbc/README.md index e6c5bec1..6561f77c 100644 --- a/basic/jdbc/README.md +++ b/basic/jdbc/README.md @@ -20,6 +20,36 @@ Currently one example exists. On the command prompt you can enter the following * 'b' * 'foo' +#Sample "Person Outbound Gateway" + +We use the outbound gateway to insert records in a Person table based on the values contained +in the message payload that is received over the channel to the adapter. + +The following are used to configure the gateway +* The request and reply channels +* The data source for the database +* The the update/insert statement to be executed. +* Optional request SQL Parameter source factory +* The select query to be executed after the insert is done +* Optional reply SQL Parameter source factory +* RowMapper if you intend to map the ResultSet to your custom object + +The following sequence of events happen when we invoke the createPerson method on the gateway +* The parameter of type Person is sent as a payload of a message over the reply-channel +* The outbound gateway reads this message and extracts the payload +* It then executes the given insert statement, the values to be inserted are derived from the payload + request-sql-parameter-source-factory provided is used to generate a parameter source. +* Since we expect an identity column to be generated, keys-generated is set to true +* The result of the insert is then use to create a reply sql parameter source, + the factory reply-sql-parameter-source-factory generates the required parameter source +* The select query provided is executed +* The ResultMapper is used to convert this ResultSet to a Person object +* The Person object is then sent as a payload of the Message over the reply channel. +* The Person payload is extracted from the Message and returned to the calling application. + +For executing the program and see the results, execute the junit test case +org.springframework.integration.samples.jdbc.OutboundGatewayTest + # Resources For help please take a look at the Spring Integration documentation: diff --git a/basic/jdbc/README.txt b/basic/jdbc/README.txt deleted file mode 100644 index 436bd1e8..00000000 --- a/basic/jdbc/README.txt +++ /dev/null @@ -1,62 +0,0 @@ -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 index bd571995..976b160b 100644 --- a/basic/jdbc/pom.xml +++ b/basic/jdbc/pom.xml @@ -15,8 +15,6 @@ 2.1.0.M3 1.6.1 4.7 - 3.0.6.RELEASE - 10.8.2.2 @@ -125,20 +123,7 @@ com.h2database h2 1.3.160 - - - - - org.apache.derby - derbyclient - ${derbyclient.driver.version} - + - - - org.springframework - spring-test - ${spring.test.version} - \ 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 index 6d8a751a..24a2f65a 100644 --- 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 @@ -15,6 +15,10 @@ */ package org.springframework.integration.samples.jdbc; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + /** * Represents the gender of the person * @author Amol Nayak @@ -23,6 +27,7 @@ package org.springframework.integration.samples.jdbc; public enum Gender { MALE("M"),FEMALE("F"); + private static Map map; private String identifier; @@ -33,4 +38,15 @@ public enum Gender { public String getIdentifier() { return identifier; } + + public static Gender getGenderByIdentifier(String identifier) { + return map.get(identifier); + } + + static { + map = new HashMap(); + for(Gender gender:EnumSet.allOf(Gender.class)) { + map.put(gender.getIdentifier(), gender); + } + } } 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 index 52bd2b97..b6781e36 100644 --- 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 @@ -24,11 +24,28 @@ import java.util.Date; */ public class Person { + private int personId; private String name; private Gender gender; private Date dateOfBirth; + + /** + * Sets the person id + * @return + */ + public int getPersonId() { + return personId; + } + /** + * Get the person Id + * @param personId + */ + public void setPersonId(int personId) { + this.personId = personId; + } + /** * Gets the name of the person * @return diff --git a/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml index bd04abaa..5c9e0f98 100644 --- a/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml +++ b/basic/jdbc/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -13,7 +13,7 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/jdbc/src/main/resources/Person_Derby.sql b/basic/jdbc/src/main/resources/Person_Derby.sql deleted file mode 100644 index 2d0d6cb7..00000000 --- a/basic/jdbc/src/main/resources/Person_Derby.sql +++ /dev/null @@ -1,6 +0,0 @@ -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/main/resources/setup-tables.sql b/basic/jdbc/src/main/resources/setup-tables.sql index a57cd2a0..dee1726a 100644 --- a/basic/jdbc/src/main/resources/setup-tables.sql +++ b/basic/jdbc/src/main/resources/setup-tables.sql @@ -1,5 +1,6 @@ create table IF NOT EXISTS USERS(USERNAME varchar(100),PASSWORD varchar(100), EMAIL varchar(100)); create table IF NOT EXISTS DUMMY(DUMMY_VALUE varchar(10)); +create table IF NOT EXISTS Person(id integer identity primary key , name varchar(100), gender varchar(1), dateOfBirth date); INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('a', 'secret', 'spring-integration@awesome.com'); INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('b', 's3cr3t', 'spring@rocks.com'); INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('foo', 'bar', 'foo@bar.de'); \ 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 deleted file mode 100644 index eb9b8987..00000000 --- a/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundChannelAdapterTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 deleted file mode 100644 index d84826fa..00000000 --- a/basic/jdbc/src/test/resources/jdbcOutboundAdapterConfig.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/basic/jdbc/src/test/resources/log4j.properties b/basic/jdbc/src/test/resources/log4j.properties deleted file mode 100644 index 7aa7d540..00000000 --- a/basic/jdbc/src/test/resources/log4j.properties +++ /dev/null @@ -1,8 +0,0 @@ -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 From 2b4ad46a80f95a8e41960d28e8258bc922857845 Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Thu, 15 Dec 2011 23:06:32 +0530 Subject: [PATCH 4/6] Removed duplicate JDBC module added --- basic/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/basic/pom.xml b/basic/pom.xml index fcf4d988..61289677 100644 --- a/basic/pom.xml +++ b/basic/pom.xml @@ -18,7 +18,6 @@ helloworld jdbc jms - jdbc jmx mail oddeven From db9579d478f84cb4252c026f9b6e2dfb00b7ec1a Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Fri, 16 Dec 2011 07:24:04 +0530 Subject: [PATCH 5/6] Commiting the missed files --- .../samples/jdbc/PersonMapper.java | 41 ++++++++++++++ .../samples/jdbc/service/PersonService.java | 35 ++++++++++++ .../samples/jdbc/OutboundGatewayTest.java | 53 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/PersonMapper.java create mode 100644 basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java create mode 100644 basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundGatewayTest.java diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/PersonMapper.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/PersonMapper.java new file mode 100644 index 00000000..06530d5b --- /dev/null +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/PersonMapper.java @@ -0,0 +1,41 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; + +import org.springframework.jdbc.core.RowMapper; + +/** + * The result set mapper that will map the {@link ResultSet} to the {@link Person} instance + * @author Amol Nayak + * + */ +public class PersonMapper implements RowMapper { + + /* (non-Javadoc) + * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int) + */ + public Person mapRow(ResultSet rs, int rowNum) throws SQLException { + Person person = new Person(); + person.setPersonId(rs.getInt("id")); + person.setName(rs.getString("name")); + person.setGender(Gender.getGenderByIdentifier(rs.getString("gender"))); + person.setDateOfBirth(rs.getDate("dateOfBirth")); + return person; + } +} diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java new file mode 100644 index 00000000..5f5bc27f --- /dev/null +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/service/PersonService.java @@ -0,0 +1,35 @@ +/* + * 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.service; + +import org.springframework.integration.samples.jdbc.Person; + +/** + * The Service used to create Person instance in database + * @author Amol Nayak + * + */ +public interface PersonService { + + /** + * Creates a {@link Person} instance from the {@link Person} instance passed + * + * @param the created person instance, it will contain the generated primary key and the formated name + * @return + */ + Person createPerson(Person person); + +} diff --git a/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundGatewayTest.java b/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundGatewayTest.java new file mode 100644 index 00000000..0ced7c93 --- /dev/null +++ b/basic/jdbc/src/test/java/org/springframework/integration/samples/jdbc/OutboundGatewayTest.java @@ -0,0 +1,53 @@ +/* + * 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.Assert; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.samples.jdbc.service.PersonService; + +/** + * The test class for jdbc outbound gateway + * @author Amol Nayak + * + */ +public class OutboundGatewayTest { + + private Logger logger = Logger.getLogger(OutboundGatewayTest.class); + + @Test + public void insertPersonRecord() { + ApplicationContext context + = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml"); + PersonService service = context.getBean(PersonService.class); + 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); + person = service.createPerson(person); + Assert.assertNotNull("Expected a non null instance of Person, got null", person); + logger.info("\n\tGenerated person with id: " + person.getPersonId() + ", with name: " + person.getName()); + } + +} From fa14acf3ab2b335b71230a42570f5aabec264e9a Mon Sep 17 00:00:00 2001 From: Amol Nayak Date: Fri, 16 Dec 2011 08:44:51 +0530 Subject: [PATCH 6/6] Modified the main to include the newly added gateway samples and changed the .md file --- basic/jdbc/README.md | 22 +++- .../integration/samples/jdbc/Main.java | 109 +++++++++++++++--- 2 files changed, 111 insertions(+), 20 deletions(-) diff --git a/basic/jdbc/README.md b/basic/jdbc/README.md index 6561f77c..11f00839 100644 --- a/basic/jdbc/README.md +++ b/basic/jdbc/README.md @@ -4,6 +4,18 @@ Spring Integration - JDBC Sample # Overview This sample provides example of how the Jdbc Adapters can be used. +The example presented covers the following two use cases + +* Find a User detail from the database based on the name provided +* Create a new Person record in the table + +The first example demonstrates the use of outbound gateway to search for a user record using the +spring integration's jdbc outbound gateway + +The second example on other hand demonstrates how the jdbc outbound gateway be used to create a new +Person record and then return the newly created Person record. +This example demonstrates how to make use of the sql parameter source factory to extract +the required values to be inserted/updated/selected in the query provided. # Getting Started @@ -14,18 +26,23 @@ You can run the application by either - mvn package - mvn exec:java -Currently one example exists. On the command prompt you can enter the following valid values and get a response back: +Make an appropriate choice for searching a User or creating a Person + +For selecting the User, on the command prompt you can enter the following valid values and get a response back: * 'a' * 'b' * 'foo' -#Sample "Person Outbound Gateway" +For creating the person record, select the appropriate steps as prompted by the application + +#Some details about the sample "Person Outbound Gateway" We use the outbound gateway to insert records in a Person table based on the values contained in the message payload that is received over the channel to the adapter. The following are used to configure the gateway + * The request and reply channels * The data source for the database * The the update/insert statement to be executed. @@ -35,6 +52,7 @@ The following are used to configure the gateway * RowMapper if you intend to map the ResultSet to your custom object The following sequence of events happen when we invoke the createPerson method on the gateway + * The parameter of type Person is sent as a payload of a message over the reply-channel * The outbound gateway reads this message and extracts the payload * It then executes the given insert statement, the values to be inserted are derived from the payload diff --git a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java index 0edab38b..2a607b88 100644 --- a/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java +++ b/basic/jdbc/src/main/java/org/springframework/integration/samples/jdbc/Main.java @@ -15,12 +15,16 @@ */ package org.springframework.integration.samples.jdbc; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Scanner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.samples.jdbc.service.PersonService; import org.springframework.integration.samples.jdbc.service.UserService; @@ -28,6 +32,7 @@ import org.springframework.integration.samples.jdbc.service.UserService; * Starts the Spring Context and will initialize the Spring Integration routes. * * @author Gunnar Hillert + * @author Amol Nayak * @version 1.0 * */ @@ -60,7 +65,8 @@ public final class Main { final Scanner scanner = new Scanner(System.in); - final UserService service = context.getBean(UserService.class); + final UserService userService = context.getBean(UserService.class); + final PersonService personService = context.getBean(PersonService.class); LOGGER.info("\n=========================================================" + "\n " @@ -68,25 +74,27 @@ public final class Main { + "\n " + "\n=========================================================" ); - System.out.print("Please enter a string and press : "); - while (!scanner.hasNext("q")) { - + System.out.println("Please enter a choice and press : "); + System.out.println("\t1. Find user details"); + System.out.println("\t2. Create a new person detail"); + System.out.println("\tq. Quit the application"); + System.out.print("Enter you choice: "); + while (true) { final String input = scanner.nextLine(); - final User user = service.findUser(input); - - if (user != null) { - - System.out.println( - String.format("User found - Username: '%s', Email: '%s', Password: '%s'", - user.getUsername(), user.getEmail(), user.getPassword())); - - } else { - System.out.println( - String.format("No User found for username: '%s'.", input)); - } - - System.out.print("Please enter a string and press :"); + if("1".equals(input.trim())) + getUserDetails(scanner,userService); + else if("2".equals(input.trim())) + createPersonDetails(scanner,personService); + else if("q".equals(input.trim())) + break; + else + System.out.println("Invalid choice\n\n"); + System.out.println("Please enter your choice and press : "); + System.out.println("\t1. Find user details"); + System.out.println("\t2. Create a new person detail"); + System.out.println("\tq. Quit the application"); + System.out.print("Enter you choice: "); } LOGGER.info("Exiting application...bye."); @@ -94,4 +102,69 @@ public final class Main { System.exit(0); } + + private static void createPersonDetails(final Scanner scanner,PersonService service) { + while(true) { + System.out.print("\nEnter the Person's name:"); + String name = scanner.nextLine(); + Gender gender; + while(true) { + System.out.print("Enter the Person's gender(M/F):"); + String genderStr = scanner.nextLine(); + if("m".equalsIgnoreCase(genderStr) || "f".equalsIgnoreCase(genderStr)) { + gender = Gender.getGenderByIdentifier(genderStr.toUpperCase()); + break; + } + } + Date dateOfBirth; + SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); + while(true) { + System.out.print("Enter the Person's Date of birth in DD/MM/YYYY format:"); + String dobStr = scanner.nextLine(); + try { + dateOfBirth = format.parse(dobStr); + break; + } catch (ParseException e) { + //Silently suppress and ask to enter details again + } + } + + Person person = new Person(); + person.setDateOfBirth(dateOfBirth); + person.setGender(gender); + person.setName(name); + person = service.createPerson(person); + System.out.println("Created person record with id: " + person.getPersonId()); + System.out.print("Do you want to create another person? (y/n)"); + String choice = scanner.nextLine(); + if(!"y".equalsIgnoreCase(choice)) + break; + } + } + /** + * @param service + * @param input + */ + private static void getUserDetails(final Scanner scanner,final UserService service) { + while(true) { + System.out.print("Please enter a string and press : "); + String input = scanner.nextLine(); + final User user = service.findUser(input); + if (user != null) { + + System.out.println( + String.format("User found - Username: '%s', Email: '%s', Password: '%s'", + user.getUsername(), user.getEmail(), user.getPassword())); + + } else { + System.out.println( + String.format("No User found for username: '%s'.", input)); + } + System.out.print("Do you want to find another user? (y/n)"); + String choice = scanner.nextLine(); + if(!"y".equalsIgnoreCase(choice)) + break; + } + + } }