11package io .eigr .spawn .internal .transport .client ;
22
33import io .eigr .functions .protocol .Protocol ;
4+ import io .eigr .spawn .api .TransportOpts ;
45import io .eigr .spawn .api .exceptions .ActorCreationException ;
56import io .eigr .spawn .api .exceptions .ActorInvocationException ;
67import io .eigr .spawn .api .exceptions .ActorRegistrationException ;
1011
1112import java .io .IOException ;
1213import java .util .Objects ;
14+ import java .util .concurrent .Executor ;
1315import java .util .concurrent .TimeUnit ;
1416
1517public 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