Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
make use of new quote service.
  • Loading branch information
JaredGordon committed Sep 9, 2015
1 parent 663fe78 commit a724282
Show file tree
Hide file tree
Showing 38 changed files with 698 additions and 833 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Expand Up @@ -67,7 +67,7 @@ subprojects { subproject ->
ext.springWsVersion = '2.0.3.RELEASE'
ext.springIntegrationVersion = '2.2.6.RELEASE'
ext.springRooVersion = '1.2.0.RELEASE'
ext.springCloudVersion = '1.0.0.RELEASE'
ext.springCloudVersion = '1.2.0.RELEASE'
ext.javaxInjectVersion = '1'
ext.servletApiVersion = '2.5'
ext.dozerVersion='5.3.2'
Expand Down Expand Up @@ -197,6 +197,9 @@ project('spring-nanotrader-data') {
compile "org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion"
compile "com.h2database:h2:$h2Version"
compile "mysql:mysql-connector-java:5.1.13"
compile "com.netflix.feign:feign-gson:8.5.0"
compile "com.google.code.gson:gson:2.3.1"
compile "com.jayway.jsonpath:json-path:0.9.1"
}
}

Expand Down
2 changes: 2 additions & 0 deletions deleteDeployment.sh
Expand Up @@ -6,6 +6,7 @@ backName=traderback
domain=cfapps.io
sqlName=tradersql
messagingName=tradermessaging
quoteName=quoteService

date

Expand All @@ -15,6 +16,7 @@ cf delete -f $backName
cf delete-service -f $frontName
cf delete-service -f $sqlName
cf delete-service -f $messagingName
cf delete-service -f $quoteName
cf delete-route $domain -f -n $frontName
cf delete-route $domain -f -n $webName
cf delete-route $domain -f -n $backName
Expand Down
4 changes: 4 additions & 0 deletions deployApp.sh
Expand Up @@ -4,6 +4,7 @@ frontName=traderfront
webName=traderweb
backName=traderback
domain=cfapps.io
quoteName=quoteService
sqlName=tradersql
messagingName=tradermessaging

Expand All @@ -12,11 +13,13 @@ date
echo Creating service instances
cf create-service cleardb spark $sqlName
cf create-service cloudamqp lemur $messagingName
cf cups $quoteName -p '{ "quoteServiceuri": "http://real-time-quote-service.cfapps.io/quotes" }'

echo Deploying front end services tier
cf push -p dist/spring-nanotrader-services-1.0.1.BUILD-SNAPSHOT.war -m 1G -t 180 -d $domain -n $frontName --no-start $frontName
cf bind-service $frontName $sqlName
cf bind-service $frontName $messagingName
cf bind-service $frontName $quoteName
cf set-env $frontName JBP_CONFIG_OPEN_JDK_JRE '[jre: {version: 1.7.0_+}]'
cf set-env $frontName JBP_CONFIG_TOMCAT '[tomcat: {version: 7.0.+}]'
cf push -p dist/spring-nanotrader-services-1.0.1.BUILD-SNAPSHOT.war -m 1G -t 180 -d $domain -n $frontName $frontName
Expand All @@ -37,6 +40,7 @@ cf set-env $backName JBP_CONFIG_OPEN_JDK_JRE '[jre: {version: 1.7.0_+}]'
cf set-env $backName JBP_CONFIG_TOMCAT '[tomcat: {version: 7.0.+}]'
cf bind-service $backName $sqlName
cf bind-service $backName $messagingName
cf bind-service $backName $quoteName
cf push -p dist/spring-nanotrader-asynch-services-1.0.1.BUILD-SNAPSHOT.war -m 1G -t 180 -d $domain -n $backName $backName

date
@@ -0,0 +1,32 @@
package org.springframework.nanotrader.data.cloud;

import java.util.List;

import org.springframework.nanotrader.data.domain.MarketSummary;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.stereotype.Repository;

