Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Jun 25, 2020
1 parent 7bad1ed commit 92f8ec3
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.lottery.client;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.TimeUnit;

public class MarketApiHttpClient {

private static final String URL = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT";

public static void main(String[] args) throws IOException, InterruptedException {
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder().uri(URI.create(URL)).header("Accept", "application/json").build();
while(true) {
var response = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
System.out.println(response);
TimeUnit.SECONDS.sleep(1);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.lottery.client;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.net.http.WebSocket.Listener;
import java.util.concurrent.CompletionStage;

public class MarketApiWebsocketClient {
private static final String URL = "wss://stream.binance.com:9443/ws/btcusdt@trade";

public static void main(String[] args) throws InterruptedException {
Listener listener = new MarketWebsocketListener();
HttpClient.newHttpClient().newWebSocketBuilder().buildAsync(URI.create(URL),listener);
Thread.sleep(60_000);
}
}

class MarketWebsocketListener implements Listener {

@Override
public void onOpen(WebSocket webSocket) {
System.err.println("Connected to the market.");
webSocket.request(1);
}

@Override
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
System.err.println(data);
webSocket.request(1);
return null;
}

@Override
public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason) {
System.err.println("Connection is closed!");
return null;
}

@Override
public void onError(WebSocket webSocket, Throwable error) {
System.err.println("An error has occured: "+error.getMessage()+" at session "+webSocket);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.lottery.service;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.lottery.dto.LotteryResponse;

import io.github.resilience4j.ratelimiter.annotation.RateLimiter;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
@Service
public class LotteryConsumerServiceWithRateLimiter {

@RateLimiter(name = "lotterySrvLimiter")
public LotteryResponse getLotteryNumbers() {
System.err.println("Calling lottery service from LotteryConsumerServiceWithRateLimiter...");
var rt = new RestTemplate();
var response = rt.getForObject("http://localhost:8001/lottery/api/v1/numbers?column=10", LotteryResponse.class);
System.err.println(response);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import org.springframework.retry.RetryCallback;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.lottery.dto.LotteryResponse;

@Service
//@Service
public class LotteryConsumerServiceWithRetryTemplate {
@Autowired
private RetryTemplate retryTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.web.client.RestTemplate;

import com.example.lottery.dto.LotteryResponse;
import com.google.inject.Exposed;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.main.banner-mode=off

server.address=localhost
server.port=5001

Expand Down
63 changes: 63 additions & 0 deletions market-websocket-client-spring-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>market-websocket-client-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>market-websocket-client-spring-boot</name>
<description>Spring Boot Project for Market Client</description>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>

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

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;

@Configuration
public class WebsocketClientConfig {
@Bean
public WebSocketClient webSocketClient() {
return new StandardWebSocketClient();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/ws");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/changes").setAllowedOrigins("*").withSockJS();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.example.event;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import com.fasterxml.jackson.annotation.JsonProperty;

@Document(collection = "trades")
public class TradeEvent {
@Id
private String id;
@JsonProperty("s")
private String symbol;
@JsonProperty("p")
private String price;
@JsonProperty("q")
private String quantity;
@JsonProperty("T")
private long timestamp;
@JsonProperty("t")
private long eventId;
@JsonProperty("a")
private long askId;
@JsonProperty("b")
private long bidId;

public TradeEvent() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getQuantity() {
return quantity;
}

public void setQuantity(String quantity) {
this.quantity = quantity;
}

public long getTimestamp() {
return timestamp;
}

public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

public long getEventId() {
return eventId;
}

public void setEventId(long eventId) {
this.eventId = eventId;
}

public long getAskId() {
return askId;
}

public void setAskId(long askId) {
this.askId = askId;
}

public long getBidId() {
return bidId;
}

public void setBidId(long bidId) {
this.bidId = bidId;
}

@Override
public String toString() {
return "TradeEvent [id=" + id + ", symbol=" + symbol + ", price=" + price + ", quantity=" + quantity
+ ", timestamp=" + timestamp + ", eventId=" + eventId + ", askId=" + askId + ", bidId=" + bidId + "]";
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.example.event.TradeEvent;

public interface TradeEventRepository extends MongoRepository<TradeEvent, String> {

}
Loading

0 comments on commit 92f8ec3

Please sign in to comment.