Skip to content

Commit c049eec

Browse files
committed
Initial version for GitHub
1 parent 1e833a8 commit c049eec

File tree

17 files changed

+822
-2
lines changed

17 files changed

+822
-2
lines changed

.classpath

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>amazon-sqs-java-temporary-queues-client-samples</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Amazon Sqs Java Temporary Queues Client Samples
1+
## Amazon SQS Java Temporary Queues Client Samples
22

3-
Example code for the Amazon SQS temporary queues client (https://github.com/awslabs/amazon-sqs-java-temporary-queues-client )
3+
Example code for the Amazon SQS temporary queues client (https://github.com/awslabs/amazon-sqs-java-temporary-queues-client)
44

55
## License
66

pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.amazonaws</groupId>
4+
<artifactId>amazon-sqs-java-temporary-queues-client-samples</artifactId>
5+
<version>0.9.0</version>
6+
<scm>
7+
<url>https://github.com/aws-samples/amazon-sqs-java-temporary-queues-client-samples.git</url>
8+
</scm>
9+
<url>https://github.com/aws-samples/amazon-sqs-java-temporary-queues-client-samples</url>
10+
11+
<licenses>
12+
<license>
13+
<name>Apache License, Version 2.0</name>
14+
<url>https://aws.amazon.com/apache2.0</url>
15+
<distribution>repo</distribution>
16+
</license>
17+
</licenses>
18+
19+
<developers>
20+
<developer>
21+
<id>amazonwebservices</id>
22+
<organization>Amazon Web Services</organization>
23+
<organizationUrl>https://aws.amazon.com</organizationUrl>
24+
<roles>
25+
<role>developer</role>
26+
</roles>
27+
</developer>
28+
</developers>
29+
30+
<dependencyManagement>
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.amazonaws</groupId>
34+
<artifactId>aws-java-sdk-bom</artifactId>
35+
<version>1.11.446</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>com.amazonaws</groupId>
45+
<artifactId>amazon-sqs-java-temporary-queues-client</artifactId>
46+
<version>0.9.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.amazonaws</groupId>
50+
<artifactId>aws-lambda-java-events</artifactId>
51+
<version>2.2.3</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.amazonaws</groupId>
55+
<artifactId>aws-lambda-java-core</artifactId>
56+
<version>1.1.0</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>4.11</version>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<pluginManagement>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>3.2</version>
73+
<configuration>
74+
<source>1.8</source>
75+
<target>1.8</target>
76+
<encoding>UTF-8</encoding>
77+
</configuration>
78+
</plugin>
79+
</plugins>
80+
</pluginManagement>
81+
</build>
82+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.amazonaws.services.sqs;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.ThreadLocalRandom;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.TimeoutException;
9+
10+
import com.amazonaws.regions.Regions;
11+
import com.amazonaws.services.sqs.model.SendMessageRequest;
12+
13+
public class WidgetStore {
14+
15+
private static boolean running = true;
16+
private static AmazonSQSRequester sqsRequester;
17+
private static String requestQueueUrl;
18+
19+
public static void main(String[] args) throws Exception {
20+
requestQueueUrl = args[0];
21+
System.out.println("Starting up store using queue: " + requestQueueUrl);
22+
23+
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
24+
sqsRequester = AmazonSQSRequesterClientBuilder.standard()
25+
.withAmazonSQS(sqs)
26+
.build();
27+
28+
shutdownHooks(sqs);
29+
30+
while (running) {
31+
Thread.sleep(ThreadLocalRandom.current().nextInt(3000) + 3000);
32+
33+
try {
34+
requestResponseLoop();
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
}
40+
41+
private static void requestResponseLoop() throws InterruptedException, ExecutionException {
42+
String requestBody = "Dude, we need more widgets!";
43+
System.out.println("Sending request: " + requestBody);
44+
SendMessageRequest request = new SendMessageRequest()
45+
.withQueueUrl(requestQueueUrl)
46+
.withMessageBody(requestBody);
47+
48+
try {
49+
String responseBody = sqsRequester.sendMessageAndGetResponse(
50+
request, 5, TimeUnit.SECONDS).getBody();
51+
System.out.println("Received response: " + responseBody);
52+
} catch (TimeoutException e) {
53+
System.out.println("Got tired of waiting for response :(");
54+
}
55+
}
56+
57+
private static void shutdownHooks(AmazonSQS sqs) {
58+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
59+
System.out.println("Shutting down!");
60+
sqs.shutdown();
61+
}));
62+
63+
Thread shutterDowner = new Thread((Runnable)(() -> {
64+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
65+
try {
66+
while (!reader.readLine().equals("exit"));
67+
} catch (Exception e) {
68+
}
69+
running = false;
70+
System.exit(0);
71+
}), "WidgetStoreShutterDowner");
72+
shutterDowner.start();
73+
}
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.amazonaws.services.sqs;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
import com.amazonaws.regions.Regions;
6+
import com.amazonaws.services.sqs.util.SQSMessageConsumer;
7+
8+
public class WidgetSupplier {
9+
10+
public static boolean running = true;
11+
12+
public static void main(String[] args) throws Exception {
13+
String queueUrl = args[0];
14+
System.out.println("Starting up supplier using queue: " + queueUrl);
15+
16+
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
17+
AmazonSQSResponder responder = AmazonSQSResponderClientBuilder.standard()
18+
.withAmazonSQS(sqs)
19+
.build();
20+
21+
SQSMessageConsumer consumer = new SQSMessageConsumer(responder.getAmazonSQS(), queueUrl, message -> {
22+
int x = ThreadLocalRandom.current().nextInt(10) + 1;
23+
String responseBody = "Here are " + x + " more widgets. Enjoy!";
24+
System.out.println("Sending reply: " + responseBody);
25+
responder.sendResponseMessage(MessageContent.fromMessage(message),
26+
new MessageContent(responseBody));
27+
});
28+
consumer.start();
29+
30+
while (running) {};
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.lu.order;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.lu.util.concurrent.CannotExecuteOrderException;
7+
8+
public class Order {
9+
int customerId;
10+
List<Integer> products;
11+
12+
public Order(int customerId)
13+
{
14+
this.customerId = customerId;
15+
this.products = new ArrayList<>();
16+
}
17+
18+
// For the benefit of easy JSON serialization
19+
public Order()
20+
{
21+
}
22+
23+
public void AddEntry(int sku)
24+
{
25+
products.add(sku);
26+
}
27+
28+
public int GetNumberOfEntries()
29+
{
30+
return products.size();
31+
}
32+
33+
public int getCustomerId()
34+
{
35+
return customerId;
36+
}
37+
38+
public List<Integer> getProducts() {
39+
return products;
40+
}
41+
42+
public void validate() {
43+
if (customerId < 0) {
44+
throw new CannotExecuteOrderException("customerId must be positive: " + customerId, null);
45+
}
46+
}
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lu.util.concurrent;
2+
3+
public class CannotExecuteOrderException extends RuntimeException {
4+
public CannotExecuteOrderException(String errorMessage, Throwable err){
5+
super(errorMessage, err);
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lu.util.concurrent;
2+
3+
import com.lu.order.Order;
4+
5+
import java.util.UUID;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.TimeoutException;
8+
9+
public interface IOrderExecuter {
10+
CompletableFuture<UUID> submitOrderAsync(Order order) throws CannotExecuteOrderException;
11+
12+
UUID submitOrder(Order order) throws TimeoutException, CannotExecuteOrderException;
13+
14+
void shutdown();
15+
}

0 commit comments

Comments
 (0)