Cloudstate with Java (Part 2): Getting Started with Java

Reading Time: 2 minutes

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

  1. Creating the Cloudstate gRPC server
  2. Registering the entities for it to serve
  3. 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()

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:

ContextEvery 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.StringThe entity’s ID. Annotation for this type is @EntityId
MetadataThe 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.
CloudEventCloudEvent 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! 🙂

Written by 

Anjali Sharma is a passionate and detail-oriented software consultant with more than 1.5 years of experience. She thrives in a fast-paced and creative environment. She is recognized for exhibiting a positive attitude and high energy. She is a good team player and believes in taking ownership of her work. Her hobbies include dancing and reading books. She always focuses on delivering quality work and practicing good coding standards.

1 thought on “Cloudstate with Java (Part 2): Getting Started with Java3 min read

Comments are closed.