import feign.Param;
import feign.RequestLine;

@Repository
public interface QuoteRepository {

@RequestLine("GET /{symbol}")
public Quote findBySymbol(@Param(value = "symbol") String symbol);

@RequestLine("GET /")
public List<Quote> findAll();

@RequestLine("GET /symbols")
List<String> symbols();

@RequestLine("GET /marketSummary")
MarketSummary marketSummary();

@RequestLine("GET /topGainers")
List<Quote> topGainers();

@RequestLine("GET /topLosers")
List<Quote> topLosers();
}
@@ -0,0 +1,31 @@
package org.springframework.nanotrader.data.cloud;

import java.net.URL;

import org.springframework.cloud.service.AbstractServiceConnectorCreator;
import org.springframework.cloud.service.ServiceConnectorConfig;

import feign.Feign;
import feign.gson.GsonEncoder;

public class QuoteRepositoryConnectionCreator extends
AbstractServiceConnectorCreator<QuoteRepository, WebServiceInfo> {

@Override
public QuoteRepository create(WebServiceInfo serviceInfo,
ServiceConnectorConfig serviceConnectorConfig) {
return createRepository(serviceInfo.getUri());
}

public QuoteRepository createRepository(String url) {
QuoteRepository qr = Feign.builder().encoder(new GsonEncoder())
.decoder(new RealTimeQuoteDecoder())
.target(QuoteRepository.class, url);

return qr;
}

public QuoteRepository createRepository(URL url) {
return createRepository(url.toString());
}
}
@@ -0,0 +1,26 @@
package org.springframework.nanotrader.data.cloud;

import java.util.Map;

import org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator;
import org.springframework.cloud.cloudfoundry.Tags;

public class QuoteWebServiceInfoCreator extends
CloudFoundryServiceInfoCreator<WebServiceInfo> {

public static final String QUOTES_PREFIX = "quoteService";

public QuoteWebServiceInfoCreator() {
super(new Tags(), QUOTES_PREFIX);
}

@Override
public WebServiceInfo createServiceInfo(Map<String, Object> serviceData) {
String id = (String) serviceData.get("name");

Map<String, Object> credentials = getCredentials(serviceData);
String uri = getUriFromCredentials(credentials);

return new WebServiceInfo(id, uri);
}
}
@@ -0,0 +1,139 @@
package org.springframework.nanotrader.data.cloud;

import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import net.minidev.json.JSONArray;

import org.springframework.nanotrader.data.domain.MarketSummary;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.util.NumberUtils;

import com.google.gson.reflect.TypeToken;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;

import feign.FeignException;
import feign.Response;
import feign.codec.DecodeException;
import feign.gson.GsonDecoder;

