How to bind a Camunda ExternalTaskClient to a local port?

Viewed 115

I am working with Camunda's ExternalTaskClient Java client. I am able to subscribe to a topic successfully, but this uses a random port on the host machine to communicate with Camunda.

How can I tell the ExternalTaskClient to use a custom outgoing port? Here is my client so far:

ExternalTaskClient client = ExternalTaskClient.create().baseUrl("http://1.2.3.4:8080/engine-rest").build();

client.subscribe("my-topic").handler(new MyHandler()).open();

Thanks!

2 Answers

If it really is necessary to specify a specific source (client) port then these are the steps:

  1. Implement a custom socket factory that implements ConnectionSocketFactory and returns a socket with your preferred local port - Camunda uses Apache HttpClient internally
  2. Register that factory with a scheme through the connection manager and schema registry - httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme(<scheme>, <outgoing port>, <your factory>));
  3. Hope that Camunda uses the HttpClient library as intended and no unpleasant side-effects pop up

In addition to the above you also need to think about handling an SSL context (in the custom factory) if the connection needs to be HTTPS. Also remember that using any port below 1024 means the user running the client needs special permissions.

Why is it a random port? Under the hood the ExternalTaskClient uses the REST-API. The baseURL you are providing in its configuration is the URL of the engine REST-API. It runs on one specific port. In your case on the default port 8080. You can change the server.port if you want the API to run on a different port.

Related