In our last blog, we talked about the various prerequisites needed to implement cloudstate with Java. In this blog we will look into few more points to keep in mind while implementing cloudstate using Java. So lets go ahead!
The Protobuf files
In our previous blog we read about the Protoc compiler which is required to compile gRPC protobuf descriptors. We also learned that this can also be done by installing build tools which already contain a protoc plugin which will automatically compile protobuf descriptors during your build. Lets see one the plugin for that: Xolstice Maven Protocol Buffers Plugin
This plugin basically assumes a location which would be src/main/proto for our protobuf files. It also includes any protobuf files coming from our Java dependencies in the protoc include path. The mere advantage of this is that we do not need to do anything to pull in the Cloudstate protobuf types or any of the Google standard protobuf types. As said they are all automatically available for import.
For example, if we simply paste our protobuf into the location src/main/proto/ and we can also define the Java package to ensure the package name used by:
option java_package = "com.example";
Now if we run mvn compile, we’ll find the generated protobuf files in the location target/generated-sources/protobuf/java.
Creating a main class
The next important thing to discuss is about the main class in our program. Our main class is important because it will be responsible for
- Creating the Cloudstate gRPC server
- Registering the entities for it to serve
- And ofcourse starting it.
To do this, we can use the CloudState server builder, for example:
import com.example.BankAccount;
import io.cloudstate.javasupport.CloudState;
public class BankAccountMain {
public static void main(String... args) {
new CloudState()
.registerEventSourcedEntity(
BankAccountEntity.class,
BankAccount.getDescriptor().findServiceByName("BankAccount"))
.start();
}
}
Injecting Parameters
In order to make cloudstate entities work, we need to annotate classes and methods. It’ll be instantiated and invoked by the Cloudstate server. The methods and constructors invoked by the server can be injected with parameters of various types from the context of the invocation.
Exactly which context parameters are available depend on the type of entity and the type of handler. The order of the parameters in the method signature can be anything, parameters are matched by type and sometimes by annotation.
Below are some context parameters that are available in every context:
Type | Description |
Context | Every invoker makes a subtype of this available for injection, and method or constructor may accept that sub type, or any super type of that subtype that is a subtype of Context. |
java.lang.String | The entity’s ID. Annotation for this type is @EntityId |
Metadata | The metadata associated with the command. It is transport specific metadata.The exact semantics of how metadata is handled depends on the underlying transport. This API exposes case insensitive lookups on metadata, but maintains the original case of the keys as received or inserted. |
CloudEvent | CloudEvent representation of Metadata associated with the command. |
In conclusion, we can say now that we have all the prerequisites to get started with cloudstate using Java. This is all for this blog. Stay tuned for more blogs on Cloudstate with Java! 🙂
Awesome blog, really loved your content.