Skip to content

Commit ede71b8

Browse files
committed
New release. Delegate spawn actor to proxy
1 parent 02626b9 commit ede71b8

File tree

8 files changed

+79
-15
lines changed

8 files changed

+79
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
target/
22
.idea/
3+
bin/
34
pom.xml.tag
45
pom.xml.releaseBackup
56
pom.xml.versionsBackup
@@ -10,4 +11,4 @@ buildNumber.properties
1011
.mvn/timing.properties
1112
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
1213
.mvn/wrapper/maven-wrapper.jar
13-
spawn-java-std-sdk.iml
14+
spawn-java-std-sdk.iml

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.0.1</version>
97+
<version>v1.2.0</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.0.1</version>
131+
<version>v1.2.0</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.0.1</version>
7+
<version>1.2.0</version>
88
<name>spawn-java-std-sdk</name>
99
<url>http://maven.apache.org</url>
1010

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected static ActorRef of(SpawnClient client, Cache<ActorOuterClass.ActorId,
4545
if (identity.isParent()) {
4646
actorId = buildActorId(identity.getSystem(), identity.getName(), identity.getParent());
4747

48-
spawnActor(actorId, client);
48+
//spawnActor(actorId, client);
4949
} else {
5050
actorId = buildActorId(identity.getSystem(), identity.getName());
5151
}
@@ -207,9 +207,9 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
207207
* @param value the action argument object.
208208
* @since 0.0.1
209209
*/
210-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws ActorInvocationException {
210+
public <T extends GeneratedMessageV3> void invokeAsync(String action, T value) throws ActorInvocationException {
211211
InvocationOpts opts = InvocationOpts.builder().async(true).build();
212-
invokeActor(action, value, null, Optional.of(opts));
212+
invokeActorAsync(action, value, Optional.of(opts));
213213
}
214214

215215
/**
@@ -223,15 +223,15 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
223223
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
224224
* @since 0.0.1
225225
*/
226-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws ActorInvocationException {
226+
public <T extends GeneratedMessageV3> void invokeAsync(String action, T value, InvocationOpts opts) throws ActorInvocationException {
227227
InvocationOpts mergedOpts = InvocationOpts.builder()
228228
.async(true)
229229
.delaySeconds(opts.getDelaySeconds())
230230
.scheduledTo(opts.getScheduledTo())
231231
.timeoutSeconds(opts.getTimeoutSeconds())
232232
.build();
233233

234-
invokeActor(action, value, null, Optional.of(mergedOpts));
234+
invokeActorAsync(action, value, Optional.of(mergedOpts));
235235
}
236236

237237
public String getActorSystem() {
@@ -264,6 +264,10 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
264264

265265
Protocol.InvocationRequest.Builder invocationRequestBuilder = Protocol.InvocationRequest.newBuilder();
266266

267+
if (Objects.nonNull(this.actorId.getParent()) && !this.actorId.getParent().isEmpty()) {
268+
invocationRequestBuilder.setRegisterRef(this.actorId.getParent());
269+
}
270+
267271
Map<String, String> metadata = new HashMap<>();
268272
options.ifPresent(opts -> {
269273
invocationRequestBuilder.setAsync(opts.isAsync());
@@ -285,6 +289,7 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
285289
.setActor(actorRef)
286290
.setActionName(cmd)
287291
.setValue(commandArg)
292+
288293
.putAllMetadata(metadata)
289294
.build();
290295

@@ -312,4 +317,36 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
312317

313318
return Optional.empty();
314319
}
320+
321+
private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeActorAsync(
322+
String cmd, S argument, Optional<InvocationOpts> options) {
323+
Objects.requireNonNull(this.actorId, "ActorId cannot be null");
324+
325+
Protocol.InvocationRequest.Builder invocationRequestBuilder = Protocol.InvocationRequest.newBuilder();
326+
327+
Map<String, String> metadata = new HashMap<>();
328+
options.ifPresent(opts -> {
329+
invocationRequestBuilder.setAsync(true);
330+
opts.getDelaySeconds().ifPresent(invocationRequestBuilder::setScheduledTo);
331+
// 'scheduledTo' override 'delay' if both is set.
332+
opts.getScheduledTo()
333+
.ifPresent(scheduleTo -> invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong()));
334+
});
335+
336+
final ActorOuterClass.Actor actorRef = ActorOuterClass.Actor.newBuilder()
337+
.setId(this.actorId)
338+
.build();
339+
340+
Any commandArg = Any.pack(argument);
341+
342+
invocationRequestBuilder
343+
.setSystem(ActorOuterClass.ActorSystem.newBuilder().setName(this.actorId.getSystem()).build())
344+
.setActor(actorRef)
345+
.setActionName(cmd)
346+
.setValue(commandArg)
347+
.putAllMetadata(metadata)
348+
.build();
349+
350+
this.client.invokeAsync(invocationRequestBuilder.build());
351+
}
315352
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ public SpawnSystem createFromEnv() {
311311
*/
312312
public SpawnSystem withActor(Class<?> actorKlass) {
313313
Optional<Entity> maybeEntity = getEntity(actorKlass);
314-
if (maybeEntity.isPresent()) {
315-
this.entities.add(maybeEntity.get());
316-
}
314+
maybeEntity.ifPresent(this.entities::add);
317315
return this;
318316
}
319317

@@ -330,9 +328,7 @@ public SpawnSystem withActor(Class<?> actorKlass) {
330328
*/
331329
public SpawnSystem withActor(Class<?> actorKlass, Object arg, ActorFactory factory) {
332330
Optional<Entity> maybeEntity = getEntity(actorKlass, arg, factory);
333-
if (maybeEntity.isPresent()) {
334-
this.entities.add(maybeEntity.get());
335-
}
331+
maybeEntity.ifPresent(this.entities::add);
336332
return this;
337333
}
338334

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11+
import java.io.IOException;
1112
import java.util.Objects;
1213
import java.util.concurrent.TimeUnit;
1314

@@ -94,6 +95,31 @@ public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) th
9495
}
9596
}
9697

98+
@Override
99+
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+
}
120+
});
121+
}
122+
97123
private String makeURLForSystemAndActor(String systemName, String actorName) {
98124
String uri = String.format("/api/v1/system/%s/actors/%s/invoke", systemName, actorName);
99125
return makeURLFrom(uri);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ public interface SpawnClient {
1010
Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws ActorRegistrationException;
1111
Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws ActorCreationException;
1212
Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws ActorInvocationException;
13+
14+
void invokeAsync(Protocol.InvocationRequest request);
1315
}
1416

src/main/proto/eigr/functions/protocol/actors/protocol.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ message InvocationRequest {
356356
int64 scheduled_to = 9;
357357

358358
bool pooled = 10;
359+
360+
string register_ref = 11;
359361
}
360362

361363
// ActorInvocation is a translation message between a local invocation made via InvocationRequest

0 commit comments

Comments
 (0)