1010import io .grpc .Status ;
1111import io .grpc .StatusRuntimeException ;
1212import java .util .Iterator ;
13- import java .util .List ;
14- import java .util .Map ;
1513
1614// Installation
1715// https://search.maven.org/artifact/com.authzed.api/authzed
@@ -35,33 +33,14 @@ public static void main(String[] args) {
3533 ManagedChannel channel = ManagedChannelBuilder
3634 .forTarget (target )
3735 .useTransportSecurity () // if not using TLS, replace with .usePlaintext()
38- .disableServiceConfigLookUp ()
39- .defaultServiceConfig (Map .of (
40- "methodConfig" , List .of (
41- Map .of (
42- "name" , List .of (
43- Map .of (
44- "service" , "authzed.api.v1.WatchService" ,
45- "method" , "Watch"
46- )
47- ),
48- "retryPolicy" , Map .of (
49- "maxAttempts" , "5" ,
50- "initialBackoff" , "1s" ,
51- "backoffMultiplier" , "4.0" ,
52- "maxBackoff" , "30s" ,
53- "retryableStatusCodes" , List .of ("UNAVAILABLE" , "INTERNAL" )
54- )
55- )
56- )
57- ))
5836 .build ();
5937
6038 ZedToken lastZedToken = ZedToken .newBuilder ().setToken ("" ).build ();
6139
6240 while (true ) {
6341 try {
6442 WatchRequest .Builder builder = WatchRequest .newBuilder ();
43+ builder .addOptionalUpdateKinds (com .authzed .api .v1 .WatchKind .WATCH_KIND_INCLUDE_CHECKPOINTS );
6544
6645 if (!lastZedToken .getToken ().isEmpty ()) {
6746 builder .setOptionalStartCursor (lastZedToken );
@@ -73,21 +52,21 @@ public static void main(String[] args) {
7352
7453 while (watchStream .hasNext ()) {
7554 WatchResponse msg = watchStream .next ();
76- System .out .println ("Received watch response: " + msg );
55+ if (msg .getUpdatesCount () > 0 ) {
56+ for (var update : msg .getUpdatesList ()) {
57+ System .out .println ("Received update: " + update );
58+ }
59+ } else {
60+ System .out .println ("No changes made in SpiceDB" );
61+ }
7762
7863 if (!msg .getChangesThrough ().getToken ().isEmpty ()) {
7964 lastZedToken = msg .getChangesThrough ();
8065 }
8166 }
82-
8367 } catch (Exception e ) {
84- if (e instanceof StatusRuntimeException sre && (sre .getStatus ().getCode ().equals (Status .UNAVAILABLE .getCode ()) ||
85- sre .getStatus ().getCode ().equals (Status .INTERNAL .getCode ()))) {
86- // Stream probably got disconnected after inactivity. Retry
87- } else {
88- System .out .println ("Error calling watch: " + e .getMessage ());
89- return ;
90- }
68+ System .out .println ("Error calling watch: " + e .getMessage ());
69+ return ;
9170 }
9271 }
9372 }
0 commit comments