Giter VIP home page Giter VIP logo

kalix-demo-digitaltwin-java's Introduction

IoT Digital Twin DEMO - Java - Event Sourced

Not supported by Lightbend in any conceivable way, not open for contributions.

Prerequisite

  • Java 11 or later
  • Apache Maven 3.6 or higher
  • Kalix:
    • Register account: Register
    • kalix tool installed: Kalix CLI
    • kalix login
    • project demo created and set for kalix
  • Docker 20.10.8 or higher (engine and client)
  • Docker Hub account (configured with Docker)
    Access to the gcr.io/kalix-public container registry
    cURL
    IDE / editor

Generate Java project (terminal)

mvn archetype:generate \
-DarchetypeGroupId=io.kalix \
-DarchetypeArtifactId=kalix-maven-archetype \
-DarchetypeVersion=LATEST
Define value for property 'groupId': com.example
Define value for property 'artifactId': digitaltwin
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.example: : com.example.digitaltwin

Import in IDE

Cleanup (IDE)

Delete:
src/main/proto/com/example/shoppingcart/counter_api.proto
src/main/proto/com/example/shoppingcart/domain/counter_domain.proto

API descriptor - endpoints (IDE)

Note: For code snippet insertion use command+J (MAC)

  1. Create file digitaltwin_api.proto in src/main/proto/com/example/digitaltwin folder.
  2. Edit src/main/proto/com/example/digitaltwin/digitaltwin_api.proto in IDE
  3. Insert header snippet: aheader
  4. Insert commands snippet: acmd
  5. Insert state snippet: astate
  6. Insert service snippet: asrv
  7. Add functions to service snippet (place cursor inside brackets service DigitalTwinService { }): afunc

API descriptor - domain (IDE)

  1. Create file digitaltwin_domain.proto in src/main/proto/com/example/digitaltwin/domain folder.
  2. Edit src/main/proto/com/example/digitaltwin/domain/digitaltwin_domain.proto in IDE
  3. Insert header snippet: dheader
  4. Insert events snippet: devts
  5. Insert state snippet: dstate

API descriptor - codegen annotations (IDE)

  1. Edit src/main/proto/com/example/digitaltwin/digitaltwin_api.proto
  2. Insert codegen annotations (place cursor under service DigitalTwinService { ): acodegen

Codegen

  1. Code generation (terminal):
mvn compile
  1. Refresh project (IDE)
  2. Trigger Maven sync (IDE)

Business logic implementation (IDE)

  1. Edit src/main/java/com/example/digitaltwin/domain/DigitalTwin class
  2. Delete class body
  3. Insert code snippet (delete everything under constructor): eall

Implement unit test

  1. Edit src/test/java/com/example/digitaltwin/domain/DigitalTwinTest class
  2. Delete class body
  3. Insert code snippet (delete everything under constructor): ut

Run unit test (terminal)

mvn test

Implement integration test (IDE)

  1. Edit src/it/java/com/example/digitaltwin/DigitalTwinIntegrationTest class
  2. Delete everything under the constructor
  3. Insert code snippet (delete everything under constructor): it

Run integration test (terminal)

mvn -Pit verify

Run locally

??

Package & deploy

  1. Edit pom.xml and update my-docker-repo in <dockerImage>my-docker-repo/${project.artifactId}</dockerImage>
  2. Execute in terminal:
mvn deploy

Expose service

kalix services expose digitaltwin
Service 'digitaltwin' was successfully exposed at: winter-bonus-0316.eu-central-1.kalix.app

Note: HOSTNAME to use for external access

Test service in production

  1. Create digital twin
curl -XPOST -d '{
  "name": "DT1",
  "metric_value_alert_threshold": "10"
}' https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/1/create -H "Content-Type: application/json"
  1. Add OK metric
curl -XPOST -d '{
  "metric_value": "5"
}' https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/1/add-metric -H "Content-Type: application/json"
  1. Get cart
curl -XGET https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/1 -H "Content-Type: application/json"
  1. Add ALERT metric
curl -XPOST -d '{
  "metric_value": "11"
}' https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/1/add-metric -H "Content-Type: application/json"
  1. Get cart
curl -XGET https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/1 -H "Content-Type: application/json"

Eventing (optional)

  1. Create file digitaltwin_topic.proto in src/main/proto/com/example/digitaltwin folder.

  2. Edit src/main/proto/com/example/digitaltwin/digitaltwin_topic.proto in IDE

  3. Insert header snippet: theader

  4. Insert events snippet: tevts

  5. Insert service snippet: tsrv

  6. Code generation (terminal):

mvn compile
  1. Refresh project (IDE)
  2. Trigger Maven sync (IDE)
  3. Edit src/main/java/com/example/digitaltwin/DigitalTwinToTopicAction class
  4. Delete class body
  5. Insert code snippet (delete everything under constructor): tall

Copy-paste list

mvn archetype:generate \
-DarchetypeGroupId=io.kalix \
-DarchetypeArtifactId=kalix-maven-archetype \
-DarchetypeVersion=LATEST
com.example
digitaltwin
com.example.digitaltwin
digitaltwin_api.proto
digitaltwin_domain.proto
mvn compile
mvn test
mvn -Pit verify
mvn deploy
curl -XPOST -d '{
  "name": "DT1",
  "metric_value_alert_threshold": "10"
}' https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/4/create -H "Content-Type: application/json"
curl -XPOST -d '{
  "metric_value": "5"
}' https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/4/add-metric -H "Content-Type: application/json"
curl -XGET https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/4 -H "Content-Type: application/json"
curl -XPOST -d '{
  "metric_value": "11"
}' https://winter-bonus-0316.eu-central-1.kalix.app/digitaltwin/4/add-metric -H "Content-Type: application/json"
digitaltwin_topic.proto

kalix-demo-digitaltwin-java's People

Contributors

aklikic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.