diff --git a/intermediate/stored-procedures-ms/README.md b/intermediate/stored-procedures-ms/README.md new file mode 100644 index 00000000..b10ba4f2 --- /dev/null +++ b/intermediate/stored-procedures-ms/README.md @@ -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 + + + + + + + + + +# 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 + diff --git a/intermediate/stored-procedures-ms/pom.xml b/intermediate/stored-procedures-ms/pom.xml new file mode 100644 index 00000000..bc2a61b4 --- /dev/null +++ b/intermediate/stored-procedures-ms/pom.xml @@ -0,0 +1,118 @@ + + 4.0.0 + + org.springframework.integration.samples.samples + ms-stored-procedures + + 2.2.0.BUILD-SNAPSHOT + jar + + Samples (Intermediate) - Stored Procedures Microsoft + http://www.springsource.org/spring-integration + + + 2.2.1 + + + + UTF-8 + 2.2.0.RC2 + 1.2.17 + 4.10 + + + + + repo.springsource.org.milestone + Spring Framework Maven Milestone Repository + https://repo.springsource.org/libs-milestone + + + + + + + maven-eclipse-plugin + 2.9 + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + + 1.6 + 1.6 + -Xlint:all + true + true + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + org.springframework.integration.samples.Main + + + + + + + + + + + junit + junit + ${junit.version} + test + + + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + + org.springframework.integration + spring-integration-jdbc + ${spring.integration.version} + + + + + + log4j + log4j + ${log4j.version} + + + + + + net.sourceforge.jtds + jtds + 1.2.6 + + + c3p0 + c3p0 + 0.9.1.2 + + + diff --git a/intermediate/stored-procedures-ms/src/main/java/org/springframework/integration/samples/Main.java b/intermediate/stored-procedures-ms/src/main/java/org/springframework/integration/samples/Main.java new file mode 100644 index 00000000..fccdbe7b --- /dev/null +++ b/intermediate/stored-procedures-ms/src/main/java/org/springframework/integration/samples/Main.java @@ -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 : "); + + 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 :"); + } + + LOGGER.info("Exiting application...bye."); + + System.exit(0); + + } +} diff --git a/intermediate/stored-procedures-ms/src/main/java/org/springframework/integration/samples/service/StringConversionService.java b/intermediate/stored-procedures-ms/src/main/java/org/springframework/integration/samples/service/StringConversionService.java new file mode 100644 index 00000000..ba089dd3 --- /dev/null +++ b/intermediate/stored-procedures-ms/src/main/java/org/springframework/integration/samples/service/StringConversionService.java @@ -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(); + +} diff --git a/intermediate/stored-procedures-ms/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/intermediate/stored-procedures-ms/src/main/resources/META-INF/spring/integration/spring-integration-context.xml new file mode 100644 index 00000000..3d67ea5c --- /dev/null +++ b/intermediate/stored-procedures-ms/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/intermediate/stored-procedures-ms/src/main/resources/log4j.xml b/intermediate/stored-procedures-ms/src/main/resources/log4j.xml new file mode 100644 index 00000000..40d94ca5 --- /dev/null +++ b/intermediate/stored-procedures-ms/src/main/resources/log4j.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/intermediate/stored-procedures-ms/src/test/java/org/springframework/integration/samples/StringConversionServiceTest.java b/intermediate/stored-procedures-ms/src/test/java/org/springframework/integration/samples/StringConversionServiceTest.java new file mode 100644 index 00000000..27faa02b --- /dev/null +++ b/intermediate/stored-procedures-ms/src/test/java/org/springframework/integration/samples/StringConversionServiceTest.java @@ -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); + + } + +} diff --git a/pom.xml b/pom.xml index c1fcd6a1..4e99a819 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.springframework.integration.samples samples-root - 2.1.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT Spring Integration Samples Root http://www.springsource.org/spring-integration pom