diff --git a/market-stomp-client-spring-boot/src/main/java/com/example/config/AppConfig.java b/market-stomp-client-spring-boot/src/main/java/com/example/config/AppConfig.java new file mode 100644 index 0000000..59d57ba --- /dev/null +++ b/market-stomp-client-spring-boot/src/main/java/com/example/config/AppConfig.java @@ -0,0 +1,32 @@ +package com.example.config; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.converter.MappingJackson2MessageConverter; +import org.springframework.web.socket.client.WebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.socket.messaging.WebSocketStompClient; +import org.springframework.web.socket.sockjs.client.SockJsClient; +import org.springframework.web.socket.sockjs.client.Transport; +import org.springframework.web.socket.sockjs.client.WebSocketTransport; + +@Configuration +public class AppConfig { + @Bean + public WebSocketClient webSocketClient() { + return new StandardWebSocketClient(); + } + + @Bean + public WebSocketStompClient webSocketStompClient(WebSocketClient client) { + List transports = new ArrayList<>(1); + transports.add(new WebSocketTransport(client)); + WebSocketClient transport = new SockJsClient(transports); + final WebSocketStompClient webSocketStompClient = new WebSocketStompClient(transport); + webSocketStompClient.setMessageConverter(new MappingJackson2MessageConverter()); + return webSocketStompClient; + } +} diff --git a/market-stomp-client-spring-boot/src/main/java/com/example/event/TradeEvent.java b/market-stomp-client-spring-boot/src/main/java/com/example/event/TradeEvent.java new file mode 100644 index 0000000..242abc0 --- /dev/null +++ b/market-stomp-client-spring-boot/src/main/java/com/example/event/TradeEvent.java @@ -0,0 +1,96 @@ +package com.example.event; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class TradeEvent { + 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 + "]"; + } + + +} diff --git a/market-stomp-client-spring-boot/src/main/java/com/example/service/MarketStompClientService.java b/market-stomp-client-spring-boot/src/main/java/com/example/service/MarketStompClientService.java new file mode 100644 index 0000000..120688f --- /dev/null +++ b/market-stomp-client-spring-boot/src/main/java/com/example/service/MarketStompClientService.java @@ -0,0 +1,57 @@ +package com.example.service; + +import java.lang.reflect.Type; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.messaging.simp.stomp.StompCommand; +import org.springframework.messaging.simp.stomp.StompHeaders; +import org.springframework.messaging.simp.stomp.StompSession; +import org.springframework.messaging.simp.stomp.StompSessionHandler; +import org.springframework.stereotype.Service; +import org.springframework.web.socket.messaging.WebSocketStompClient; + +import com.example.event.TradeEvent; + +@Service +public class MarketStompClientService implements StompSessionHandler { + @Autowired + private WebSocketStompClient stompClient; + @Value("${stomp.url}") + private String stompUrl; + + @PostConstruct + public void connect() { + stompClient.connect(stompUrl, this); + } + + @Override + public Type getPayloadType(StompHeaders stompHeaders) { + return TradeEvent.class; + } + + @Override + public void handleFrame(StompHeaders header, Object payload) { + TradeEvent trade = (TradeEvent) payload; + System.err.println("Message received: " + trade); + } + + @Override + public void afterConnected(StompSession session, StompHeaders headers) { + System.err.println("Connected!"); + session.subscribe("/topic/changes", this); + } + + @Override + public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] arg3, + Throwable error) { + System.err.println(error); + } + + @Override + public void handleTransportError(StompSession session, Throwable error) { + System.err.println(error); + } +} diff --git a/market-stomp-client-spring-boot/src/main/resources/application.properties b/market-stomp-client-spring-boot/src/main/resources/application.properties new file mode 100644 index 0000000..c2ec944 --- /dev/null +++ b/market-stomp-client-spring-boot/src/main/resources/application.properties @@ -0,0 +1,3 @@ +server.address=localhost +server.port=5050 +stomp.url=ws://localhost:7070/changes \ No newline at end of file