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,60 +33,28 @@ 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
60- ZedToken lastZedToken = ZedToken .newBuilder ().setToken ("" ).build ();
38+ try {
39+ WatchRequest request = WatchRequest .
40+ newBuilder ()
41+ .addOptionalUpdateKinds (com .authzed .api .v1 .WatchKind .WATCH_KIND_INCLUDE_CHECKPOINTS )
42+ .build ();
6143
62- while (true ) {
63- try {
64- WatchRequest .Builder builder = WatchRequest .newBuilder ();
44+ Iterator <WatchResponse > watchStream = watchClient .watch (request );
6545
66- if (!lastZedToken .getToken ().isEmpty ()) {
67- builder .setOptionalStartCursor (lastZedToken );
68- }
69-
70- WatchRequest request = builder .build ();
71-
72- Iterator <WatchResponse > watchStream = watchClient .watch (request );
73-
74- while (watchStream .hasNext ()) {
75- WatchResponse msg = watchStream .next ();
76- System .out .println ("Received watch response: " + msg );
77-
78- if (!msg .getChangesThrough ().getToken ().isEmpty ()) {
79- lastZedToken = msg .getChangesThrough ();
46+ while (watchStream .hasNext ()) {
47+ WatchResponse msg = watchStream .next ();
48+ if (msg .getUpdatesCount () > 0 ) {
49+ for (var update : msg .getUpdatesList ()) {
50+ System .out .println ("Received update: " + update );
8051 }
81- }
82-
83- } 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
8752 } else {
88- System .out .println ("Error calling watch: " + e .getMessage ());
89- return ;
53+ System .out .println ("No changes made in SpiceDB" );
9054 }
9155 }
56+ } catch (Exception e ) {
57+ System .out .println ("Error calling watch: " + e .getMessage ());
9258 }
9359 }
9460}
0 commit comments