INTEXT-48 - Add sample for AWS S3 support

For reference see: https://jira.springsource.org/browse/INTEXT-48
This commit is contained in:
Gunnar Hillert
2013-02-25 17:58:03 -05:00
parent 57f9f0f8a4
commit 4426d540aa
6 changed files with 401 additions and 0 deletions

62
samples/aws-s3/README.md Normal file
View File

@@ -0,0 +1,62 @@
Spring Integration - Amazon S3 Sample
=====================================
## Introduction
This sample demonstrate the usage of the *Spring Integration* support for
[Amazon Simple Storage Service][] (Amazon S3). The sample covers 2 use-cases:
* **Upload** a file to Amazon S3 using the *s3-outbound-channel-adapter*
* **Poll** files from Amazon S3 using the *s3-inbound-channel-adapter*
## How to Run the Sample
You can execute this sample simply via [Maven][]:
$ mvn clean package exec:java
Once executed you should see the following screen:
=========================================================
Welcome to the Spring Integration Amazon S3 Sample
For more information please visit:
https://github.com/SpringSource/spring-integration-extensions
=========================================================
What would you like to do?
1. Upload a file to Amazon S3
2. Poll files from Amazon S3
q. Quit the application
>
Once you have selected either **Option 1** or **Option 2**, you will be asked to
provide the **Access Key ID** and the **Secret Access Key** for *Amazon Web Services*.
You may also consider providing the **Access Key ID** and the **Secret Access Key**
via the command line:
$ mvn clean package exec:java -DaccessKey=12345 -DsecretKey=12345
In that case you will not be asked to provide that information again.
## s3-outbound-channel-adapter
When you selected the option to upload a file you will be asked to specify the file you would like to upload:
Please enter the path to the file you want to upload:
Enter a path such as `/demo/data/myfile.txt`.
## s3-inbound-channel-adapter
The polled files will be stored under `s3-local-storage`.
--------------------------------------------------------------------------------
For help please take a look at the [Spring Integration documentation][]
[Amazon Simple Storage Service]: http://aws.amazon.com/s3/
[Maven]: http://maven.apache.org/
[Spring Integration documentation]: http://www.springsource.org/spring-integration

112
samples/aws-s3/pom.xml Normal file
View File

@@ -0,0 +1,112 @@
<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>aws-s3</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aws-s3</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.1.RELEASE</spring.integration.version>
<spring.integration.aws.version>0.5.0.BUILD-SNAPSHOT</spring.integration.aws.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.11</junit.version>
</properties>
<repositories>
<repository>
<id>repo.springsource.org.milestone</id>
<name>Spring Framework Maven Milestone Repository</name>
<url>https://repo.springsource.org/libs-snapshot</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>3.0</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.aws.s3.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-file</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-aws</artifactId>
<version>${spring.integration.aws.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,155 @@
/*
* Copyright 2002-2013 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.aws.s3;
import java.io.File;
import java.util.Scanner;
import org.apache.log4j.Logger;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
/**
* Starts the Spring Context and will initialize the Spring Integration routes.
*
* @author Gunnar Hillert
* @since 1.0
*
*/
public final class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
private static final String HORIZONTAL_LINE = "\n=========================================================";
private Main() { }
/**
* Load the Spring Integration Application Context
*
* @param args - command line arguments
*/
public static void main(final String... args) {
final Scanner scanner = new Scanner(System.in);
if (LOGGER.isInfoEnabled()) {
LOGGER.info(HORIZONTAL_LINE
+ "\n "
+ "\n Welcome to the Spring Integration Amazon S3 Sample "
+ "\n "
+ "\n For more information please visit: "
+ "\nhttps://github.com/SpringSource/spring-integration-extensions"
+ "\n "
+ HORIZONTAL_LINE );
}
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
final ConfigurableEnvironment environment = context.getEnvironment();
System.out.println("What would you like to do?");
System.out.println("\t1. Upload a file to Amazon S3");
System.out.println("\t2. Poll files from Amazon S3");
System.out.println("\tq. Quit the application");
System.out.print(" > ");
String filePath;
while (true) {
final String input = scanner.nextLine();
if("1".equals(input.trim())) {
System.out.println("Uploading to Amazon S3...");
environment.setActiveProfiles("upload-to-s3");
setupCredentials(environment, scanner);
setupS3info(environment, scanner);
context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();
System.out.print("\nPlease enter the path to the file you want to upload: ");
filePath = scanner.nextLine();
final MessageChannel messageChannel = context.getBean("s3channel", MessageChannel.class);
messageChannel.send(MessageBuilder.withPayload(new File(filePath)).build());
break;
}
else if("2".equals(input.trim())) {
System.out.println("Polling files from Amazon S3...");
environment.setActiveProfiles("poll-s3");
setupCredentials(environment, scanner);
setupS3info(environment, scanner);
context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();
}
else if("q".equals(input.trim())) {
System.out.println("Exiting application...bye.");
System.exit(0);
}
else {
System.out.println("Invalid choice\n\n");
System.out.print("Enter you choice: ");
}
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Exiting application...bye.");
}
System.exit(0);
}
private static void setupCredentials(ConfigurableEnvironment environment, Scanner scanner) {
if (!environment.containsProperty("accessKey")) {
System.out.print("\nPlease enter your Access Key ID: ");
final String accessKey = scanner.nextLine();
environment.getSystemProperties().put("accessKey", accessKey);
}
if (!environment.containsProperty("secretKey")) {
System.out.print("\nPlease enter your Secret Access Key: ");
final String secretKey = scanner.nextLine();
environment.getSystemProperties().put("secretKey", secretKey);
}
}
private static void setupS3info(ConfigurableEnvironment environment, Scanner scanner) {
if (!environment.containsProperty("bucket")) {
System.out.print("\nWhich bucket do you want to use? ");
final String bucket = scanner.nextLine();
environment.getSystemProperties().put("bucket", bucket);
}
if (!environment.containsProperty("remoteDirectory")) {
System.out.print("\nPlease enter the S3 remote directory to use: ");
final String remoteDirectory = scanner.nextLine();
environment.getSystemProperties().put("remoteDirectory", remoteDirectory);
}
}
}

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-file="http://www.springframework.org/schema/integration/file"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder />
<bean id="credentials" class="org.springframework.integration.aws.core.BasicAWSCredentials">
<property name="accessKey" value="${accessKey}"/>
<property name="secretKey" value="${secretKey}"/>
</bean>
<int:channel id="s3channel">
<int:interceptors>
<int:wire-tap channel="loggit"/>
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="loggit" level="INFO"
logger-name="org.springframework.integration.samples.aws.s3"
expression="'File Name: ' + payload.name + '(' + payload.length() + ')'"/>
<beans profile="poll-s3">
<int-aws:s3-inbound-channel-adapter credentials-ref="credentials" local-directory="s3-local-storage"
temporary-suffix=".transferring"
bucket="${bucket}" channel="s3channel" remote-directory="${remoteDirectory}">
<int-file:outbound-channel-adapter channel="s3channel" directory="target/s3-out"/>
<int:poller default="true" fixed-rate="5000"/>
</beans>
<beans profile="upload-to-s3">
<int-aws:s3-outbound-channel-adapter credentials-ref="credentials"
bucket="${bucket}" channel="s3channel" remote-directory="${remoteDirectory}"/>
</beans>
</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.aws.s3">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>