Skip to content

Adding remote endpoint request from inside activity with retry #1388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<commons-cli.version>1.9.0</commons-cli.version>
<commons-io.version>2.14.0</commons-io.version>
<zipkin.version>3.4.0</zipkin.version>
<microcks.version>0.3.1</microcks.version>
</properties>

<distributionManagement>
Expand Down
6 changes: 6 additions & 0 deletions spring-boot-examples/workflows/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.microcks</groupId>
<artifactId>microcks-testcontainers</artifactId>
<version>${microcks.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@

package io.dapr.springboot.examples.wfp;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.springboot.examples.wfp.continueasnew.CleanUpLog;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class WorkflowPatternsConfiguration {
@Bean
public CleanUpLog cleanUpLog(){
return new CleanUpLog();
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplateBuilder().build();
}

@Bean
public ObjectMapper mapper() {
return new ObjectMapper();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.dapr.springboot.examples.wfp.externalevent.ExternalEventWorkflow;
import io.dapr.springboot.examples.wfp.fanoutin.FanOutInWorkflow;
import io.dapr.springboot.examples.wfp.fanoutin.Result;
import io.dapr.springboot.examples.wfp.remoteendpoint.Payload;
import io.dapr.springboot.examples.wfp.remoteendpoint.RemoteEndpointWorkflow;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.client.WorkflowInstanceStatus;
import org.slf4j.Logger;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class WorkflowPatternsRestController {
private Map<String, String> ordersToApprove = new HashMap<>();



/**
* Run Chain Demo Workflow
* @return the output of the ChainWorkflow execution
Expand Down Expand Up @@ -137,4 +140,17 @@ public CleanUpLog continueAsNew()
return workflowInstanceStatus.readOutputAs(CleanUpLog.class);
}

@PostMapping("wfp/remote-endpoint")
public Payload remoteEndpoint(@RequestBody Payload payload)
throws TimeoutException {

String instanceId = daprWorkflowClient.scheduleNewWorkflow(RemoteEndpointWorkflow.class, payload);
logger.info("Workflow instance " + instanceId + " started");

WorkflowInstanceStatus workflowInstanceStatus = daprWorkflowClient
.waitForInstanceCompletion(instanceId, null, true);
System.out.printf("workflow instance with ID: %s completed.", instanceId);
return workflowInstanceStatus.readOutputAs(Payload.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.springboot.examples.wfp.remoteendpoint;

import io.dapr.workflows.WorkflowActivity;
import io.dapr.workflows.WorkflowActivityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class CallRemoteEndpointActivity implements WorkflowActivity {

private Logger logger = LoggerFactory.getLogger(CallRemoteEndpointActivity.class);

@Value("${application.process-base-url:}")
private String processBaseURL;

@Autowired
private RestTemplate restTemplate;


@Override
public Object run(WorkflowActivityContext ctx) {
logger.info("Starting Activity: " + ctx.getName());
var payload = ctx.getInput(Payload.class);

HttpEntity<Payload> request =
new HttpEntity<>(payload);
payload = restTemplate.postForObject(processBaseURL + "/process", request, Payload.class);

logger.info("Payload from the remote service: " + payload);

return payload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2025 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.springboot.examples.wfp.remoteendpoint;

public class Payload {
private String id;
private String content;
private Boolean processed = false;

public Payload(String id, String content) {
this.id = id;
this.content = content;
}

public Payload() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}


public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Boolean getProcessed() {
return processed;
}

public void setProcessed(Boolean processed) {
this.processed = processed;
}

@Override
public String toString() {
return "Payload{" +
"id='" + id + '\'' +
", content='" + content + '\'' +
", processed=" + processed +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.springboot.examples.wfp.remoteendpoint;

import io.dapr.workflows.Workflow;
import io.dapr.workflows.WorkflowStub;
import io.dapr.workflows.WorkflowTaskOptions;
import io.dapr.workflows.WorkflowTaskRetryPolicy;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class RemoteEndpointWorkflow implements Workflow {

@Override
public WorkflowStub create() {
return ctx -> {
ctx.getLogger().info("Starting Workflow: " + ctx.getName());

Payload payload = ctx.getInput(Payload.class);
payload = ctx.callActivity(CallRemoteEndpointActivity.class.getName(), payload ,
new WorkflowTaskOptions(new WorkflowTaskRetryPolicy(5,
Duration.ofSeconds(2), 1.0, Duration.ofSeconds(10), Duration.ofSeconds(20))),
Payload.class).await();

ctx.getLogger().info("Workflow finished with result: " + payload);
ctx.complete(payload);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@

import io.dapr.testcontainers.Component;
import io.dapr.testcontainers.DaprContainer;
import io.github.microcks.testcontainers.MicrocksContainersEnsemble;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.test.context.DynamicPropertyRegistrar;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.Network;

import java.util.Collections;
import java.util.List;

import static io.dapr.testcontainers.DaprContainerConstants.DAPR_RUNTIME_IMAGE_TAG;

Expand All @@ -28,16 +36,66 @@ public class DaprTestContainersConfig {

@Bean
@ServiceConnection
public DaprContainer daprContainer() {
public DaprContainer daprContainer(Network network) {

return new DaprContainer(DAPR_RUNTIME_IMAGE_TAG)
.withAppName("workflow-patterns-app")
.withComponent(new Component("kvstore", "state.in-memory", "v1", Collections.singletonMap("actorStateStore", String.valueOf(true))))
.withAppPort(8080)
.withNetwork(network)
.withAppHealthCheckPath("/actuator/health")
.withAppChannelAddress("host.testcontainers.internal");
}


@Bean
MicrocksContainersEnsemble microcksEnsemble(Network network) {
return new MicrocksContainersEnsemble(network, "quay.io/microcks/microcks-uber:1.11.2")
.withAccessToHost(true) // We need this to access our webapp while it runs
.withMainArtifacts("third-parties/remote-http-service.yaml");
}

@Bean
public DynamicPropertyRegistrar endpointsProperties(MicrocksContainersEnsemble ensemble) {
// We need to replace the default endpoints with those provided by Microcks.
return (properties) -> {
properties.add("application.process-base-url", () -> ensemble.getMicrocksContainer()
.getRestMockEndpoint("API Payload Processor", "1.0.0"));
};
}

@Bean
public Network getDaprNetwork(Environment env) {
boolean reuse = env.getProperty("reuse", Boolean.class, false);
if (reuse) {
Network defaultDaprNetwork = new Network() {
@Override
public String getId() {
return "dapr-network";
}

@Override
public void close() {

}

@Override
public Statement apply(Statement base, Description description) {
return null;
}
};

List<com.github.dockerjava.api.model.Network> networks = DockerClientFactory.instance().client().listNetworksCmd()
.withNameFilter("dapr-network").exec();
if (networks.isEmpty()) {
Network.builder().createNetworkCmdModifier(cmd -> cmd.withName("dapr-network")).build().getId();
return defaultDaprNetwork;
} else {
return defaultDaprNetwork;
}
} else {
return Network.newNetwork();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import io.dapr.client.DaprClient;
import io.dapr.springboot.DaprAutoConfiguration;
import io.dapr.springboot.examples.wfp.continueasnew.CleanUpLog;
import io.dapr.springboot.examples.wfp.remoteendpoint.Payload;
import io.github.microcks.testcontainers.MicrocksContainersEnsemble;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -39,6 +41,9 @@ class WorkflowPatternsAppTests {
@Autowired
private DaprClient daprClient;

@Autowired
private MicrocksContainersEnsemble ensemble;

@BeforeEach
void setUp() {
RestAssured.baseURI = "http://localhost:" + 8080;
Expand Down Expand Up @@ -139,4 +144,20 @@ void testContinueAsNew() {
assertEquals(5, cleanUpLog.getCleanUpTimes());
}

@Test
void testRemoteEndpoint() {

Payload payload = given().contentType(ContentType.JSON)
.body(new Payload("123", "content goes here"))
.when()
.post("/wfp/remote-endpoint")
.then()
.statusCode(200).extract().as(Payload.class);

assertEquals(true, payload.getProcessed());

assertEquals(2, ensemble.getMicrocksContainer()
.getServiceInvocationsCount("API Payload Processor", "1.0.0"));
}

}
Loading
Loading