Skip to content

Commit edf4a49

Browse files
committed
feat:Support the callback of the notice message
1 parent 391a95d commit edf4a49

8 files changed

Lines changed: 202 additions & 10 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<groupId>com.webull.openapi</groupId>
2222
<artifactId>webull-openapi-java-sdk-parent</artifactId>
2323
<packaging>pom</packaging>
24-
<version>1.1.20</version>
24+
<version>1.1.21</version>
2525

2626
<name>webull-openapi-java-sdk-parent</name>
2727
<url>https://github.com/webull-inc/webull-openapi-java-sdk/</url>

‎webull-openapi-java-sdk-samples/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<groupId>com.webull.openapi</groupId>
2121
<artifactId>webull-openapi-java-sdk-parent</artifactId>
22-
<version>1.1.20</version>
22+
<version>1.1.21</version>
2323
</parent>
2424
<modelVersion>4.0.0</modelVersion>
2525

‎webull-openapi-java-sdk-samples/src/main/java/com/webull/openapi/samples/data/DataStreamingClient.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.webull.openapi.core.utils.GUID;
1111
import com.webull.openapi.data.quotes.subscribe.IDataStreamingClient;
1212
import com.webull.openapi.data.quotes.subscribe.message.MarketData;
13+
import com.webull.openapi.data.quotes.subscribe.message.NoticeData;
1314
import com.webull.openapi.samples.config.Env;
1415

1516
import java.util.HashSet;
@@ -42,6 +43,7 @@ public static void main(String[] args) {
4243
*/
4344
//.tokenDir("conf_custom_relative")
4445
.onMessage(DataStreamingClient::handleMarketData)
46+
.onNotice(DataStreamingClient::handleNotice)
4547
.addSubscription(symbols, Category.US_STOCK.name(), subTypes, "1", false)
4648
.build()) {
4749

@@ -62,6 +64,14 @@ private static void handleMarketData(MarketData marketData) {
6264
logger.info("Received market data: {}", JsonSerializer.toJson(marketData));
6365
}
6466

67+
private static void handleNotice(NoticeData notice) {
68+
if (notice.isPermissionGrabbed()) {
69+
logger.warn("Permission grabbed: {}", notice.getContent());
70+
} else {
71+
logger.info("Notice received: type={}, rtt={}, drop={}, sent={}",
72+
notice.getType(), notice.getRtt(), notice.getDrop(), notice.getSent());
73+
}
74+
}
6575

6676
private static void subscribeBlocking(IDataStreamingClient client) {
6777
client.connectBlocking();

‎webull-openapi-java-sdk/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.webull.openapi</groupId>
99
<artifactId>webull-openapi-java-sdk-parent</artifactId>
10-
<version>1.1.20</version>
10+
<version>1.1.21</version>
1111
</parent>
1212

1313
<artifactId>webull-openapi-java-sdk</artifactId>

‎webull-openapi-java-sdk/src/main/java/com/webull/openapi/data/internal/mqtt/DataStreamingClientBuilder.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@
3939
import com.webull.openapi.data.quotes.subscribe.lifecycle.QuotesSubsHandler;
4040
import com.webull.openapi.data.quotes.subscribe.lifecycle.QuotesSubsInboundHandler;
4141
import com.webull.openapi.data.quotes.subscribe.message.MarketData;
42+
import com.webull.openapi.data.quotes.subscribe.message.NoticeData;
4243
import com.webull.openapi.data.quotes.subscribe.proxy.ProxyConfig;
4344
import com.webull.openapi.data.quotes.subscribe.retry.QuotesSubsRetryCondition;
4445

46+
import java.util.ArrayList;
4547
import java.util.LinkedList;
48+
import java.util.List;
4649
import java.util.Set;
4750
import java.util.concurrent.TimeUnit;
4851
import java.util.function.Consumer;
@@ -67,6 +70,7 @@ public final class DataStreamingClientBuilder implements IDataStreamingClientBui
6770

6871
private final LinkedList<QuotesSubsHandler> handlers = new LinkedList<>();
6972
private final LinkedList<QuotesSubsInboundHandler> onMessages = new LinkedList<>();
73+
private final List<Consumer<NoticeData>> onNoticeHandlers = new ArrayList<>();
7074

7175
private Set<String> symbols;
7276
private String category;
@@ -177,6 +181,13 @@ public IDataStreamingClientBuilder onMessage(Consumer<MarketData> consumer) {
177181
return this;
178182
}
179183

184+
@Override
185+
public IDataStreamingClientBuilder onNotice(Consumer<NoticeData> consumer) {
186+
Assert.notNull("consumer", consumer);
187+
this.onNoticeHandlers.add(consumer);
188+
return this;
189+
}
190+
180191
@Override
181192
public IDataStreamingClientBuilder addSubscription(Set<String> symbols, String category, Set<String> subTypes, String depth, Boolean overnightRequired) {
182193
this.symbols = symbols;
@@ -228,8 +239,8 @@ public IDataStreamingClient build() {
228239
}
229240
allHandlers.add(subscriptionManager);
230241

231-
// add decoder
232-
allHandlers.add(new PublishToMarketDataDecoder());
242+
// add decoder (with notice handlers for notice topic dispatching)
243+
allHandlers.add(new PublishToMarketDataDecoder(this.onNoticeHandlers));
233244
// add on message handlers
234245
allHandlers.addAll(this.onMessages);
235246
// add other handlers

‎webull-openapi-java-sdk/src/main/java/com/webull/openapi/data/internal/mqtt/codec/PublishToMarketDataDecoder.java‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.webull.openapi.data.internal.mqtt.codec;
1717

18+
import com.google.gson.Gson;
1819
import com.webull.openapi.core.common.dict.SubscribeType;
1920
import com.webull.openapi.core.logger.Logger;
2021
import com.webull.openapi.core.logger.LoggerFactory;
@@ -23,21 +24,34 @@
2324
import com.webull.openapi.data.quotes.subscribe.codec.AbstractInboundDecoder;
2425
import com.webull.openapi.data.quotes.subscribe.message.MarketData;
2526
import com.webull.openapi.data.quotes.subscribe.message.Metadata;
27+
import com.webull.openapi.data.quotes.subscribe.message.NoticeData;
2628
import com.webull.openapi.data.quotes.subscribe.message.QuotesPublish;
2729

28-
import java.util.ArrayList;
30+
import java.nio.charset.StandardCharsets;
31+
import java.util.Collections;
2932
import java.util.EnumMap;
3033
import java.util.List;
3134
import java.util.Map;
3235
import java.util.Optional;
36+
import java.util.function.Consumer;
3337

3438
public class PublishToMarketDataDecoder extends AbstractInboundDecoder<MqttPublish, MarketData> {
3539

3640
private static final Logger logger = LoggerFactory.getLogger(PublishToMarketDataDecoder.class);
41+
private static final String ECHO_TOPIC = "echo";
42+
private static final String NOTICE_TOPIC = "notice";
43+
44+
private final Gson gson = new Gson();
45+
private final List<Consumer<NoticeData>> noticeHandlers;
3746

3847
private final Map<SubscribeType, QuotesPublishDecoder<? extends QuotesBasic>> delegates = new EnumMap<>(SubscribeType.class);
3948

4049
public PublishToMarketDataDecoder() {
50+
this(Collections.emptyList());
51+
}
52+
53+
public PublishToMarketDataDecoder(List<Consumer<NoticeData>> noticeHandlers) {
54+
this.noticeHandlers = noticeHandlers != null ? noticeHandlers : Collections.emptyList();
4155
delegates.put(SubscribeType.QUOTE, new QuoteDecoder());
4256
delegates.put(SubscribeType.SNAPSHOT, new SnapshotDecoder());
4357
delegates.put(SubscribeType.TICK, new TickDecoder());
@@ -49,14 +63,17 @@ public PublishToMarketDataDecoder() {
4963
@Override
5064
public MarketData decode(MqttPublish in) {
5165
String topic = in.getTopic();
52-
List<String> notDecodeTopic = new ArrayList<>();
53-
notDecodeTopic.add("echo");
54-
notDecodeTopic.add("notice");
55-
if (notDecodeTopic.contains(topic)) {
66+
67+
if (ECHO_TOPIC.equals(topic)) {
5668
logger.debug("No decoding is required, the type is {}.", topic);
5769
return null;
5870
}
5971

72+
if (NOTICE_TOPIC.equals(topic)) {
73+
handleNotice(in);
74+
return null;
75+
}
76+
6077
Optional<SubscribeType> subscribeTypeOpt = SubscribeType.fromType(topic);
6178
if (!subscribeTypeOpt.isPresent()) {
6279
logger.warn("Unrecognized data type={}.", topic);
@@ -67,4 +84,37 @@ public MarketData decode(MqttPublish in) {
6784
QuotesPublish<?> delegateOut = delegate.decode(in.getPayload());
6885
return new MarketData(metadata, delegateOut);
6986
}
87+
88+
private void handleNotice(MqttPublish in) {
89+
NoticeData notice = decodeNotice(in);
90+
if (notice == null) {
91+
return;
92+
}
93+
94+
logger.debug("Received notice message: {}.", notice);
95+
96+
// dispatch to notice handlers
97+
for (Consumer<NoticeData> handler : noticeHandlers) {
98+
try {
99+
handler.accept(notice);
100+
} catch (Exception e) {
101+
logger.error("Error in notice handler, type={}.", notice.getType(), e);
102+
}
103+
}
104+
}
105+
106+
private NoticeData decodeNotice(MqttPublish in) {
107+
try {
108+
byte[] payload = in.getPayloadAsBytes();
109+
if (payload == null || payload.length == 0) {
110+
logger.warn("Received empty notice message.");
111+
return null;
112+
}
113+
String json = new String(payload, StandardCharsets.UTF_8);
114+
return gson.fromJson(json, NoticeData.class);
115+
} catch (Exception e) {
116+
logger.error("Failed to decode notice message.", e);
117+
return null;
118+
}
119+
}
70120
}

‎webull-openapi-java-sdk/src/main/java/com/webull/openapi/data/quotes/subscribe/IDataStreamingClientBuilder.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.webull.openapi.data.quotes.api.subsribe.IMarketStreamingClient;
1919
import com.webull.openapi.data.quotes.subscribe.lifecycle.QuotesSubsHandler;
2020
import com.webull.openapi.data.quotes.subscribe.message.MarketData;
21+
import com.webull.openapi.data.quotes.subscribe.message.NoticeData;
2122
import com.webull.openapi.data.quotes.subscribe.proxy.ProxyConfig;
2223
import com.webull.openapi.data.quotes.subscribe.retry.QuotesSubsRetryCondition;
2324
import com.webull.openapi.core.retry.RetryPolicy;
@@ -64,6 +65,8 @@ default IDataStreamingClientBuilder reconnectBy(BackoffStrategy backoffStrategy)
6465

6566
IDataStreamingClientBuilder onMessage(Consumer<MarketData> consumer);
6667

68+
IDataStreamingClientBuilder onNotice(Consumer<NoticeData> consumer);
69+
6770
IDataStreamingClientBuilder addSubscription(Set<String> symbols, String category, Set<String> subTypes, String depth, Boolean overnightRequired);
6871

6972
IDataStreamingClient build();
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2022 Webull
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.webull.openapi.data.quotes.subscribe.message;
17+
18+
/**
19+
* Notice message received from the server via MQTT "notice" topic.
20+
* <p>
21+
* Two types of notice messages:
22+
* <ul>
23+
* <li>type=1001: Status message (rtt, drop, sent)</li>
24+
* <li>type=1002: Permission grabbed message (content)</li>
25+
* </ul>
26+
*/
27+
public class NoticeData {
28+
29+
/**
30+
* Notice type: "1001" for status, "1002" for permission grabbed.
31+
*/
32+
private String type;
33+
34+
/**
35+
* Client-server latency in ms (type=1001).
36+
*/
37+
private Long rtt;
38+
39+
/**
40+
* Server dropped message count (type=1001).
41+
*/
42+
private Long drop;
43+
44+
/**
45+
* Server sent message count (type=1001).
46+
*/
47+
private Long sent;
48+
49+
/**
50+
* Notice content, e.g. permission grabbed reason (type=1002).
51+
*/
52+
private String content;
53+
54+
public String getType() {
55+
return type;
56+
}
57+
58+
public void setType(String type) {
59+
this.type = type;
60+
}
61+
62+
public Long getRtt() {
63+
return rtt;
64+
}
65+
66+
public void setRtt(Long rtt) {
67+
this.rtt = rtt;
68+
}
69+
70+
public Long getDrop() {
71+
return drop;
72+
}
73+
74+
public void setDrop(Long drop) {
75+
this.drop = drop;
76+
}
77+
78+
public Long getSent() {
79+
return sent;
80+
}
81+
82+
public void setSent(Long sent) {
83+
this.sent = sent;
84+
}
85+
86+
public String getContent() {
87+
return content;
88+
}
89+
90+
public void setContent(String content) {
91+
this.content = content;
92+
}
93+
94+
/**
95+
* Whether this is a permission grabbed notice.
96+
*/
97+
public boolean isPermissionGrabbed() {
98+
return "1002".equals(type);
99+
}
100+
101+
/**
102+
* Whether this is a status notice.
103+
*/
104+
public boolean isStatus() {
105+
return "1001".equals(type);
106+
}
107+
108+
@Override
109+
public String toString() {
110+
return "NoticeData{" +
111+
"type='" + type + '\'' +
112+
", rtt=" + rtt +
113+
", drop=" + drop +
114+
", sent=" + sent +
115+
", content='" + content + '\'' +
116+
'}';
117+
}
118+
}

0 commit comments

Comments
 (0)