Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modern Java application example. #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
target
### Java template
*.class

*.iml

.idea

# Mobile Tools for Java (J2ME)
.mtj.tmp/


# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/

/src/main/resources/application-local.properties

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

### Maven template
target/
58 changes: 35 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
java-api-streaming
FX-OANDA-Streaming
==================

A simple demo app in Java for getting streaming rates using OANDA
API. This example uses [apache httpcomponents](http://hc.apache.org/httpcomponents-client-ga/) for https
connections and [json-simple](https://code.google.com/p/json-simple/) for json decoding.
A simple demo app in Java 8 using Spring Boot for getting streaming rates using OANDA
API. This example uses [Spring RestTemplate](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html) for https
connections.

### Setup

Clone this repo to the location of your choice.

Modify the following variables in
[JavaApiStreaming.java](src/main/java/JavaApiStreaming.java):
[application.properites](src/main/resources/application.properties):

domain
access_token
account_id
instruments
instruments

Make sure you separate the instruments by comma (e.g: EUR_USD,USD_JPY,EUR_JPY).


Maven is used for building. Install from http://maven.apache.org/download.cgi.
On Ubuntu, you can run `sudo apt-get install maven`.
On Ubuntu, you can run `sudo apt-get install maven`. On Mac using brew `brew install maven`

To create and execute the jar file, run

mvn package
java -jar target/streaming-example-java-1.jar
mvn clean package
java -jar target/oanda.streaming-0.0.1-SNAPSHOT.jar

### Sample Output

EUR_USD
2014-03-21T17:56:09.932922Z
1.37912
1.37923
-------
USD_CAD
2014-03-21T17:56:20.776248Z
1.12011
1.12029
-------
USD_JPY
2014-03-21T17:56:13.668154Z
102.262
102.275
2016-04-10 14:28:57.068 INFO 84369 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2016-04-10 14:28:57.121 INFO 84369 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-04-10 14:28:57.132 INFO 84369 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2016-04-10 14:28:57.305 INFO 84369 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-10 14:28:58.809 INFO 84369 --- [ restartedMain] i.c.fx.oanda.streaming.FXOANDAStreaming : {
"instrument" : "EUR_USD",
"bid" : 1.13929,
"ask" : 1.14029,
"time" : "2016-04-08T21:08:58.499"
}
2016-04-10 14:28:58.813 INFO 84369 --- [ restartedMain] i.c.fx.oanda.streaming.FXOANDAStreaming : {
"instrument" : "USD_JPY",
"bid" : 108.028,
"ask" : 108.128,
"time" : "2016-04-08T21:09:00.391"
}
2016-04-10 14:28:58.819 INFO 84369 --- [ restartedMain] i.c.fx.oanda.streaming.FXOANDAStreaming : {
"instrument" : "EUR_JPY",
"bid" : 123.133,
"ask" : 123.233,
"time" : "2016-04-08T21:08:59.84"
}


### More Information

Expand Down
86 changes: 37 additions & 49 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,69 +1,57 @@
<?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"
<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>Oanda</groupId>
<artifactId>streaming-example-java</artifactId>
<version>1</version>
<groupId>io.cax.fx</groupId>
<artifactId>oanda.streaming</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>oanda.streaming</name>
<description>Demo FX OANDA Streaming with Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
<scope>compile</scope>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<scope>compile</scope>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>dependency-jars/</classpathPrefix>
<mainClass>JavaApiStreaming</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>


</project>
80 changes: 0 additions & 80 deletions src/main/java/JavaApiStreaming.java

This file was deleted.

85 changes: 85 additions & 0 deletions src/main/java/io/cax/fx/oanda/streaming/FXOandaStreaming.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.cax.fx.oanda.streaming;

import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cax.fx.oanda.streaming.domain.Tick;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.util.Scanner;


@SpringBootApplication
public class FXOANDAStreaming implements CommandLineRunner{

Logger logger = LoggerFactory.getLogger(FXOANDAStreaming.class);

@Value("${oanda.access_token}")
private String accessToken;

@Value("${oanda.account_id}")
private String accountId;


@Value("${oanda.domain}")
private String domain;

@Value("${oanda.instruments}")
private String instruments;

private RestOperations restTemplate;

public static void main (String[]args) throws IOException {
SpringApplication.run(FXOANDAStreaming.class, args);
}

@Override
public void run(String... strings) throws Exception {

restTemplate = new RestTemplate();


UriComponents uriComponents = UriComponentsBuilder.fromUriString(domain).path("/v1/prices").queryParam("accountId",accountId).queryParam("instruments",instruments).build();

restTemplate.execute(uriComponents.toUriString(),
HttpMethod.GET,
clientHttpRequest -> clientHttpRequest.getHeaders().add("Authorization","Bearer " + accessToken),
clientHttpResponse -> {
try(Scanner scanner = new Scanner(clientHttpResponse.getBody(),"utf-8")){

while(scanner.hasNext()) formatPrintContent(scanner.nextLine());
}
return new ResponseEntity<>(HttpStatus.OK);
});
}

private void formatPrintContent(String content){

ObjectMapper mapper = new ObjectMapper();

try {

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Tick tick = mapper.readValue(content,Tick.class);
logger.info(mapper.writer(new DefaultPrettyPrinter()).writeValueAsString(tick));

} catch (IOException e) {
logger.debug("heartbeat not processed: " + e.getMessage());
}


}
}
Loading