Skip to content

Commit cc35f11

Browse files
committed
Use an Executor to build async invocation
1 parent e455a1c commit cc35f11

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ The second thing we have to do is add the spawn dependency to the project.
9494
<dependency>
9595
<groupId>com.github.eigr</groupId>
9696
<artifactId>spawn-java-std-sdk</artifactId>
97-
<version>v1.2.3</version>
97+
<version>v1.2.4</version>
9898
</dependency>
9999
```
100100
We're also going to configure a few things for our application build to work, including compiling the protobuf files.
@@ -128,7 +128,7 @@ See below a full example of the pom.xml file:
128128
<dependency>
129129
<groupId>com.github.eigr</groupId>
130130
<artifactId>spawn-java-std-sdk</artifactId>
131-
<version>v1.2.3</version>
131+
<version>v1.2.4</version>
132132
</dependency>
133133
<dependency>
134134
<groupId>ch.qos.logback</groupId>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>io.eigr.spawn</groupId>
55
<artifactId>spawn-java-std-sdk</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.2.3</version>
7+
<version>1.2.4</version>
88
<name>spawn-java-std-sdk</name>
99
<url>http://maven.apache.org</url>
1010

src/main/java/io/eigr/spawn/api/Spawn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public Spawn build() {
354354
.expireAfterWrite(Duration.ofSeconds(CACHE_EXPIRE_AFTER_WRITE_SECONDS))
355355
.build();
356356

357-
this.client = new OkHttpSpawnClient(this.system, this.transportOpts.getProxyHost(), this.transportOpts.getProxyPort());
357+
this.client = new OkHttpSpawnClient(this.system, this.transportOpts);
358358

359359
return new Spawn(this);
360360
}

src/main/java/io/eigr/spawn/internal/transport/client/OkHttpSpawnClient.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.eigr.spawn.internal.transport.client;
22

33
import io.eigr.functions.protocol.Protocol;
4+
import io.eigr.spawn.api.TransportOpts;
45
import io.eigr.spawn.api.exceptions.ActorCreationException;
56
import io.eigr.spawn.api.exceptions.ActorInvocationException;
67
import io.eigr.spawn.api.exceptions.ActorRegistrationException;
@@ -10,6 +11,7 @@
1011

1112
import java.io.IOException;
1213
import java.util.Objects;
14+
import java.util.concurrent.Executor;
1315
import java.util.concurrent.TimeUnit;
1416

1517
public final class OkHttpSpawnClient implements SpawnClient {
@@ -20,15 +22,16 @@ public final class OkHttpSpawnClient implements SpawnClient {
2022

2123
private static final String SPAWN_ACTOR_SPAWN = "/api/v1/system/%s/actors/spawn";
2224

25+
private final Executor executor;
26+
2327
private final String system;
24-
private final String proxyHost;
25-
private final int proxyPort;
28+
29+
private final TransportOpts opts;
2630
private final OkHttpClient client;
2731

28-
public OkHttpSpawnClient(String system, String proxyHost, int proxyPort) {
32+
public OkHttpSpawnClient(String system, TransportOpts opts) {
2933
this.system = system;
30-
this.proxyHost = proxyHost;
31-
this.proxyPort = proxyPort;
34+
this.opts = opts;
3235
this.client = new OkHttpClient.Builder()
3336
.connectTimeout(120, TimeUnit.SECONDS)
3437
.readTimeout(120, TimeUnit.SECONDS)
@@ -37,6 +40,7 @@ public OkHttpSpawnClient(String system, String proxyHost, int proxyPort) {
3740
.retryOnConnectionFailure(true)
3841
.connectionPool(new ConnectionPool(256, 100, TimeUnit.SECONDS))
3942
.build();
43+
this.executor = opts.getExecutor();
4044
}
4145

4246
@Override
@@ -97,26 +101,28 @@ public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) th
97101

98102
@Override
99103
public void invokeAsync(Protocol.InvocationRequest request) {
100-
RequestBody body = RequestBody.create(
101-
request.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
102-
103-
Request invocationRequest = new Request.Builder()
104-
.url(makeURLForSystemAndActor(request.getSystem().getName(), request.getActor().getId().getName()))
105-
.post(body)
106-
.build();
107-
108-
Call invocationCall = client.newCall(invocationRequest);
109-
invocationCall.enqueue(new Callback() {
110-
@Override
111-
public void onFailure(final Call call, IOException err) {
112-
log.error("Error while actor invoke async.", err);
113-
}
114-
115-
@Override
116-
public void onResponse(Call call, final Response response) throws IOException {
117-
String res = response.body().string();
118-
log.trace("actor invoke async response [{}].", res);
119-
}
104+
executor.execute(() -> {
105+
RequestBody body = RequestBody.create(
106+
request.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
107+
108+
Request invocationRequest = new Request.Builder()
109+
.url(makeURLForSystemAndActor(request.getSystem().getName(), request.getActor().getId().getName()))
110+
.post(body)
111+
.build();
112+
113+
Call invocationCall = client.newCall(invocationRequest);
114+
invocationCall.enqueue(new Callback() {
115+
@Override
116+
public void onFailure(final Call call, IOException err) {
117+
log.warn("Error while Actor invoke async.", err);
118+
}
119+
120+
@Override
121+
public void onResponse(Call call, final Response response) throws IOException {
122+
String res = response.body().string();
123+
log.trace("Actor invoke async response [{}].", res);
124+
}
125+
});
120126
});
121127
}
122128

@@ -126,7 +132,7 @@ private String makeURLForSystemAndActor(String systemName, String actorName) {
126132
}
127133

128134
private String makeURLFrom(String uri) {
129-
return String.format("http://%s:%S%s", this.proxyHost, this.proxyPort, uri);
135+
return String.format("http://%s:%S%s", this.opts.getProxyHost(), this.opts.getPort(), uri);
130136
}
131137

132138
private String makeSpawnURLFrom(String systemName) {

0 commit comments

Comments
 (0)