Skip to content
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

feat UrlResolver #4546

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,45 @@ public interface ExternalTaskClientBuilder {
/**
* Base url of the Camunda BPM Platform REST API. This information is mandatory.
*
* If this method is used, it will create a permanent url resolver with the given baseUrl.
*
* @param baseUrl of the Camunda BPM Platform REST API
* @return the builder
*/
ExternalTaskClientBuilder baseUrl(String baseUrl);


/**
* Url resolver of the Camunda BPM Platform REST API. This information is mandatory.
*
* If the server is in a cluster or you are using spring cloud, you can create a class which implements UrlResolver..
*
* this is a sample for spring cloud DiscoveryClient
*
* public class CustomUrlResolver implements UrlResolver{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice to have an example included here

*
* protected String serviceId;
*
* protected DiscoveryClient discoveryClient;
*
* protected String getRandomServiceInstance() {
* List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);
* Random random = new Random();
* return serviceInstances.get(random.nextInt(serviceInstances.size())).getUri().toString();
* }
*
* @Override
* public String getBaseUrl() {
* return getRandomServiceInstance();
* }
* }
*
*
* @param urlResolver of the Camunda BPM Platform REST API
* @return the builder
*/
ExternalTaskClientBuilder urlResolver(UrlResolver urlResolver);

/**
* A custom worker id the Workflow Engine is aware of. This information is optional.
* Note: make sure to choose a unique worker id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; 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 org.camunda.bpm.client;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing the licenseHeader, can you please add it to the top of this file. You can copy it from one of the other files


/**
* Get service url of the Camunda server
*/
public interface UrlResolver {

String getBaseUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;

import org.camunda.bpm.client.UrlResolver;
import org.camunda.bpm.client.task.OrderingConfig;
import org.camunda.bpm.client.task.ExternalTask;
import org.camunda.bpm.client.task.impl.ExternalTaskImpl;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class EngineClient {
public static final String PROCESS_INSTANCE_ID_RESOURCE_PATH = PROCESS_INSTANCE_RESOURCE_PATH + "/" + ID_PATH_PARAM;
public static final String GET_BINARY_VARIABLE =
PROCESS_INSTANCE_ID_RESOURCE_PATH + "/variables/" + NAME_PATH_PARAM + "/data";
protected String baseUrl;
protected UrlResolver urlResolver;
protected String workerId;
protected int maxTasks;
protected boolean usePriority;
Expand All @@ -65,40 +66,57 @@ public class EngineClient {
protected RequestExecutor engineInteraction;
protected TypedValues typedValues;

public EngineClient(String workerId, int maxTasks, Long asyncResponseTimeout, String baseUrl, RequestExecutor engineInteraction) {
this(workerId, maxTasks, asyncResponseTimeout, baseUrl, engineInteraction, true, OrderingConfig.empty());
}

public EngineClient(String workerId, int maxTasks, Long asyncResponseTimeout, String baseUrl, RequestExecutor engineInteraction,
boolean usePriority, OrderingConfig orderingConfig) {
this.workerId = workerId;
this.asyncResponseTimeout = asyncResponseTimeout;
this.maxTasks = maxTasks;
this.usePriority = usePriority;
this.engineInteraction = engineInteraction;
this.baseUrl = baseUrl;
this.orderingConfig = orderingConfig;
}
public EngineClient(String workerId, int maxTasks, Long asyncResponseTimeout, String baseUrl, RequestExecutor engineInteraction) {
this(workerId, maxTasks, asyncResponseTimeout, baseUrl, engineInteraction, true, OrderingConfig.empty());
}

public EngineClient(String workerId, int maxTasks, Long asyncResponseTimeout, String baseUrl, RequestExecutor engineInteraction,
boolean usePriority, OrderingConfig orderingConfig) {
this.workerId = workerId;
this.asyncResponseTimeout = asyncResponseTimeout;
this.maxTasks = maxTasks;
this.usePriority = usePriority;
this.engineInteraction = engineInteraction;
this.urlResolver = new PermanentUrlResolver(baseUrl);
this.orderingConfig = orderingConfig;
}


public EngineClient(String workerId, int maxTasks, Long asyncResponseTimeout, UrlResolver urlResolver, RequestExecutor engineInteraction) {
this(workerId, maxTasks, asyncResponseTimeout, urlResolver, engineInteraction, true, OrderingConfig.empty());
}


public EngineClient(String workerId, int maxTasks, Long asyncResponseTimeout, UrlResolver urlResolver, RequestExecutor engineInteraction,
boolean usePriority, OrderingConfig orderingConfig) {
this.workerId = workerId;
this.asyncResponseTimeout = asyncResponseTimeout;
this.maxTasks = maxTasks;
this.usePriority = usePriority;
this.engineInteraction = engineInteraction;
this.urlResolver = urlResolver;
this.orderingConfig = orderingConfig;
}

public List<ExternalTask> fetchAndLock(List<TopicRequestDto> topics) {
FetchAndLockRequestDto payload = new FetchAndLockRequestDto(workerId, maxTasks, asyncResponseTimeout,
topics, usePriority, orderingConfig);

String resourceUrl = baseUrl + FETCH_AND_LOCK_RESOURCE_PATH;
String resourceUrl = getBaseUrl() + FETCH_AND_LOCK_RESOURCE_PATH;
ExternalTask[] externalTasks = engineInteraction.postRequest(resourceUrl, payload, ExternalTaskImpl[].class);
return Arrays.asList(externalTasks);
}

public void lock(String taskId, long lockDuration) {
LockRequestDto payload = new LockRequestDto(workerId, lockDuration);
String resourcePath = LOCK_RESOURCE_PATH.replace("{id}", taskId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

public void unlock(String taskId) {
String resourcePath = UNLOCK_RESOURCE_PATH.replace("{id}", taskId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, null, Void.class);
}

Expand All @@ -108,15 +126,15 @@ public void complete(String taskId, Map<String, Object> variables, Map<String, O

CompleteRequestDto payload = new CompleteRequestDto(workerId, typedValueDtoMap, localTypedValueDtoMap);
String resourcePath = COMPLETE_RESOURCE_PATH.replace("{id}", taskId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

public void setVariables(String processId, Map<String, Object> variables) {
Map<String, TypedValueField> typedValueDtoMap = typedValues.serializeVariables(variables);
SetVariablesRequestDto payload = new SetVariablesRequestDto(workerId, typedValueDtoMap);
String resourcePath = SET_VARIABLES_RESOURCE_PATH.replace("{id}", processId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

Expand All @@ -127,35 +145,35 @@ public void failure(String taskId, String errorMessage, String errorDetails, int

FailureRequestDto payload = new FailureRequestDto(workerId, errorMessage, errorDetails, retries, retryTimeout, typedValueDtoMap, localTypedValueDtoMap);
String resourcePath = FAILURE_RESOURCE_PATH.replace("{id}", taskId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

public void bpmnError(String taskId, String errorCode, String errorMessage, Map<String, Object> variables) {
Map<String, TypedValueField> typeValueDtoMap = typedValues.serializeVariables(variables);
BpmnErrorRequestDto payload = new BpmnErrorRequestDto(workerId, errorCode, errorMessage, typeValueDtoMap);
String resourcePath = BPMN_ERROR_RESOURCE_PATH.replace("{id}", taskId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

public void extendLock(String taskId, long newDuration) {
ExtendLockRequestDto payload = new ExtendLockRequestDto(workerId, newDuration);
String resourcePath = EXTEND_LOCK_RESOURCE_PATH.replace("{id}", taskId);
String resourceUrl = baseUrl + resourcePath;
String resourceUrl = getBaseUrl() + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

public byte[] getLocalBinaryVariable(String variableName, String executionId) {
String resourcePath = baseUrl + GET_BINARY_VARIABLE
String resourcePath = getBaseUrl() + GET_BINARY_VARIABLE
.replace(ID_PATH_PARAM, executionId)
.replace(NAME_PATH_PARAM, variableName);

return engineInteraction.getRequest(resourcePath);
}

public String getBaseUrl() {
return baseUrl;
return this.urlResolver.getBaseUrl();
}

public String getWorkerId() {
Expand Down
Loading