Modified the main to include the newly added gateway samples and changed the .md file

This commit is contained in:
Amol Nayak
2011-12-16 08:44:51 +05:30
parent db9579d478
commit fa14acf3ab
2 changed files with 111 additions and 20 deletions

View File

@@ -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

View File

@@ -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 <enter>: ");
while (!scanner.hasNext("q")) {
System.out.println("Please enter a choice and press <enter>: ");
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 <enter>:");
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 <enter>: ");
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 <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()));
} 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;
}
}
}