public class RealTimeQuoteDecoder extends GsonDecoder {

@Override
public Object decode(Response response, Type type) throws IOException,
DecodeException, FeignException {

Response.Body body = response.body();
if (body == null) {
return null;
}

// System.out.println(Util.toString(body.asReader()));

Type typeOfListOfQuote = new TypeToken<List<Quote>>() {
}.getType();
if (Quote.class.equals(type) || typeOfListOfQuote.equals(type)) {
return processQuoteBody(body);
}

if (MarketSummary.class.equals(type)) {
return processMarketSummaryBody(body);
}

return super.decode(response, type);
}

private BigDecimal getBigDecimal(ReadContext ctx, String path) {
Object o = ctx.read(path);
if (o == null) {
return new BigDecimal(0);
}
return NumberUtils.parseNumber(o.toString(), BigDecimal.class);
}

private Object processMarketSummaryBody(Response.Body body)
throws IOException {
ReadContext ctx = JsonPath.parse(body.asInputStream());

MarketSummary m = new MarketSummary();
m.setChange(getBigDecimal(ctx, "$.change"));
m.setSummaryDate(new Date());
m.setTradeStockIndexAverage(getBigDecimal(ctx, "$.average"));
m.setTradeStockIndexOpenAverage(getBigDecimal(ctx, "$.open"));
m.setTradeStockIndexVolume(getBigDecimal(ctx, "$.volume"));

return m;
}

private Object processQuoteBody(Response.Body body) throws IOException {
ReadContext ctx = JsonPath.parse(body.asInputStream());

// is this a single quote, or a collection?
if (ctx.read("$.").getClass().equals(JSONArray.class)) {
return quotesFromJson(ctx);
}

return quoteFromJson(ctx);
}

private Quote quoteFromJson(ReadContext ctx) {
Quote q = new Quote();
q.setChange1(getBigDecimal(ctx, "$.Change"));
q.setCompanyname(ctx.read("$.Name").toString());
q.setHigh(getBigDecimal(ctx, "$.DaysHigh"));
q.setLow(getBigDecimal(ctx, "$.DaysLow"));
q.setOpen1(getBigDecimal(ctx, "$.Open"));
q.setPrice(getBigDecimal(ctx, "$.Price"));
q.setSymbol(ctx.read("$.Symbol").toString());
q.setVolume(getBigDecimal(ctx, "$.Volume"));

return q;
}

private List<Quote> quotesFromJson(ReadContext ctx) {
ArrayList<Quote> quotes = new ArrayList<Quote>();

JSONArray qs = ctx.read("$..");
for (int i = 0; i < qs.size(); i++) {
Quote q = new Quote();
q.setChange1(getBigDecimal(ctx, "$..[" + i + "].Change"));
q.setCompanyname(ctx.read("$..[" + i + "].Name").toString());
q.setHigh(getBigDecimal(ctx, "$..[" + i + "].DaysHigh"));
q.setLow(getBigDecimal(ctx, "$..[" + i + "].DaysLow"));
q.setOpen1(getBigDecimal(ctx, "$..[" + i + "].PreviousClose"));
q.setPrice(getBigDecimal(ctx, "$..[" + i + "].Price"));
q.setSymbol(ctx.read("$..[" + i + "].Symbol").toString());
q.setVolume(getBigDecimal(ctx, "$..[" + i + "].Volume"));

quotes.add(q);
}

return quotes;
}

public static String formatSymbols(Set<String> symbols) {
if (symbols == null || symbols.size() < 1) {
return "()";
}

Object[] o = symbols.toArray();

StringBuffer sb = new StringBuffer("(");
for (int i = 0; i < o.length; i++) {
sb.append("\'");
sb.append(o[i]);
sb.append("\'");
if (i < o.length - 1) {
sb.append(",");
}
}
sb.append(")");
return sb.toString();
}
}
@@ -0,0 +1,9 @@
package org.springframework.nanotrader.data.cloud;

import org.springframework.cloud.service.UriBasedServiceInfo;

public class WebServiceInfo extends UriBasedServiceInfo {
public WebServiceInfo(String id, String url) {
super(id, url);
}
}
Expand Up @@ -84,12 +84,17 @@ public void setOrderid(Integer id) {
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "M-")
private Date opendate;

@ManyToOne
@JoinColumn(name = "quote_symbol", referencedColumnName = "symbol")
private Quote quote;

@Column(name = "quoteid")
private String quoteid;

public String getQuoteid() {
return quoteid;
}

public void setQuoteid(String s) {
this.quoteid = s;
}

public Account getAccountAccountid() {
return accountAccountid;
Expand Down Expand Up @@ -163,16 +168,6 @@ public void setOpendate(Date opendate) {
this.opendate = opendate;
}



public Quote getQuote() {
return quote;
}

public void setQuote(Quote quote) {
this.quote = quote;
}

@Override
public String toString() {
return "Order [orderid=" + orderid + ", orderfee=" + orderfee + ", completiondate=" + completiondate
Expand Down

0 comments on commit a724282

Please sign in to comment.