This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from joaopsilva/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 622
Error handling and shutdown improvements #49
Merged
joaopsilva
merged 5 commits into
binance-exchange:master
from
big-andy-coates:error_handling_and_shutdown
Feb 23, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
18e3b0a
Improvements to error handling and the ability to shutdown the client…
c3cd9e1
Merge branch 'master' into error_handling_and_shutdown
big-andy-coates a10dc0c
-Removed unused imports
8664140
Add error handling and closure of websockets to readme
37ef9a0
code review improvements
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
src/main/java/com/binance/api/client/BinanceApiCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
package com.binance.api.client; | ||
|
||
import com.binance.api.client.exception.BinanceApiException; | ||
|
||
/** | ||
* BinanceApiCallback is a functional interface used together with the BinanceApiAsyncClient to provide a non-blocking REST client. | ||
* | ||
* @param <T> the return type from the callback | ||
*/ | ||
@FunctionalInterface | ||
public interface BinanceApiCallback<T> { | ||
|
||
/** | ||
* Called whenever a response comes back from the Binance API. | ||
* | ||
* @param response the expected response object | ||
* @throws BinanceApiException if it is not possible to obtain the expected response object (e.g. incorrect API-KEY). | ||
*/ | ||
void onResponse(T response) throws BinanceApiException; | ||
void onResponse(T response); | ||
|
||
/** | ||
* Called whenever an error occurs. | ||
* | ||
* @param cause the cause of the failure | ||
*/ | ||
default void onFailure(Throwable cause) {} | ||
} |
53 changes: 46 additions & 7 deletions
53
src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,64 @@ | ||
package com.binance.api.client; | ||
|
||
import java.util.List; | ||
import com.binance.api.client.domain.event.AggTradeEvent; | ||
import com.binance.api.client.domain.event.AllMarketTickersEvent; | ||
import com.binance.api.client.domain.event.CandlestickEvent; | ||
import com.binance.api.client.domain.event.DepthEvent; | ||
import com.binance.api.client.domain.event.UserDataUpdateEvent; | ||
import com.binance.api.client.domain.market.CandlestickInterval; | ||
|
||
import java.io.Closeable; | ||
import java.util.List; | ||
|
||
/** | ||
* Binance API data streaming façade, supporting streaming of events through web sockets. | ||
*/ | ||
public interface BinanceApiWebSocketClient { | ||
public interface BinanceApiWebSocketClient extends Closeable { | ||
|
||
/** | ||
* Open a new web socket to receive {@link DepthEvent depthEvents} on a callback. | ||
* | ||
* @param symbol the market symbol to subscribe to | ||
* @param callback the callback to call on new events | ||
* @return a {@link Closeable} that allows the underlying web socket to be closed. | ||
*/ | ||
Closeable onDepthEvent(String symbol, BinanceApiCallback<DepthEvent> callback); | ||
|
||
void onDepthEvent(String symbol, BinanceApiCallback<DepthEvent> callback); | ||
/** | ||
* Open a new web socket to receive {@link CandlestickEvent candlestickEvents} on a callback. | ||
* | ||
* @param symbol the market symbol to subscribe to | ||
* @param interval the interval of the candles tick events required | ||
* @param callback the callback to call on new events | ||
* @return a {@link Closeable} that allows the underlying web socket to be closed. | ||
*/ | ||
Closeable onCandlestickEvent(String symbol, CandlestickInterval interval, BinanceApiCallback<CandlestickEvent> callback); | ||
|
||
void onCandlestickEvent(String symbol, CandlestickInterval interval, BinanceApiCallback<CandlestickEvent> callback); | ||
/** | ||
* Open a new web socket to receive {@link AggTradeEvent aggTradeEvents} on a callback. | ||
* | ||
* @param symbol the market symbol to subscribe to | ||
* @param callback the callback to call on new events | ||
* @return a {@link Closeable} that allows the underlying web socket to be closed. | ||
*/ | ||
Closeable onAggTradeEvent(String symbol, BinanceApiCallback<AggTradeEvent> callback); | ||
|
||
void onAggTradeEvent(String symbol, BinanceApiCallback<AggTradeEvent> callback); | ||
/** | ||
* Open a new web socket to receive {@link UserDataUpdateEvent userDataUpdateEvents} on a callback. | ||
* | ||
* @param listenKey the listen key to subscribe to. | ||
* @param callback the callback to call on new events | ||
* @return a {@link Closeable} that allows the underlying web socket to be closed. | ||
*/ | ||
Closeable onUserDataUpdateEvent(String listenKey, BinanceApiCallback<UserDataUpdateEvent> callback); | ||
|
||
void onUserDataUpdateEvent(String listenKey, BinanceApiCallback<UserDataUpdateEvent> callback); | ||
/** | ||
* Open a new web socket to receive {@link AllMarketTickersEvent allMarketTickersEvents} on a callback. | ||
* | ||
* @param callback the callback to call on new events | ||
* @return a {@link Closeable} that allows the underlying web socket to be closed. | ||
*/ | ||
Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTickersEvent>> callback); | ||
|
||
void onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTickersEvent>> callback); | ||
void close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there should still be some sort of notification to inform about the closure, because a regular API user might not see the closure of the socket, leaving him wondering why his application no longer works after a time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, without this, when the user calls
close
on the web socket, they user would receive aEOFException
on this onFailure callback. This is not intuitive IMHO.The flag stops the
EOFException
that occurs in response to the user'sclose
request.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea but if the user does not close it and does not check onFailure, then the socket closes unnoticed.