INTSAMPLES-97 - Add Postgres Stored Procedure Sample
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import org.springframework.integration.model.CoffeeBeverage;
|
||||
import org.springframework.integration.service.CoffeeService;
|
||||
|
||||
|
||||
/**
|
||||
* Starts the Spring Context and will initialize the Spring Integration routes.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public final class Main {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Main.class);
|
||||
|
||||
private static final String LINE = "\n=========================================================";
|
||||
private static final String NEWLINE = "\n";
|
||||
|
||||
private Main() { }
|
||||
|
||||
/**
|
||||
* Load the Spring Integration Application Context
|
||||
*
|
||||
* @param args - command line arguments
|
||||
*/
|
||||
public static void main(final String... args) {
|
||||
|
||||
LOGGER.info(LINE
|
||||
+ LINE
|
||||
+ "\n Welcome to Spring Integration Coffee Database! "
|
||||
+ NEWLINE
|
||||
+ "\n For more information please visit: "
|
||||
+ "\n http://www.springsource.org/spring-integration "
|
||||
+ NEWLINE
|
||||
+ LINE );
|
||||
|
||||
final AbstractApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml");
|
||||
|
||||
context.registerShutdownHook();
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
final CoffeeService service = context.getBean(CoffeeService.class);
|
||||
|
||||
LOGGER.info(LINE
|
||||
+ NEWLINE
|
||||
+ "\n Please press 'q + Enter' to quit the application."
|
||||
+ NEWLINE
|
||||
+ LINE);
|
||||
|
||||
System.out.print("Please enter 'list' and press <enter> to get a list of coffees.");
|
||||
System.out.print("Enter a coffee id, e.g. '1' and press <enter> to get a description.\n\n");
|
||||
|
||||
while (!scanner.hasNext("q")) {
|
||||
|
||||
String input = scanner.nextLine();
|
||||
|
||||
if ("list".equalsIgnoreCase(input)) {
|
||||
List<CoffeeBeverage> coffeeBeverages = service.findAllCoffeeBeverages();
|
||||
|
||||
for (CoffeeBeverage coffeeBeverage : coffeeBeverages) {
|
||||
System.out.println(String.format("%s - %s", coffeeBeverage.getId(),
|
||||
coffeeBeverage.getName()));
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println("Retrieving coffee information...");
|
||||
String coffeeDescription = service.findCoffeeBeverage(Integer.valueOf(input));
|
||||
|
||||
System.out.println(String.format("Searched for '%s' - Found: '%s'.", input, coffeeDescription));
|
||||
System.out.print("To try again, please enter another coffee beverage and press <enter>:\n\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LOGGER.info("Exiting application...bye.");
|
||||
|
||||
System.exit(0);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class CoffeeBeverage {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
/** Default Constructor */
|
||||
public CoffeeBeverage() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param name
|
||||
* @param description
|
||||
*/
|
||||
public CoffeeBeverage(Integer id, String name, String description) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime
|
||||
* result
|
||||
+ ((this.description == null) ? 0 : this.description.hashCode());
|
||||
result = prime * result
|
||||
+ ((this.name == null) ? 0 : this.name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
CoffeeBeverage other = (CoffeeBeverage) obj;
|
||||
|
||||
if (this.description == null) {
|
||||
|
||||
if (other.description != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (!this.description.equals(other.description)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.name == null) {
|
||||
|
||||
if (other.name != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (!this.name.equals(other.name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("CoffeeBeverage [id=").append(this.id).append(", name=")
|
||||
.append(this.name).append(", description=")
|
||||
.append(this.description).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.annotation.Payload;
|
||||
import org.springframework.integration.model.CoffeeBeverage;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
/**
|
||||
* Provides access to the Coffee Database Services.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 2.2
|
||||
*/
|
||||
@Transactional
|
||||
public interface CoffeeService {
|
||||
|
||||
/**
|
||||
* Find the description for a provided coffee beverage.
|
||||
*
|
||||
* @param Id of the coffee beverage
|
||||
* @return The the description of the coffee beverage
|
||||
*/
|
||||
String findCoffeeBeverage(Integer input);
|
||||
|
||||
/**
|
||||
* Find the description for a provided coffee beverage.
|
||||
*
|
||||
* @return Collection of coffee beverages
|
||||
*/
|
||||
@Payload("new java.util.Date()")
|
||||
List<CoffeeBeverage> findAllCoffeeBeverages();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.support;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.integration.model.CoffeeBeverage;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class CoffeBeverageMapper implements RowMapper<CoffeeBeverage> {
|
||||
|
||||
public CoffeeBeverage mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new CoffeeBeverage(rs.getInt("ID"), rs.getString("COFFEE_NAME"), rs.getString("COFFEE_DESCRIPTION"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
||||
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
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager"/>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
|
||||
destroy-method="close">
|
||||
<property name="driverClassName" value="org.postgresql.Driver" />
|
||||
<property name="url"
|
||||
value="jdbc:postgresql:integration" />
|
||||
<property name="username" value="postgres" />
|
||||
<property name="password" value="postgres" />
|
||||
</bean>
|
||||
|
||||
<int:channel id="findCoffeeProcedureRequestChannel" />
|
||||
<int:channel id="findAllProcedureRequestChannel" />
|
||||
|
||||
<int:gateway id="gateway" default-request-timeout="4000"
|
||||
default-reply-timeout="4000"
|
||||
service-interface="org.springframework.integration.service.CoffeeService">
|
||||
<int:method name="findCoffeeBeverage" request-channel="findCoffeeProcedureRequestChannel" />
|
||||
<int:method name="findAllCoffeeBeverages" request-channel="findAllProcedureRequestChannel" />
|
||||
</int:gateway>
|
||||
|
||||
<int-jdbc:stored-proc-outbound-gateway
|
||||
id="outbound-gateway-storedproc-find-coffee" data-source="dataSource"
|
||||
request-channel="findCoffeeProcedureRequestChannel" is-function="true"
|
||||
skip-undeclared-results="true" stored-procedure-name="FIND_COFFEE"
|
||||
expect-single-result="true">
|
||||
<int-jdbc:sql-parameter-definition name="coffee_name" type="INTEGER" direction="IN"/>
|
||||
<int-jdbc:parameter name="coffee_name" expression="payload" />
|
||||
</int-jdbc:stored-proc-outbound-gateway>
|
||||
|
||||
<int-jdbc:stored-proc-outbound-gateway
|
||||
id="outbound-gateway-storedproc-find-all" data-source="dataSource" ignore-column-meta-data="true"
|
||||
request-channel="findAllProcedureRequestChannel" expect-single-result="true"
|
||||
stored-procedure-name="FIND_ALL_COFFEE_BEVERAGES">
|
||||
<int-jdbc:returning-resultset name="ref"
|
||||
row-mapper="org.springframework.integration.support.CoffeBeverageMapper" />
|
||||
</int-jdbc:stored-proc-outbound-gateway>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Loggers -->
|
||||
<logger name="org.springframework.integration">
|
||||
<level value="INFO" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.integration.samples">
|
||||
<level value="INFO" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="INFO" />
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
Reference in New Issue
Block a user