Merge pull request #25 from amolnayak311/INTSAMPLES-43

INTSAMPLES-43
This commit is contained in:
Gunnar Hillert
2012-01-07 07:43:12 -08:00
6 changed files with 63 additions and 149 deletions

View File

@@ -4,12 +4,12 @@ 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
The example presented covers the following use cases
* Find a User detail from the database based on the name provided
* Find a Person 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
The first example demonstrates the use of outbound gateway to search for a person 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
@@ -26,16 +26,12 @@ You can run the application by either
- mvn package
- mvn exec:java
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'
Make an appropriate choice for searching a Person or creating a Person
For creating the person record, select the appropriate steps as prompted by the application
On creation of Person records, you may choose the option of selecting the created person records and view their details
#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

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.samples.jdbc;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import org.slf4j.Logger;
@@ -25,7 +26,6 @@ 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;
/**
@@ -65,7 +65,7 @@ public final class Main {
final Scanner scanner = new Scanner(System.in);
final UserService userService = context.getBean(UserService.class);
final PersonService personService = context.getBean(PersonService.class);
LOGGER.info("\n========================================================="
@@ -75,14 +75,14 @@ public final class Main {
+ "\n=========================================================" );
System.out.println("Please enter a choice and press <enter>: ");
System.out.println("\t1. Find user details");
System.out.println("\t1. Find person 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();
if("1".equals(input.trim()))
getUserDetails(scanner,userService);
getPersonDetails(scanner, personService);
else if("2".equals(input.trim()))
createPersonDetails(scanner,personService);
else if("q".equals(input.trim()))
@@ -90,8 +90,8 @@ public final class Main {
else
System.out.println("Invalid choice\n\n");
System.out.println("Please enter your choice and press <enter>: ");
System.out.println("\t1. Find user details");
System.out.println("Please enter a choice and press <enter>: ");
System.out.println("\t1. Find person details");
System.out.println("\t2. Create a new person detail");
System.out.println("\tq. Quit the application");
System.out.print("Enter you choice: ");
@@ -145,22 +145,23 @@ public final class Main {
* @param service
* @param input
*/
private static void getUserDetails(final Scanner scanner,final UserService service) {
private static void getPersonDetails(final Scanner scanner,final PersonService service) {
while(true) {
System.out.print("Please enter a string and press <enter>: ");
System.out.print("Please enter the name of the person and press<enter>: ");
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()));
final List<Person> personList = service.findPersonByName(input);
if(personList != null && !personList.isEmpty()) {
for(Person person:personList) {
System.out.print(
String.format("Person found - Person Id: '%d', Person Name is: '%s', Gender: '%s'",
person.getPersonId(),person.getName(), person.getGender()));
System.out.println(String.format(", Date of birth: '%1$td/%1$tm/%1$tC%1$ty'", person.getDateOfBirth()));
}
} 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.format("No Person record found for name: '%s'.", input));
}
System.out.print("Do you want to find another person? (y/n)");
String choice = scanner.nextLine();
if(!"y".equalsIgnoreCase(choice))
break;

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.integration.samples.jdbc.service;
import java.util.List;
import org.springframework.integration.samples.jdbc.Person;
/**
@@ -31,5 +33,14 @@ public interface PersonService {
* @return
*/
Person createPerson(Person person);
/**
* Find the person by the person name, the name search is case insensitive, however the
* spaces are not ignored
*
* @param name
* @return the matching {@link Person} record
*/
List<Person> findPersonByName(String name);
}

View File

@@ -1,33 +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.service;
import org.springframework.integration.samples.jdbc.User;
/**
* Provides user services.
*/
public interface UserService {
/**
* Retrieves a user based on the provided username.
*
* @param username Find users by username
* @return The user if exists, null otherwise.
*/
User findUser(String username);
}

View File

@@ -8,10 +8,7 @@
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:channel id="requestChannel"/>
<int:channel id="replyChannel"/>
<jdbc:embedded-database id="datasource" type="H2">
<jdbc:script location="classpath:setup-tables.sql"/>
</jdbc:embedded-database>
@@ -19,39 +16,40 @@
<!-- See also:
http://static.springsource.org/spring-integration/reference/htmlsingle/#gateway-proxy
http://www.eaipatterns.com/MessagingGateway.html -->
<int:gateway default-request-timeout="5000"
default-reply-timeout="5000"
default-request-channel="requestChannel"
default-reply-channel="replyChannel"
service-interface="org.springframework.integration.samples.jdbc.service.UserService">
<int:method name="convertToUpperCase"/>
<int:channel id="createPersonRequestChannel"/>
<int:channel id="createPersonReplyChannel"/>
<int:channel id="findPersonRequestChannel"/>
<int:channel id="findPersonReplyChannel"/>
<int:gateway id="personService" service-interface="org.springframework.integration.samples.jdbc.service.PersonService">
<int:method name="createPerson"
request-channel="createPersonRequestChannel"
request-timeout="5000"
reply-channel="createPersonReplyChannel"
reply-timeout="5000"/>
<int:method name="findPersonByName"
request-channel="findPersonRequestChannel"
request-timeout="5000"
reply-channel="findPersonReplyChannel"
reply-timeout="5000"/>
</int:gateway>
<int-jdbc:outbound-gateway data-source="datasource"
update="UPDATE DUMMY SET DUMMY_VALUE='test'"
request-channel="requestChannel" query="select * from users where username=:payload"
reply-channel="replyChannel" row-mapper="rowMapper">
request-channel="findPersonRequestChannel"
query="select * from Person where lower(name)=lower(:payload)"
reply-channel="findPersonReplyChannel" row-mapper="personResultMapper"
max-rows-per-poll="100">
</int-jdbc:outbound-gateway>
<bean id="rowMapper" class="org.springframework.integration.samples.jdbc.UserMapper"/>
<!-- ====================Config for Person Service ========================= -->
<int:channel id="outboundJdbcRequestChannel"/>
<int:channel id="outboundJdbcResponseChannel"/>
<int:gateway id="personService" service-interface="org.springframework.integration.samples.jdbc.service.PersonService">
<int:method name="createPerson"
request-channel="outboundJdbcRequestChannel"
request-timeout="5000"
reply-channel="outboundJdbcResponseChannel"
reply-timeout="5000"/>
</int:gateway>
<bean id="personResultMapper" class="org.springframework.integration.samples.jdbc.PersonMapper"/>
<int-jdbc:outbound-gateway data-source="datasource"
request-channel="outboundJdbcRequestChannel"
reply-channel="outboundJdbcResponseChannel"
request-channel="createPersonRequestChannel"
reply-channel="createPersonReplyChannel"
update="insert into Person (name,gender,dateOfBirth)
values
(:name,:gender,:dateOfBirth)"

View File

@@ -1,59 +0,0 @@
/*
* Copyright 2002-2010 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.sts;
import static junit.framework.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.samples.jdbc.User;
import org.springframework.integration.samples.jdbc.service.UserService;
/**
* Verify that the Spring Integration Application Context starts successfully.
*/
public class StringConversionServiceTest {
@Test
public void testStartupOfSpringInegrationContext() throws Exception{
final ApplicationContext context
= new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml",
StringConversionServiceTest.class);
Thread.sleep(2000);
}
@Test
public void testConvertStringToUpperCase() {
final ApplicationContext context
= new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml",
StringConversionServiceTest.class);
final UserService service = context.getBean(UserService.class);
final String userNameToUse = "a";
final String expectedResult = "I LOVE SPRING INTEGRATION";
final User user = service.findUser(userNameToUse);
assertEquals("Expecting that the returned username is 'a'.",
userNameToUse, user.getUsername());
}
}