Skip to content

Commit 99624a6

Browse files
committed
docs: add DemoActor implementation and client usage example (fixes #639)
1 parent d536af6 commit 99624a6

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

  • sdkdocs/java/content/en/java-sdk-docs/java-client

sdkdocs/java/content/en/java-sdk-docs/java-client/_index.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,61 @@ public interface DemoActor {
307307
}
308308
```
309309

310+
### Actor implementation
311+
Below is an example implementation of the `DemoActor`:
312+
313+
```java
314+
import io.dapr.actors.runtime.AbstractActor;
315+
import io.dapr.actors.runtime.ActorHost;
316+
import reactor.core.publisher.Mono;
317+
318+
public class DemoActorImpl extends AbstractActor implements DemoActor {
319+
320+
public DemoActorImpl(ActorHost host) {
321+
super(host);
322+
}
323+
324+
@Override
325+
public void registerReminder() {
326+
// Example: register a reminder (implementation omitted for brevity)
327+
}
328+
329+
@Override
330+
public String say(String something) {
331+
return "Echo: " + something;
332+
}
333+
334+
@Override
335+
public void clock(String message) {
336+
System.out.println("Clock message: " + message);
337+
}
338+
339+
@Override
340+
public Mono<Integer> incrementAndGet(int delta) {
341+
return Mono.just(delta);
342+
}
343+
}
344+
```
345+
### Calling an actor from a client
346+
You can invoke actor methods using the Dapr client:
347+
348+
```java
349+
import io.dapr.client.DaprClient;
350+
import io.dapr.client.DaprClientBuilder;
351+
352+
try (DaprClient client = new DaprClientBuilder().build()) {
353+
String result = client.invokeActorMethod(
354+
"DemoActor",
355+
"myActorId",
356+
"say",
357+
"Hello World",
358+
String.class
359+
).block();
360+
361+
System.out.println(result);
362+
}
363+
```
364+
310365
- For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{% ref howto-actors.md %}}).
311366
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/actors) for code samples and instructions to try actors
312367

0 commit comments

Comments
 (0)