Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java SDK for Serverless Workflow

License

NotificationsYou must be signed in to change notification settings

serverlessworkflow/sdk-java

Repository files navigation

Verify JAVA SDKDeploy JAVA SDK

Serverless Workflow Specification - Java SDK

Provides the Java API/SPI and Model Validation for theServerless Workflow Specification

With the SDK you can:

  • Parse workflow JSON and YAML definitions
  • Programmatically build workflow definitions
  • Validate workflow definitions (both schema and workflow integrity validation)
  • Generate workflow diagram (SVG)

Serverless Workflow Java SDK isnot a workflow runtime implementation but can be used by Java runtime implementationsto parse and validate workflow definitions as well as generate the workflow diagram (SVG).

Status

Latest ReleasesConformance to spec version
2.0.0.Finalv0.6
1.0.3.Finalv0.5

Getting Started

Building locally

To build project and run tests locally:

git clone https://github.com/serverlessworkflow/sdk-java.gitmvn clean install

To use it in your projects you can:

Maven projects:

a) Add the following repository to your pom.xmlrepositories section:

<repository>    <id>oss.sonatype.org-snapshot</id>    <url>http://oss.sonatype.org/content/repositories/snapshots</url>    <releases>        <enabled>false</enabled>    </releases>    <snapshots>        <enabled>true</enabled>    </snapshots></repository>

b) Add the following dependencies to your pom.xmldependencies section:

<dependency>    <groupId>io.serverlessworkflow</groupId>    <artifactId>serverlessworkflow-api</artifactId>    <version>2.0.0.Final</version></dependency><dependency>    <groupId>io.serverlessworkflow</groupId>    <artifactId>serverlessworkflow-spi</artifactId>    <version>2.0.0.Final</version></dependency><dependency>    <groupId>io.serverlessworkflow</groupId>    <artifactId>serverlessworkflow-validation</artifactId>    <version>2.0.0.Final</version></dependency><dependency>    <groupId>io.serverlessworkflow</groupId>    <artifactId>serverlessworkflow-diagram</artifactId>    <version>2.0.0.Final</version></dependency>

Gradle projects:

a) Add the following repositories to your build.gradlerepositories section:

maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

b) Add the following dependencies to your build.gradledependencies section:

implementation("io.serverlessworkflow:serverlessworkflow-api:2.0.0.Final")implementation("io.serverlessworkflow:serverlessworkflow-spi:2.0.0.Final")implementation("io.serverlessworkflow:serverlessworkflow-validation:2.0.0.Final")implementation("io.serverlessworkflow:serverlessworkflow-diagram:2.0.0.Final")

How to Use

Creating from JSON/YAML source

You can create a Workflow instance from JSON/YAML source:

Let's say you have a simple YAML based workflow definition:

id:greetingversion:'1.0'name:Greeting Workflowstart:Greetdescription:Greet Someonefunctions:  -name:greetingFunctionoperation:file://myapis/greetingapis.json#greetingstates:-name:Greettype:operationactions:  -functionRef:refName:greetingFunctionarguments:name:"${ .greet.name }"actionDataFilter:results:"${ .payload.greeting }"stateDataFilter:output:"${ .greeting }"end:true

To parse it and create a Workflow intance you can do:

Workflowworkflow =Workflow.fromSource(source);

where 'source' is the above mentioned YAML definition.

The fromSource static method can take in definitions in both JSON and YAML formats.

Once you have the Workflow instance you can use its API to inspect it, for example:

assertNotNull(workflow);assertEquals("greeting",workflow.getId());assertEquals("Greeting Workflow",workflow.getName());assertNotNull(workflow.getFunctions());assertEquals(1,workflow.getFunctions().size());assertEquals("greetingFunction",workflow.getFunctions().get(0).getName());assertNotNull(workflow.getStates());assertEquals(1,workflow.getStates().size());assertTrue(workflow.getStates().get(0)instanceofOperationState);OperationStateoperationState = (OperationState)workflow.getStates().get(0);assertEquals("Greet",operationState.getName());assertEquals(DefaultState.Type.OPERATION,operationState.getType());...

Using builder API

You can also programmatically create Workflow instances, for example:

Workflowworkflow =newWorkflow()                .withId("test-workflow")                .withName("test-workflow-name")                .withVersion("1.0")                .withStart(newStart().withStateName("MyDelayState"))                .withFunctions(newFunctions(Arrays.asList(newFunctionDefinition().withName("testFunction")                                .withOperation("testSwaggerDef#testOperationId")))                )                .withStates(Arrays.asList(newDelayState().withName("MyDelayState").withType(DELAY)                                .withTimeDelay("PT1M")                                .withEnd(newEnd().withTerminate(true)                                )                        )                );

This will create a test workflow that defines an event, a function and a single Delay State.

You can use the workflow instance to get its JSON/YAML definition as well:

assertNotNull(Workflow.toJson(testWorkflow));assertNotNull(Workflow.toYaml(testWorkflow));

Using Workflow Validation

Validation allows you to perform Json Schema validation against the JSON/YAML workflow definitions.Once you have aWorkflow instance, you can also run integrity checks.

You can validate a Workflow JSON/YAML definition to get validation errors:

WorkflowValidatorworkflowValidator =newWorkflowValidatorImpl();List<ValidationError>validationErrors =workflowValidator.setSource("WORKFLOW_MODEL_JSON/YAML").validate();

WhereWORKFLOW_MODEL_JSON/YAML is the actual workflow model JSON or YAML definition.

Or you can just check if it is valid (without getting specific errors):

WorkflowValidatorworkflowValidator =newWorkflowValidatorImpl();booleanisValidWorkflow =workflowValidator.setSource("WORKFLOW_MODEL_JSON/YAML").isValid();

If you build your Workflow programmatically, you can validate it as well:

Workflowworkflow =newWorkflow()                .withId("test-workflow")                .withVersion("1.0")                .withStart(newStart().withStateName("MyDelayState"))                .withStates(Arrays.asList(newDelayState().withName("MyDelayState").withType(DefaultState.Type.DELAY)                                .withTimeDelay("PT1M")                                .withEnd(newEnd().withTerminate(true)                                )                )););WorkflowValidatorworkflowValidator =newWorkflowValidatorImpl();List<ValidationError>validationErrors =workflowValidator.setWorkflow(workflow).validate();

Building Workflow Diagram

Given a valid workflow definition (JSON/YAML) or a Workflow object you can build the workflow diagram SVG.The generated diagram SVG usesPlantUML state diagram visualization and can be embedded inside yourtooling or web pages, or any SVG viewer.

You can build the workflow diagram SVG with the following code:

Workflowworkflow =Workflow.fromSource(source);WorkflowDiagramworkflowDiagram =newWorkflowDiagramImpl();workflowDiagram.setWorkflow(workflow);StringdiagramSVG =workflowDiagram.getSvgDiagram();

diagramSVG includes the diagram SVG source which you can then decide to save to a file,print, or process further.

Here are some generated diagrams from the specification examples:

  1. Job Monitoring Example

Job Monitoring Example Diagram

  1. Send CloudEvent on Workflow completion Example

Send Cloud Event on Workflow complation


[8]ページ先頭

©2009-2025 Movatter.jp