Skip to content

Commit

Permalink
Merge branch 'springboot'
Browse files Browse the repository at this point in the history
  • Loading branch information
steveww committed Sep 1, 2020
2 parents 86bcdd5 + f0b4923 commit 5ef8c91
Show file tree
Hide file tree
Showing 15 changed files with 553 additions and 337 deletions.
7 changes: 3 additions & 4 deletions shipping/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#
# Build
#
FROM openjdk:8-jdk AS build
FROM debian:10 AS build

RUN apt-get update && apt-get -y install maven

WORKDIR /opt/shipping

COPY pom.xml /opt/shipping/
RUN mvn install

RUN mvn dependency:resolve
COPY src /opt/shipping/src/
RUN mvn package

Expand All @@ -25,7 +24,7 @@ WORKDIR /opt/shipping
ENV CART_ENDPOINT=cart:8080
ENV DB_HOST=mysql

COPY --from=build /opt/shipping/target/shipping-1.0-jar-with-dependencies.jar shipping.jar
COPY --from=build /opt/shipping/target/shipping-1.0.jar shipping.jar

CMD [ "java", "-Xmn256m", "-Xmx768m", "-jar", "shipping.jar" ]

126 changes: 60 additions & 66 deletions shipping/pom.xml
Original file line number Diff line number Diff line change
@@ -1,78 +1,72 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>steveww</groupId>
<artifactId>shipping</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Spark Java Sample</name>
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.instana</groupId>
<artifactId>shipping</artifactId>
<version>1.0</version>
<name>shipping service</name>
<description>Shipping calculations</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.7.2</version>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
<version>4.5.12</version>
</dependency>
</dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>org.steveww.spark.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package org.steveww.spark;
package com.instana.robotshop.shipping;

public class Location {
private double latitude;
private double longitude;

public Location(double latitude, double longitude) {
public class Calculator {
private double latitude = 0;
private double longitude = 0;

Calculator(double latitdue, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}

public double getLatitude() {
return this.latitude;
}

public double getLongitude() {
return this.longitude;
Calculator(City city) {
this.latitude = city.getLatitude();
this.longitude = city.getLongitude();
}


/**
* Calculate the distance between this location and the target location.
* Use decimal lat/long degrees
Expand All @@ -34,10 +31,13 @@ public long getDistance(double targetLatitude, double targetLongitude) {
double diffLatR = Math.toRadians(targetLatitude - this.latitude);
double diffLongR = Math.toRadians(targetLongitude - this.longitude);

double a = Math.sin(diffLatR / 2.0) * Math.sin(diffLatR / 2.0) + Math.cos(latitudeR) * Math.cos(targetLatitudeR) * Math.sin(diffLongR / 2.0) * Math.sin(diffLongR);
double a = Math.sin(diffLatR / 2.0) * Math.sin(diffLatR / 2.0)
+ Math.cos(latitudeR) * Math.cos(targetLatitudeR)
* Math.sin(diffLongR / 2.0) * Math.sin(diffLongR);

double c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));

return (long)Math.rint(earthRadius * c / 1000.0);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.instana.robotshop.shipping;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class CartHelper {
private static final Logger logger = LoggerFactory.getLogger(CartHelper.class);

private String baseUrl;

public CartHelper(String baseUrl) {
this.baseUrl = baseUrl;
}

// TODO - Remove deprecated calls
public String addToCart(String id, String data) {
logger.info("add shipping to cart {}", id);
StringBuilder buffer = new StringBuilder();

CloseableHttpClient httpClient = null;
try {
// set timeout to 5 secs
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);

httpClient = HttpClients.createDefault();
HttpPost postRequest = new HttpPost(baseUrl + id);
StringEntity payload = new StringEntity(data);
payload.setContentType("application/json");
postRequest.setEntity(payload);
CloseableHttpResponse res = httpClient.execute(postRequest);

if (res.getStatusLine().getStatusCode() == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
String line;
while ((line = in.readLine()) != null) {
buffer.append(line);
}
} else {
logger.warn("Failed with code {}", res.getStatusLine().getStatusCode());
}
try {
res.close();
} catch(IOException e) {
logger.warn("httpresponse", e);
}
} catch(Exception e) {
logger.warn("http client exception", e);
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch(IOException e) {
logger.warn("httpclient", e);
}
}
}

// this will be empty on error
return buffer.toString();
}
}
85 changes: 85 additions & 0 deletions shipping/src/main/java/com/instana/robotshop/shipping/City.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.instana.robotshop.shipping;

import javax.persistence.Table;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Column;

/*
* Bean for City
*/
@Entity
@Table(name = "cities")
public class City {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long uuid;

@Column(name = "country_code")
private String code;
private String city;
private String name;
private String region;
private double latitude;
private double longitude;

public long getUuid() {
return this.uuid;
}

public String getCode() {
return this.code;
}

public void setCode(String code) {
this.code = code;
}

public String getCity() {
return this.city;
}

public void setCity(String city) {
this.city = city;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getRegion() {
return this.region;
}

public void setRegion(String code) {
this.region = region;
}

public double getLatitude() {
return this.latitude;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
}

public double getLongitude() {
return this.longitude;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
}

@Override
public String toString() {
return String.format("Country: %s City: %s Region: %s Coords: %f %f", this.code, this.city, this.region, this.latitude, this.longitude);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.instana.robotshop.shipping;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.Query;

public interface CityRepository extends CrudRepository<City, Long> {
List<City> findByCode(String code);

@Query(
value = "select c from City c where c.code = ?1 and c.city like ?2%"
)
List<City> match(String code, String text);

City findById(long id);
}
Loading

0 comments on commit 5ef8c91

Please sign in to comment.