-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register workflows and acitivities using instances along classes (#1201)
- Loading branch information
1 parent
cf7405f
commit 58d6218
Showing
11 changed files
with
306 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowActivityInstanceWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2023 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.workflows.runtime; | ||
|
||
import com.microsoft.durabletask.TaskActivity; | ||
import com.microsoft.durabletask.TaskActivityFactory; | ||
import io.dapr.workflows.WorkflowActivity; | ||
|
||
/** | ||
* Wrapper for Durable Task Framework task activity factory. | ||
*/ | ||
public class WorkflowActivityInstanceWrapper<T extends WorkflowActivity> implements TaskActivityFactory { | ||
private final T activity; | ||
private final String name; | ||
|
||
/** | ||
* Constructor for WorkflowActivityWrapper. | ||
* | ||
* @param instance Instance of the activity to wrap. | ||
*/ | ||
public WorkflowActivityInstanceWrapper(T instance) { | ||
this.name = instance.getClass().getCanonicalName(); | ||
this.activity = instance; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public TaskActivity create() { | ||
return ctx -> activity.run(new DefaultWorkflowActivityContext(ctx)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowInstanceWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.workflows.runtime; | ||
|
||
import com.microsoft.durabletask.TaskOrchestration; | ||
import com.microsoft.durabletask.TaskOrchestrationFactory; | ||
import io.dapr.workflows.Workflow; | ||
import io.dapr.workflows.saga.Saga; | ||
|
||
/** | ||
* Wrapper for Durable Task Framework orchestration factory. | ||
*/ | ||
class WorkflowInstanceWrapper<T extends Workflow> implements TaskOrchestrationFactory { | ||
private final T workflow; | ||
private final String name; | ||
|
||
public WorkflowInstanceWrapper(T instance) { | ||
this.name = instance.getClass().getCanonicalName(); | ||
this.workflow = instance; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public TaskOrchestration create() { | ||
return ctx -> { | ||
if (workflow.getSagaOption() != null) { | ||
Saga saga = new Saga(workflow.getSagaOption()); | ||
workflow.run(new DefaultWorkflowContext(ctx, saga)); | ||
} else { | ||
workflow.run(new DefaultWorkflowContext(ctx)); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...orkflows/src/test/java/io/dapr/workflows/runtime/WorkflowActivityInstanceWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.dapr.workflows.runtime; | ||
|
||
import com.microsoft.durabletask.TaskActivityContext; | ||
import io.dapr.workflows.WorkflowActivity; | ||
import io.dapr.workflows.WorkflowActivityContext; | ||
import org.junit.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class WorkflowActivityInstanceWrapperTest { | ||
public static class TestActivity implements WorkflowActivity { | ||
@Override | ||
public Object run(WorkflowActivityContext ctx) { | ||
String activityContextName = ctx.getName(); | ||
return ctx.getInput(String.class) + " world! from " + activityContextName; | ||
} | ||
} | ||
|
||
@Test | ||
public void getName() { | ||
WorkflowActivityInstanceWrapper<TestActivity> wrapper = new WorkflowActivityInstanceWrapper<>(new TestActivity()); | ||
|
||
assertEquals( | ||
"io.dapr.workflows.runtime.WorkflowActivityInstanceWrapperTest.TestActivity", | ||
wrapper.getName() | ||
); | ||
} | ||
|
||
@Test | ||
public void createWithInstance() { | ||
TaskActivityContext mockContext = mock(TaskActivityContext.class); | ||
WorkflowActivityInstanceWrapper<TestActivity> wrapper = new WorkflowActivityInstanceWrapper<>(new TestActivity()); | ||
|
||
when(mockContext.getInput(String.class)).thenReturn("Hello"); | ||
when(mockContext.getName()).thenReturn("TestActivityContext"); | ||
|
||
Object result = wrapper.create().run(mockContext); | ||
|
||
verify(mockContext, times(1)).getInput(String.class); | ||
assertEquals("Hello world! from TestActivityContext", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.