这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws-lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.aws-sam/
1 change: 1 addition & 0 deletions aws-lambda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<modules>
<module>lambda</module>
<module>shipping-tracker/ShippingFunction</module>
<module>todo-reminder/ToDoFunction</module>
</modules>

</project>
105 changes: 105 additions & 0 deletions aws-lambda/todo-reminder/ToDoFunction/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>helloworld</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>To Do Application Example.</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>lightweight-config</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-log4j2</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>11.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
<version>11.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>11.2</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.lambda.todo;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.baeldung.lambda.todo.config.ExecutionContext;
import com.baeldung.lambda.todo.service.PostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Handler for requests to Lambda function.
*/
public class App implements RequestStreamHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

private String environmentName = System.getenv("ENV_NAME");
private ExecutionContext executionContext = new ExecutionContext();

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
context.getLogger().log("App starting\n");
context.getLogger().log("Environment: "
+ environmentName + "\n");

try {
PostService postService = executionContext.getPostService();
executionContext.getToDoReaderService()
.getOldestToDo()
.ifPresent(postService::makePost);
} catch (Exception e) {
LOGGER.error("Failed: {}", e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.lambda.todo.api;

import feign.RequestLine;

public interface PostApi {
@RequestLine("POST /posts")
void makePost(PostItem item);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.baeldung.lambda.todo.api;

public class PostItem {
private String title;
private String body;
private int userId;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

@Override
public String toString() {
return "PostItem{"
+ "title='" + title + '\''
+ ", body='" + body + '\''
+ ", userId=" + userId +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.lambda.todo.api;

import feign.RequestLine;

import java.util.List;

public interface ToDoApi {
@RequestLine("GET /todos")
List<ToDoItem> getAllTodos();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung.lambda.todo.api;

public class ToDoItem {
private int userId;
private int id;
private String title;
private boolean completed;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public boolean isCompleted() {
return completed;
}

public void setCompleted(boolean completed) {
this.completed = completed;
}

@Override
public String toString() {
return "ToDoItem{"
+ "userId=" + userId
+ ", id=" + id
+ ", title='" + title + '\''
+ ", completed=" + completed +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung.lambda.todo.config;

public class Config {
private String toDoEndpoint;
private String postEndpoint;
private String environmentName;

private Credentials toDoCredentials;
private Credentials postCredentials;

public String getToDoEndpoint() {
return toDoEndpoint;
}

public void setToDoEndpoint(String toDoEndpoint) {
this.toDoEndpoint = toDoEndpoint;
}

public String getPostEndpoint() {
return postEndpoint;
}

public void setPostEndpoint(String postEndpoint) {
this.postEndpoint = postEndpoint;
}

public String getEnvironmentName() {
return environmentName;
}

public void setEnvironmentName(String environmentName) {
this.environmentName = environmentName;
}

public Credentials getToDoCredentials() {
return toDoCredentials;
}

public void setToDoCredentials(Credentials toDoCredentials) {
this.toDoCredentials = toDoCredentials;
}

public Credentials getPostCredentials() {
return postCredentials;
}

public void setPostCredentials(Credentials postCredentials) {
this.postCredentials = postCredentials;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.lambda.todo.config;

public class Credentials {
private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.lambda.todo.config;

import com.baeldung.lambda.todo.service.PostService;
import com.baeldung.lambda.todo.service.ToDoReaderService;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExecutionContext {
private static final Logger LOGGER =
LoggerFactory.getLogger(ExecutionContext.class);

private ToDoReaderService toDoReaderService;
private PostService postService;

public ExecutionContext() {
LOGGER.info("Loading configuration");

try {
Injector injector = Guice.createInjector(new Services());
this.toDoReaderService = injector.getInstance(ToDoReaderService.class);
this.postService = injector.getInstance(PostService.class);
} catch (Exception e) {
LOGGER.error("Could not start", e);
}
}

public ToDoReaderService getToDoReaderService() {
return toDoReaderService;
}

public PostService getPostService() {
return postService;
}
}
Loading