Skip to content

Commit

Permalink
initial @EnableDaprWorkflows implementation
Browse files Browse the repository at this point in the history
Signed-off-by: salaboy <[email protected]>
  • Loading branch information
salaboy committed Dec 6, 2024
1 parent a645f54 commit 75015dc
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.dapr.client.DaprClientBuilder;
import io.dapr.config.Properties;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -65,14 +66,26 @@ DaprClient daprClient(DaprClientBuilder daprClientBuilder) {
@Bean
@ConditionalOnMissingBean
DaprWorkflowClient daprWorkflowClient(DaprConnectionDetails daprConnectionDetails) {
final Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
return new DaprWorkflowClient(properties);
}

@Bean
@ConditionalOnMissingBean
WorkflowRuntimeBuilder daprWorkflowRuntimeBuilder(DaprConnectionDetails daprConnectionDetails) {
final Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
return new WorkflowRuntimeBuilder(properties);
}

private Properties createPropertiesFromConnectionDetails(DaprConnectionDetails daprConnectionDetails) {
final Map<String, String> propertyOverrides = new HashMap<>();
propertyOverrides.put(Properties.HTTP_ENDPOINT.getName(), daprConnectionDetails.httpEndpoint());
propertyOverrides.put(Properties.HTTP_PORT.getName(), String.valueOf(daprConnectionDetails.httpPort()));
propertyOverrides.put(Properties.GRPC_ENDPOINT.getName(), daprConnectionDetails.grpcEndpoint());
propertyOverrides.put(Properties.GRPC_PORT.getName(), String.valueOf(daprConnectionDetails.grpcPort()));
final Properties properties = new Properties(propertyOverrides);
return new DaprWorkflowClient(properties);
return new Properties(propertyOverrides);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.dapr.spring.boot.autoconfigure.client;

import io.dapr.spring.boot.autoconfigure.client.workflows.TestActivity;
import io.dapr.spring.boot.autoconfigure.client.workflows.TestWorkflow;
import io.dapr.workflows.client.DaprWorkflowClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(classes = {WorkflowTestApplication.class, DaprClientAutoConfiguration.class, TestActivity.class, TestWorkflow.class})
public class DaprWorkflowsRegistrationTests {

@Autowired
private DaprWorkflowClient daprWorkflowClient;

@Test
public void doThat(){
assertNotNull(daprWorkflowClient);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.dapr.spring.boot.autoconfigure.client;

import io.dapr.spring.workflows.config.EnableDaprWorkflows;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDaprWorkflows
public class WorkflowTestApplication {
public static void main(String[] args) {
SpringApplication.run(WorkflowTestApplication.class, args);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.dapr.spring.boot.autoconfigure.client.workflows;

import io.dapr.workflows.runtime.WorkflowActivity;
import io.dapr.workflows.runtime.WorkflowActivityContext;

public class TestActivity implements WorkflowActivity {
@Override
public Object run(WorkflowActivityContext ctx) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.dapr.spring.boot.autoconfigure.client.workflows;

import io.dapr.workflows.Workflow;
import io.dapr.workflows.WorkflowStub;

public class TestWorkflow extends Workflow {

@Override
public WorkflowStub create() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>dapr-spring-messaging</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-workflows</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.dapr.spring.workflows.config;

import io.dapr.workflows.Workflow;
import io.dapr.workflows.runtime.WorkflowActivity;
import io.dapr.workflows.runtime.WorkflowRuntime;
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;

import java.util.Map;

@Configuration
@ComponentScan("io.dapr.spring.workflows.config")
public class DaprWorkflowsConfig implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(DaprWorkflowsConfig.class);
@Autowired
private WorkflowRuntimeBuilder workflowRuntimeBuilder;

private static ApplicationContext context;

/**
* Register workflows and activities to the workflowRuntimeBuilder.
*/
public void registerWorkflowsAndActivities() {
LOGGER.info("Registering Dapr Workflows and Activities");
Map<String, Workflow> workflowBeans = context.getBeansOfType(Workflow.class);
for (Workflow w : workflowBeans.values()) {
LOGGER.info("Dapr Workflow: '{}' registered", w.getClass().getName());
workflowRuntimeBuilder.registerWorkflow(w.getClass());
}

Map<String, WorkflowActivity> workflowActivitiesBeans = context.getBeansOfType(WorkflowActivity.class);
for (WorkflowActivity a : workflowActivitiesBeans.values()) {
LOGGER.info("Dapr Workflow Activity: '{}' registered", a.getClass().getName());
workflowRuntimeBuilder.registerActivity(a.getClass());
}

try (WorkflowRuntime runtime = workflowRuntimeBuilder.build()) {
LOGGER.info("Starting workflow runtime ... ");
runtime.start(false);
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
registerWorkflowsAndActivities();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.dapr.spring.workflows.config;


import org.springframework.context.annotation.Import;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Target(TYPE)
@Import(DaprWorkflowsConfig.class)
public @interface EnableDaprWorkflows {
}

0 comments on commit 75015dc

Please sign in to comment.