-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42e233e
commit c78c031
Showing
4 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
market-stomp-client-spring-boot/src/main/java/com/example/config/AppConfig.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 |
---|---|---|
@@ -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<Transport> 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; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
market-stomp-client-spring-boot/src/main/java/com/example/event/TradeEvent.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 |
---|---|---|
@@ -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 + "]"; | ||
} | ||
|
||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...-stomp-client-spring-boot/src/main/java/com/example/service/MarketStompClientService.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 |
---|---|---|
@@ -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); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
market-stomp-client-spring-boot/src/main/resources/application.properties
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
server.address=localhost | ||
server.port=5050 | ||
stomp.url=ws://localhost:7070/changes |