Merge pull request #63 from ghillert/INTSAMPLES-87

* ghillert-INTSAMPLES-87:
  INTSAMPLES-87 - Add MS SQL Server Stored Proc Sample For reference see: https://jira.springsource.org/browse/INTSAMPLES-87
This commit is contained in:
Gunnar Hillert
2012-10-24 00:08:20 -04:00
8 changed files with 498 additions and 1 deletions

View File

@@ -0,0 +1,121 @@
Spring Integration - Stored Procedure Example - Microsoft SQL Server (Express)
================================================================================
# Overview
This example provides a simple example using the *Stored Procedure Outbound Gateway*. This example will call *Stored Procedure* as well as a *User-defined Function* using *Microsoft SQL Server (Express)*.
# Setup
## Pre-requisites
Access to a *Microsoft SQL Server* or *Microsoft SQL Server Express* database instance.
This sample was tested against: **Microsoft SQL Server 2008 R2 RTM - Express** (Which can be downloaded and used for free). The sample should also work for newer versions (including the full version) of *Microsoft SQL Server*. You can download *Microsoft SQL Server Express 2008: SQL Server Express*:
* [http://www.microsoft.com/en-us/download/details.aspx?id=23650](http://www.microsoft.com/en-us/download/details.aspx?id=23650)
If you have trouble accessing a remote instance of *Microsoft SQL Server Express*, see:
* [http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277#method2](http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277#method2)
## JDBC Driver
This sample uses the [jTDS](http://jtds.sourceforge.net) driver, which is considered to be faster than [Microsoft's JDBC driver](http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx). Nevertheless, the sample should work with either driver.
#### Creating the Stored Procedure
USE [your database name]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CAPITALIZE_STRING]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CAPITALIZE_STRING]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ===========================================================
-- Author: Gunnar Hillert
-- Create date: 2012-Aug-30
-- Description: Simple Stored Procedure to capatilize a string
-- ===========================================================
CREATE PROCEDURE [dbo].[CAPITALIZE_STRING]
@inoutString VARCHAR(100) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select @inoutString = upper(@inoutString);
END
GO
#### Creating the Function
USE [sitest]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GET_COOL_NUMBER]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GET_COOL_NUMBER]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ===========================================================
-- Author: Gunnar Hillert
-- Create date: 2012-Aug-30
-- Description: Simple Function that returns a constant number
-- ===========================================================
CREATE FUNCTION [dbo].[GET_COOL_NUMBER]
(
)
RETURNS int
AS
BEGIN
DECLARE @cool_number int = 12345;
RETURN @cool_number;
END
GO
### Setting up the DataSource
You may have to update the *Microsoft SQL Server* properties in:
/src/main/resources/META-INF/spring/integration/spring-integration-context.xml
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:jtds:sqlserver://172.16.48.128:1433/sitest" />
<property name="user" value="sitest" />
<property name="password" value="integration" />
</bean>
# Run the Sample
* running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application)
* or from the command line:
- mvn package
- mvn exec:java
--------------------------------------------------------------------------------
For help please take a look at the Spring Integration documentation:
http://www.springsource.org/spring-integration

View File

