Skip to content

Commit b47057c

Browse files
committed
first commit
0 parents  commit b47057c

4 files changed

Lines changed: 240 additions & 0 deletions

File tree

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# this file is only used for GitHub
2+
# it is not related to your Kafka Streams learning
3+
4+
language: java
5+
6+
script:
7+
# test the java components
8+
- mvn clean verify

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Learning
3+
4+
This is a companion repository for my [**Amazon EC2 course on Udemy**](https://www.udemy.com/aws-ec2-masterclass/?couponCode=GITHUB)
5+
6+
Happy learning!
7+
8+
<p align="center">
9+
<a href="https://www.udemy.com/aws-ec2-masterclass/?couponCode=GITHUB">
10+
<img src="https://udemy-images.udemy.com/course/480x270/1527740_a494.jpg" alt="AWS EC2 Course Logo"/>
11+
</a>
12+
</p>
13+
14+
# Content
15+
16+
- Companion Java Application we'll use for this course

pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.simplesteph.udemy</groupId>
8+
<artifactId>ec2-masterclass-sample-app</artifactId>
9+
<version>1.0</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.sparkjava</groupId>
14+
<artifactId>spark-core</artifactId>
15+
<version>2.7.1</version>
16+
</dependency>
17+
18+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
19+
<dependency>
20+
<groupId>org.slf4j</groupId>
21+
<artifactId>slf4j-api</artifactId>
22+
<version>1.7.25</version>
23+
</dependency>
24+
25+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
26+
<dependency>
27+
<groupId>org.slf4j</groupId>
28+
<artifactId>slf4j-simple</artifactId>
29+
<version>1.7.25</version>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<!--force java 8-->
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.6.1</version>
41+
<configuration>
42+
<source>1.8</source>
43+
<target>1.8</target>
44+
</configuration>
45+
</plugin>
46+
47+
<!--package as one fat jar-->
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-assembly-plugin</artifactId>
51+
<version>3.1.0</version>
52+
<configuration>
53+
<descriptorRefs>
54+
<descriptorRef>jar-with-dependencies</descriptorRef>
55+
</descriptorRefs>
56+
<archive>
57+
<manifest>
58+
<addClasspath>true</addClasspath>
59+
<mainClass>com.github.simplesteph.udemy.EC2SampleApp</mainClass>
60+
</manifest>
61+
</archive>
62+
</configuration>
63+
<executions>
64+
<execution>
65+
<id>assemble-all</id>
66+
<phase>package</phase>
67+
<goals>
68+
<goal>single</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
77+
</project>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.github.simplesteph.udemy;
2+
3+
import spark.Request;
4+
import spark.Response;
5+
6+
import java.math.BigInteger;
7+
import java.net.InetAddress;
8+
import java.net.UnknownHostException;
9+
import java.text.NumberFormat;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import static spark.Spark.get;
14+
import static spark.Spark.threadPool;
15+
16+
public class EC2SampleApp {
17+
18+
private static List<String> myBigList = new ArrayList<>();
19+
private static Boolean isHealthy = true;
20+
21+
public static void main(String[] args) {
22+
23+
int maxThreads = 32;
24+
int minThreads = 2;
25+
int timeOutMillis = 30000;
26+
threadPool(maxThreads, minThreads, timeOutMillis);
27+
28+
get("/", EC2SampleApp::hello);
29+
get("/cpu", EC2SampleApp::cpu);
30+
get("/ram", EC2SampleApp::ram);
31+
get("/ram/info", EC2SampleApp::ramInfo);
32+
get("/ram/clean", EC2SampleApp::ramClean);
33+
get("/health", EC2SampleApp::health);
34+
get("/health/flip", EC2SampleApp::flipHealth);
35+
get("/details", EC2SampleApp::details);
36+
}
37+
38+
private static String hello(Request request, Response response) throws UnknownHostException {
39+
StringBuilder sb = new StringBuilder();
40+
41+
String myHostname = InetAddress.getLocalHost().getHostName();
42+
sb.append("Hello World By: ").append(myHostname).append("<br/>");
43+
String sourceIP = request.ip();
44+
sb.append("Receive Request From: ").append(sourceIP).append("<br/>");
45+
return sb.toString();
46+
}
47+
48+
49+
private static String cpu(Request request, Response response) {
50+
long currentTime = System.currentTimeMillis();
51+
BigInteger computation = fib(10000);
52+
return "fib function took " + (System.currentTimeMillis() - currentTime) + " milliseconds";
53+
}
54+
55+
56+
private static String ram(Request request, Response response) {
57+
try {
58+
String myBigString = new String(new char[10000000]);
59+
myBigList.add(myBigString);
60+
return ramInfo(request, response);
61+
} catch (OutOfMemoryError e){
62+
return "OutOfMemory Exception!";
63+
}
64+
}
65+
66+
private static String ramInfo(Request request, Response response) {
67+
return getRamInfo();
68+
}
69+
70+
private static String ramClean(Request request, Response response) {
71+
myBigList.clear();
72+
return ramInfo(request, response);
73+
}
74+
75+
76+
private static String health(Request request, Response response) {
77+
if (isHealthy){
78+
return "Healthy!";
79+
} else {
80+
response.status(401);
81+
return "Not healthy";
82+
}
83+
}
84+
85+
private static String flipHealth(Request request, Response response) {
86+
isHealthy = !isHealthy;
87+
return health(request, response);
88+
}
89+
90+
91+
private static String details(Request request, Response response) throws UnknownHostException {
92+
StringBuilder sb = new StringBuilder();
93+
String myHostname = InetAddress.getLocalHost().getHostName();
94+
sb.append("Hello World By: ").append(myHostname).append("<br/>");
95+
String sourceIP = request.ip();
96+
sb.append("Receive Request From: ").append(sourceIP).append("<br/>");
97+
sb.append("Request Headers are: ").append("<br/>");
98+
request.headers().forEach(header -> sb.append(header).append(": ").append(request.headers(header)).append("<br/>"));
99+
return sb.toString();
100+
}
101+
102+
// Fibonacci sequence to perform some long computations
103+
private static BigInteger fib(long nth){
104+
nth = nth - 1;
105+
long count = 0;
106+
BigInteger first = BigInteger.ZERO;
107+
BigInteger second = BigInteger.ONE;
108+
109+
BigInteger third = null;
110+
while(count < nth){
111+
third = new BigInteger(first.add(second).toString());
112+
first = new BigInteger(second.toString());
113+
second = new BigInteger(third.toString());
114+
count++;
115+
}
116+
117+
return third;
118+
}
119+
120+
121+
// get some RAM statistics
122+
private static String getRamInfo() {
123+
124+
Runtime runtime = Runtime.getRuntime();
125+
NumberFormat format = NumberFormat.getInstance();
126+
127+
StringBuilder sb = new StringBuilder();
128+
long maxMemory = runtime.maxMemory();
129+
long allocatedMemory = runtime.totalMemory();
130+
long freeMemory = runtime.freeMemory();
131+
132+
sb.append("free memory: ").append(format.format(freeMemory / 1024)).append("<br/>");
133+
sb.append("allocated memory: ").append(format.format(allocatedMemory / 1024)).append("<br/>");
134+
sb.append("max memory: ").append(format.format(maxMemory / 1024)).append("<br/>");
135+
sb.append("total free memory: ").append(format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024)).append("<br/>");
136+
return sb.toString();
137+
}
138+
139+
}

0 commit comments

Comments
 (0)