About orkes playground

58 Views Asked by At

I could successfully created and started the workflow in orkes playground. Now, coming to Java (SpringBoot) code, I entered the conductor.server.url as https://play.orkes.io/api I created two more properties in application.properties with key and secret My Java code looks like this:

    TaskClient taskClient = new TaskClient();
    String url=env.getProperty("conductor.server.url");
    System.out.println("conductor.server.url is "+url);

But, I don't see that we are reading the key & secret from application.properties into this Java code. If I run like this, I get the error "com.netflix.conductor.client.exception.ConductorClientException: Token cannot be null or empty"

Could you please help me to fix this?

2

There are 2 best solutions below

3
Olivier Poupeney On

In your Spring Boot application, create 2 beans as follow:

@Bean
public ApiClient apiClient() {
    String key = env.getProperty("orkes.access.key");
    String secret = env.getProperty("orkes.access.secret");
    String conductorServer = env.getProperty("orkes.conductor.server.url");
    return new ApiClient(conductorServer, key, secret);
}
    
@Bean
public TaskClient getTaskClient(ApiClient apiClient) {
    return new OrkesTaskClient(apiClient);
}

Then you can create your worker class such as:

@Component
public class MyWorker implements Worker {
    @Override
    public String getTaskDefName() {
        return "my_worker_task";
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public TaskResult execute(Task task) {
        Map<String, Object> inputData = task.getInputData();
        ...
        return TaskResult.newTaskResult(TaskResult.Status.COMPLETED);
    }
}

Conductor SDK has also a spring support that wires things automatically (no need to create the beans). Creating a worker is as simple as follow:

/**
 * Note: Using this setting, up to 5 tasks will run in parallel, with tasks being polled every 200ms
 */

@WorkerTask(value = "fraud-check", threadCount = 5, pollingInterval = 200)
public FraudCheckResult checkForFraudTask(DepositDetail depositDetail) {
    return fraudCheckService.checkForFraud(depositDetail);
}

Full example here: https://github.com/conductor-sdk/orkes-java-springboot2-example/blob/main/src/main/java/io/orkes/example/banking/workers/ConductorWorkers.java#L27-L35

0
Olivier Poupeney On

Works for me. Here is the source of my Spring Boot app:

SampleApplication.java

package io.orkes.example;

import lombok.AllArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@AllArgsConstructor
@SpringBootApplication(scanBasePackages = {"io.orkes"})
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

controller/SampleApiController.java

package io.orkes.example.controller;

import io.orkes.example.service.WorkflowService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@AllArgsConstructor
@RestController
public class SampleApiController {

    private final WorkflowService workflowService;

    @PostMapping(value = "/sampleFlow", produces = "application/json")
    public ResponseEntity<Map<String, Object>> triggerSampleFlow() {
        log.info("Starting sample flow ");
        return ResponseEntity.ok(workflowService.startSampleWorkflow());
    }

}

service/WorkflowService.java

package io.orkes.example.service;

import com.netflix.conductor.common.metadata.workflow.StartWorkflowRequest;
import io.orkes.conductor.client.WorkflowClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@AllArgsConstructor
@Service
public class WorkflowService {

    private final WorkflowClient workflowClient;

    public Map<String, Object> startSampleWorkflow() {

        StartWorkflowRequest request = new StartWorkflowRequest();
        request.setName("guru_sequential_http");

        String workflowId = workflowClient.startWorkflow(request);
        log.info("Workflow id: {}", workflowId);

        return Map.of("workflowId", workflowId);
    }

}

resources/application.properties

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=false
server.port=8082

conductor.security.client.key-id=XXX
conductor.security.client.secret=YYY
conductor.server.url=https://play.orkes.io/api

Using Postman, I can trigger the workflow by sending a Post request to localhost:8082/sampleFlow

Hope this helps!