Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created November 5, 2025 08:10
Show Gist options
  • Save maxandersen/80202a0c0c9ebedb48ca43193b85955b to your computer and use it in GitHub Desktop.
Save maxandersen/80202a0c0c9ebedb48ca43193b85955b to your computer and use it in GitHub Desktop.
//DEPS com.cajunsystems:cajun:0.1.4
//JAVA 21+
//PREVIEW
import com.cajunsystems.*;
import com.cajunsystems.handler.Handler;
public class HelloWorld {
// Define your message types with explicit replyTo
public record HelloMessage(String name, Pid replyTo) {}
public record GreetingResponse(String greeting) {}
// Greeter actor that processes requests
public static class GreeterHandler implements Handler<HelloMessage> {
@Override
public void receive(HelloMessage message, ActorContext context) {
context.getLogger().info("Received greeting request for: {}", message.name());
// Process and reply
String greeting = "Hello, " + message.name() + "!";
context.tell(message.replyTo(), new GreetingResponse(greeting));
}
}
// Receiver actor that handles responses
public static class ReceiverHandler implements Handler<GreetingResponse> {
@Override
public void receive(GreetingResponse message, ActorContext context) {
System.out.println("Received: " + message.greeting());
}
}
public static void main(String[] args) throws Exception {
// 1. Create the ActorSystem
ActorSystem system = new ActorSystem();
// 2. Spawn actors
Pid greeter = system.actorOf(GreeterHandler.class)
.withId("greeter")
.spawn();
Pid receiver = system.actorOf(ReceiverHandler.class)
.withId("receiver")
.spawn();
// 3. Send a message with explicit replyTo
greeter.tell(new HelloMessage("World", receiver));
// Wait a bit for async processing
Thread.sleep(100);
// 4. Shutdown the system when done
system.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment