syntax = "proto3"; message MyMessage { string name = 1; int32 age = 2; } import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import akka.actor.UntypedActor; import com.example.MyMessageProtos.MyMessage; public class MyActor extends UntypedActor { @Override public void onReceive(Object message) throws Exception { if (message instanceof MyMessage) { MyMessage myMessage = (MyMessage) message; System.out.println("Received: " + myMessage); } } public static void main(String[] args) { ActorSystem system = ActorSystem.create("MySystem"); ActorRef actor = system.actorOf(Props.create(MyActor.class)); MyMessage myMessage = MyMessage.newBuilder() .setName("John") .setAge(30) .build(); actor.tell(myMessage, null); system.terminate(); } } <dependencies> <!-- Akka --> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor_2.12</artifactId> <version>2.6.16</version> </dependency> <!-- Akka Protobuf V3 --> <dependency> <groupId>com.lightbend.akka</groupId> <artifactId>akka-protobuf-v3_2.12</artifactId> <version>3.0.0</version> </dependency> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.15.8:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.40.0:exe:${os.detected.classifier}</pluginArtifact> <protoSourceRoot>src/main/protobuf</protoSourceRoot> <clearOutputDirectory>false</clearOutputDirectory> <outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory> </configuration> <executions> <execution> <id>protobuf-generate</id> <goals> <goal>compile</goal> </goals> <phase>generate-sources</phase> </execution> </executions> </plugin> </dependencies>


上一篇:
下一篇:
切换中文