1515 */
1616package com .webull .openapi .data .internal .mqtt .codec ;
1717
18+ import com .google .gson .Gson ;
1819import com .webull .openapi .core .common .dict .SubscribeType ;
1920import com .webull .openapi .core .logger .Logger ;
2021import com .webull .openapi .core .logger .LoggerFactory ;
2324import com .webull .openapi .data .quotes .subscribe .codec .AbstractInboundDecoder ;
2425import com .webull .openapi .data .quotes .subscribe .message .MarketData ;
2526import com .webull .openapi .data .quotes .subscribe .message .Metadata ;
27+ import com .webull .openapi .data .quotes .subscribe .message .NoticeData ;
2628import 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 ;
2932import java .util .EnumMap ;
3033import java .util .List ;
3134import java .util .Map ;
3235import java .util .Optional ;
36+ import java .util .function .Consumer ;
3337
3438public 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}
0 commit comments