-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSubscriptionClient.java
More file actions
90 lines (78 loc) · 2.96 KB
/
SubscriptionClient.java
File metadata and controls
90 lines (78 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.chainbuff.grpc;
import geyser.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class SubscriptionClient {
private final ManagedChannel channel;
private final GeyserGrpc.GeyserStub stub;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final AtomicInteger retryCount = new AtomicInteger(0);
public SubscriptionClient(String target) {
this(ManagedChannelBuilder.forTarget(target).build());
}
SubscriptionClient(ManagedChannel channel) {
this.channel = channel;
this.stub = GeyserGrpc.newStub(channel);
}
public void shutdown() {
if (channel != null) {
channel.shutdown();
try {
if (!channel.awaitTermination(5, TimeUnit.SECONDS)) {
channel.shutdownNow();
}
} catch (InterruptedException e) {
channel.shutdownNow();
}
}
scheduler.shutdown();
System.out.println("Shutting down");
}
public void subscribe(SubscribeRequest request,SubscribeDataCallback callback) {
StreamObserver<SubscribeUpdate> responseObserver = new StreamObserver<>() {
@Override
public void onNext(SubscribeUpdate data) {
if (retryCount.get()> 0){
retryCount.set(0);
}
if (callback != null){
callback.onData(data);
}
}
@Override
public void onError(Throwable t) {
System.err.println("Subscription error: " + t.getMessage());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//重试5次
if (retryCount.get() < 5){
retryCount.incrementAndGet();
subscribe(request,callback);
}else {
shutdown();
}
}
@Override
public void onCompleted() {
System.out.println("Subscription completed");
}
};
StreamObserver<SubscribeRequest> subscribe = this.stub.subscribe(responseObserver);
subscribe.onNext(request);
//定时发送ping保命
this.scheduler.scheduleAtFixedRate(() -> {
SubscribeRequest.Builder builder = SubscribeRequest.newBuilder();
builder.setCommitment(CommitmentLevel.CONFIRMED);
builder.setPing(SubscribeRequestPing.newBuilder().setId(1).build());
subscribe.onNext(builder.build());
}, 5, 5, TimeUnit.SECONDS);
}
}