@@ -0,0 +1,118 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.integration.samples.samples</groupId>
<artifactId>ms-stored-procedures</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Samples (Intermediate) - Stored Procedures Microsoft</name>
<url>http://www.springsource.org/spring-integration</url>
<prerequisites>
<maven>2.2.1</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.2.0.RC2</spring.integration.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.10</junit.version>
</properties>
<repositories>
<repository>
<id>repo.springsource.org.milestone</id>
<name>Spring Framework Maven Milestone Repository</name>
<url>https://repo.springsource.org/libs-milestone</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.springframework.integration.samples.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring Integration -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Persistence -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,91 @@
/*
* 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.samples;
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.samples.service.StringConversionService;
/**
* Starts the Spring Context and will initialize the Spring Integration routes.
*
* @author Gunnar Hillert
* @version 1.0
*
*/
public final class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
private Main() { }
/**
* Load the Spring Integration Application Context
*
* @param args - command line arguments
*/
public static void main(final String... args) {
LOGGER.info("\n========================================================="
+ "\n "
+ "\n Welcome to Spring Integration! "
+ "\n "
+ "\n For more information please visit: "
+ "\n http://www.springsource.org/spring-integration "
+ "\n "
+ "\n=========================================================" );
final AbstractApplicationContext context =
new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
final Scanner scanner = new Scanner(System.in);
final StringConversionService service = context.getBean(StringConversionService.class);
LOGGER.info("\n========================================================="
+ "\n "
+ "\n Please press 'q + Enter' to quit the application. "
+ "\n "
+ "\n=========================================================" );
System.out.print("Please enter a string and press <enter>: ");
while (!scanner.hasNext("q")) {
String input = scanner.nextLine();
System.out.println("Converting String to Uppcase using Stored Procedure...");
String inputUpperCase = service.convertToUpperCase(input);
System.out.println("Retrieving Numeric value via Sql Function...");
Integer number = service.getNumber();
System.out.println(String.format("Converted '%s' - End Result: '%s_%s'.", input, inputUpperCase, number));
System.out.print("To try again, please enter a string and press <enter>:");
}
LOGGER.info("Exiting application...bye.");
System.exit(0);
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.samples.service;
/**
* Provides string manipulation services.
*
* @author Gunnar Hillert
*
*/
public interface StringConversionService {
/**
* Converts a String to Upper Case.
*
* @param stringToConvert The string to convert to upper case
* @return The converted upper case string.
*/
String convertToUpperCase(String stringToConvert);
/**
* Retrieving a constant numeric value.
*
* @return Returns a constant number
*/
Integer getNumber();
}

View File

@@ -0,0 +1,42 @@
<?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"
xsi:schemaLocation="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">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:jtds:sqlserver://172.16.48.128:1433/sitest" />
<property name="user" value="sitest" />
<property name="password" value="integration" />
</bean>
<int:channel id="functionRequestChannel" />
<int:channel id="procedureRequestChannel" />
<int:channel id="replyChannel" />
<int:gateway id="gateway" default-request-timeout="5000"
default-reply-timeout="5000" default-request-channel="functionRequestChannel"
service-interface="org.springframework.integration.samples.service.StringConversionService">
<int:method name="convertToUpperCase" request-channel="procedureRequestChannel" />
<int:method name="getNumber" request-channel="functionRequestChannel"
payload-expression="new java.util.Date()" />
</int:gateway>
<int-jdbc:stored-proc-outbound-gateway
id="outbound-gateway-procedure" request-channel="procedureRequestChannel"
data-source="dataSource" stored-procedure-name="CAPITALIZE_STRING"
expect-single-result="true">
<int-jdbc:parameter name="INOUTSTRING" expression="payload" />
</int-jdbc:stored-proc-outbound-gateway>
<int-jdbc:stored-proc-outbound-gateway
id="outbound-gateway-function" request-channel="functionRequestChannel"
data-source="dataSource" stored-procedure-name="GET_COOL_NUMBER"
is-function="true" expect-single-result="true">
</int-jdbc:stored-proc-outbound-gateway>
</beans>

View File

@@ -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="warn" />
</logger>
<logger name="org.springframework.integration.samples">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -0,0 +1,55 @@
/*
* 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.samples;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.samples.service.StringConversionService;
/**
* Verify that the Spring Integration Application Context starts successfully.
*/
public class StringConversionServiceTest {
@Test
public void testStartupOfSpringInegrationContext() throws Exception{
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 StringConversionService service = context.getBean(StringConversionService.class);
final String stringToConvert = "I love Spring Integration";
final String expectedResult = "I LOVE SPRING INTEGRATION";
final String convertedString = service.convertToUpperCase(stringToConvert);
Assert.assertEquals("Expecting that the string is converted to upper case.",
expectedResult, convertedString);
}
}

View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.integration.samples</groupId>
<artifactId>samples-root</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.BUILD-SNAPSHOT</version>
<name>Spring Integration Samples Root</name>
<url>http://www.springsource.org/spring-integration</url>
<packaging>pom</packaging>