Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 0184c4e

Browse files
authored
Merge pull request #95 from graphql-java/websocket-support
Websocket support
2 parents fd2129a + 054c432 commit 0184c4e

File tree

42 files changed

+679
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+679
-85
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Oembedler Inc. and Contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
sourceCompatibility = 1.8
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
compile(project(":graphql-spring-boot-starter"))
13+
compile(project(":graphiql-spring-boot-starter"))
14+
compile "com.graphql-java:graphql-java-tools:$LIB_GRAPHQL_JAVA_TOOLS_VER"
15+
16+
compile "io.reactivex.rxjava2:rxjava:2.1.5"
17+
18+
compile("org.springframework.boot:spring-boot-starter-web:$LIB_SPRING_BOOT_VER")
19+
20+
testCompile "org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER"
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.oembedler.moon.graphql.boot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SubscriptionSampleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SubscriptionSampleApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.oembedler.moon.graphql.boot.publishers;
2+
3+
import com.oembedler.moon.graphql.boot.resolvers.StockPriceUpdate;
4+
import io.reactivex.BackpressureStrategy;
5+
import io.reactivex.Flowable;
6+
import io.reactivex.Observable;
7+
import io.reactivex.ObservableEmitter;
8+
import io.reactivex.observables.ConnectableObservable;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.stereotype.Component;
12+
13+
import java.math.BigDecimal;
14+
import java.math.RoundingMode;
15+
import java.time.LocalDateTime;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.Random;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.concurrent.Executors;
22+
import java.util.concurrent.ScheduledExecutorService;
23+
import java.util.concurrent.TimeUnit;
24+
25+
@Component
26+
public class StockTickerPublisher {
27+
28+
private static final Logger LOG = LoggerFactory.getLogger(StockTickerPublisher.class);
29+
30+
private final Flowable<StockPriceUpdate> publisher;
31+
32+
public StockTickerPublisher() {
33+
Observable<StockPriceUpdate> stockPriceUpdateObservable = Observable.create(emitter -> {
34+
35+
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
36+
executorService.scheduleAtFixedRate(newStockTick(emitter), 0, 2, TimeUnit.SECONDS);
37+
38+
});
39+
40+
ConnectableObservable<StockPriceUpdate> connectableObservable = stockPriceUpdateObservable.share().publish();
41+
connectableObservable.connect();
42+
43+
publisher = connectableObservable.toFlowable(BackpressureStrategy.BUFFER);
44+
}
45+
46+
private Runnable newStockTick(ObservableEmitter<StockPriceUpdate> emitter) {
47+
return () -> {
48+
List<StockPriceUpdate> stockPriceUpdates = getUpdates(rollDice(0, 5));
49+
if (stockPriceUpdates != null) {
50+
emitStocks(emitter, stockPriceUpdates);
51+
}
52+
};
53+
}
54+
55+
private void emitStocks(ObservableEmitter<StockPriceUpdate> emitter, List<StockPriceUpdate> stockPriceUpdates) {
56+
for (StockPriceUpdate stockPriceUpdate : stockPriceUpdates) {
57+
try {
58+
emitter.onNext(stockPriceUpdate);
59+
} catch (RuntimeException e) {
60+
LOG.error("Cannot send StockUpdate", e);
61+
}
62+
}
63+
}
64+
65+
public Flowable<StockPriceUpdate> getPublisher() {
66+
return publisher;
67+
}
68+
69+
public Flowable<StockPriceUpdate> getPublisher(List<String> stockCodes) {
70+
if (stockCodes != null) {
71+
return publisher.filter(stockPriceUpdate -> stockCodes.contains(stockPriceUpdate.getStockCode()));
72+
}
73+
return publisher;
74+
}
75+
76+
private List<StockPriceUpdate> getUpdates(int number) {
77+
List<StockPriceUpdate> updates = new ArrayList<>();
78+
for (int i = 0; i < number; i++) {
79+
updates.add(rollUpdate());
80+
}
81+
return updates;
82+
}
83+
84+
85+
private final static Map<String, BigDecimal> CURRENT_STOCK_PRICES = new ConcurrentHashMap<>();
86+
87+
static {
88+
CURRENT_STOCK_PRICES.put("TEAM", dollars(39, 64));
89+
CURRENT_STOCK_PRICES.put("IBM", dollars(147, 10));
90+
CURRENT_STOCK_PRICES.put("AMZN", dollars(1002, 94));
91+
CURRENT_STOCK_PRICES.put("MSFT", dollars(77, 49));
92+
CURRENT_STOCK_PRICES.put("GOOGL", dollars(1007, 87));
93+
}
94+
95+
private StockPriceUpdate rollUpdate() {
96+
ArrayList<String> STOCK_CODES = new ArrayList<>(CURRENT_STOCK_PRICES.keySet());
97+
98+
String stockCode = STOCK_CODES.get(rollDice(0, STOCK_CODES.size() - 1));
99+
BigDecimal currentPrice = CURRENT_STOCK_PRICES.get(stockCode);
100+
101+
102+
BigDecimal incrementDollars = dollars(rollDice(0, 1), rollDice(0, 99));
103+
if (rollDice(0, 10) > 7) {
104+
// 0.3 of the time go down
105+
incrementDollars = incrementDollars.negate();
106+
}
107+
BigDecimal newPrice = currentPrice.add(incrementDollars);
108+
109+
CURRENT_STOCK_PRICES.put(stockCode, newPrice);
110+
return new StockPriceUpdate(stockCode, LocalDateTime.now(), newPrice, incrementDollars);
111+
}
112+
113+
private static BigDecimal dollars(int dollars, int cents) {
114+
return truncate("" + dollars + "." + cents);
115+
}
116+
117+
private static BigDecimal truncate(final String text) {
118+
BigDecimal bigDecimal = new BigDecimal(text);
119+
if (bigDecimal.scale() > 2)
120+
bigDecimal = new BigDecimal(text).setScale(2, RoundingMode.HALF_UP);
121+
return bigDecimal.stripTrailingZeros();
122+
}
123+
124+
private final static Random rand = new Random();
125+
126+
private static int rollDice(int min, int max) {
127+
return rand.nextInt((max - min) + 1) + min;
128+
}
129+
130+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.oembedler.moon.graphql.boot.resolvers;
2+
3+
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
class Query implements GraphQLQueryResolver {
8+
9+
public String hello() {
10+
return "Hello world!";
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.oembedler.moon.graphql.boot.resolvers;
2+
3+
import java.math.BigDecimal;
4+
import java.time.LocalDateTime;
5+
import java.time.format.DateTimeFormatter;
6+
7+
public class StockPriceUpdate {
8+
9+
private final String stockCode;
10+
private final String dateTime;
11+
private final BigDecimal stockPrice;
12+
private final BigDecimal stockPriceChange;
13+
14+
public StockPriceUpdate(String stockCode, LocalDateTime dateTime, BigDecimal stockPrice, BigDecimal stockPriceChange) {
15+
this.stockCode = stockCode;
16+
this.dateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);
17+
this.stockPrice = stockPrice;
18+
this.stockPriceChange = stockPriceChange;
19+
}
20+
21+
public String getStockCode() {
22+
return stockCode;
23+
}
24+
25+
public String getDateTime() {
26+
return dateTime;
27+
}
28+
29+
public BigDecimal getStockPrice() {
30+
return stockPrice;
31+
}
32+
33+
public BigDecimal getStockPriceChange() {
34+
return stockPriceChange;
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.oembedler.moon.graphql.boot.resolvers;
2+
3+
import com.coxautodev.graphql.tools.GraphQLSubscriptionResolver;
4+
import com.oembedler.moon.graphql.boot.publishers.StockTickerPublisher;
5+
import org.reactivestreams.Publisher;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.List;
9+
10+
@Component
11+
class Subscription implements GraphQLSubscriptionResolver {
12+
13+
private StockTickerPublisher stockTickerPublisher;
14+
15+
Subscription(StockTickerPublisher stockTickerPublisher) {
16+
this.stockTickerPublisher = stockTickerPublisher;
17+
}
18+
19+
Publisher<StockPriceUpdate> stockQuotes(List<String> stockCodes) {
20+
return stockTickerPublisher.getPublisher(stockCodes);
21+
}
22+
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring:
2+
application:
3+
name: graphql-subscription-example
4+
server:
5+
port: 9000
6+
7+
graphql:
8+
servlet:
9+
subscriptions:
10+
websocket:
11+
path: /subscriptions

0 commit comments

Comments
 (0)