First draft of an Oracle Stored Proc Sample

Sample show-cases the execution of Oracle Stored Procedures and Functions
This commit is contained in:
Gunnar Hillert
2011-10-05 11:04:59 -04:00
parent de878d682e
commit 9876236e98
8 changed files with 488 additions and 0 deletions

3
.gitignore vendored
View File

@@ -6,3 +6,6 @@ log.roo
.project
.classpath
.DS_Store
*.iml
*.ipr
*.iws

View File

@@ -0,0 +1,108 @@
Spring Integration - Stored Procedure Example - Oracle
================================================================================
# Overview
This example provides a simple example using the stored procedure outbound gateway
adapter. This example will call an Oracle Stored Procedure as well as an Oracle Function using the StoredProc Outbound Gateway.
This sample was tested against: **Oracle Database Express Edition 11g Release 2** (which can be downloaded and used for free).
Nevertheless, the example should work with other versions as well.
# Setup
##Pre-requisites
- Access to a Oracle or Oracle XE database instance
- Install Oracle JDBC Driver to your local Maven repository (~/.m2)
##JDBC Driver installation
- Go to [http://www.oracle.com/technetwork/indexes/downloads/index.html](http://www.oracle.com/technetwork/indexes/downloads/index.html)
- Under "JDBC Drivers", download the appropriate driver relevant to your Oracle and JDK version (This sample was tested using
"Oracle Database 11g Release 2 JDBC Drivers")
- Once downloaded, install the driver to your local Maven repository:
$ mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=~/dev/ojdbc6.jar -DgeneratePom=true
- Now you can add the dependency to the Maven pom.xml file. Please check the pom.xml for this project and verify that it matches your installed Oracle JDBC driver
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
## Setting Up Oracle
### Create Tablespace
CREATE TABLESPACE procedure_test
DATAFILE 'c:/data/procedure_test.dbf'
SIZE 10M
AUTOEXTEND ON NEXT 10M
MAXSIZE 100M;
### Create User
CREATE USER storedproc
IDENTIFIED BY storedproc
DEFAULT TABLESPACE procedure_test
TEMPORARY TABLESPAC temp;
### Grant Rights
GRANT CREATE SESSION,CREATE TABLE,CREATE VIEW,CREATE SEQUENCE TO storedproc;
ALTER USER storedproc QUOTA unlimited ON procedure_test;
### Creating the Stored Procedure
create or replace
PROCEDURE CAPITALIZE_STRING(inoutString IN OUT VARCHAR) IS
BEGIN
SELECT upper(inoutString) INTO inoutString from dual ;
END;
### Creating the Function
create or replace
FUNCTION GET_COOL_NUMBER
RETURN NUMBER
IS cool_number NUMBER(11,2);
BEGIN
cool_number := 12345;
RETURN cool_number;
END;
## Setting Up Oracle
You may have to update the Oracle DB properties in:
/src/main/resources/META-INF/spring/integration/spring-integration-context.xml
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="connectionCachingEnabled" value="true" />
<property name="URL" value="jdbc:oracle:thin:@//localhost:1521/XE" />
<property name="password" value="storedproc" />
<property name="user" value="storedproc" />
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">3</prop>
<prop key="MaxLimit">20</prop>
</props>
</property>
</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,122 @@
<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</groupId>
<artifactId>oracle-stored-procedures</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>stored-procedures-oracle</name>
<url>http://www.springsource.org/spring-integration</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.1.0.BUILD-SNAPSHOT</spring.integration.version>
<slf4j.version>1.6.1</slf4j.version>
<junit.version>4.7</junit.version>
</properties>
<repositories>
<repository>
<id>repository.springframework.maven.release</id>
<name>Spring Framework Maven Release Repository</name>
<url>http://maven.springframework.org/release</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</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.3.2</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</version>
<configuration>
<mainClass>org.springframework.integration.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>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.28</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,92 @@
/*
* 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;
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.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 = LoggerFactory.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,37 @@
/*
* 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.service;
import org.springframework.integration.annotation.Payload;
import java.util.Map;
/**
* Provides string manipulation services.
*/
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);
Integer getNumber();
}

View File

@@ -0,0 +1,44 @@
<?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="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="connectionCachingEnabled" value="true" />
<property name="URL" value="jdbc:oracle:thin:@//10.0.1.10:1521/XE" />
<property name="password" value="tt" />
<property name="user" value="tt" />
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">3</prop>
<prop key="MaxLimit">20</prop>
</props>
</property>
</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.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,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
</encoder>
</appender>
<logger name="org.springframework.integration">
<level value="INFO" />
</logger>
<logger name="org.springframework">
<level value="INFO" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>

View File

@@ -0,0 +1,58 @@
/*
* 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.service.StringConversionService;
/**
* 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 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);
// assertEquals("Expecting that the string is converted to upper case.",
//dd expectedResult, convertedString);
}
}