@@ -1375,6 +1375,7 @@ private final class ConnectionState implements InvocationBinder {
1375
1375
private Boolean handshakeReceived = false ;
1376
1376
private ScheduledExecutorService handshakeTimeout = null ;
1377
1377
private BehaviorSubject <InvocationMessage > messages = BehaviorSubject .create ();
1378
+ private ExecutorService resultInvocationPool = null ;
1378
1379
1379
1380
public final Lock lock = new ReentrantLock ();
1380
1381
public final CompletableSubject handshakeResponseSubject = CompletableSubject .create ();
@@ -1506,7 +1507,7 @@ public void handleHandshake(ByteBuffer payload) {
1506
1507
}
1507
1508
handshakeReceived = true ;
1508
1509
handshakeResponseSubject .onComplete ();
1509
- handleInvocations ();
1510
+ startInvocationProcessing ();
1510
1511
}
1511
1512
}
1512
1513
@@ -1528,66 +1529,81 @@ public void close() {
1528
1529
if (this .handshakeTimeout != null ) {
1529
1530
this .handshakeTimeout .shutdownNow ();
1530
1531
}
1532
+
1533
+ if (this .resultInvocationPool != null ) {
1534
+ this .resultInvocationPool .shutdownNow ();
1535
+ }
1531
1536
}
1532
1537
1533
1538
public void dispatchInvocation (InvocationMessage message ) {
1534
1539
messages .onNext (message );
1535
1540
}
1536
1541
1537
- private void handleInvocations () {
1538
- messages .observeOn (Schedulers .io ()).subscribe (invocationMessage -> {
1539
- List <InvocationHandler > handlers = this .connection .handlers .get (invocationMessage .getTarget ());
1540
- boolean expectsResult = invocationMessage .getInvocationId () != null ;
1541
- if (handlers == null ) {
1542
- if (expectsResult ) {
1543
- logger .warn ("Failed to find a value returning handler for '{}' method. Sending error to server." , invocationMessage .getTarget ());
1544
- sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1545
- null , "Client did not provide a result." ));
1546
- } else {
1547
- logger .warn ("Failed to find handler for '{}' method." , invocationMessage .getTarget ());
1548
- }
1549
- return ;
1550
- }
1551
- Object result = null ;
1552
- Exception resultException = null ;
1553
- Boolean hasResult = false ;
1554
- for (InvocationHandler handler : handlers ) {
1555
- try {
1556
- Object action = handler .getAction ();
1557
- if (handler .getHasResult ()) {
1558
- FunctionBase function = (FunctionBase )action ;
1559
- result = function .invoke (invocationMessage .getArguments ()).blockingGet ();
1560
- hasResult = true ;
1561
- } else {
1562
- ((ActionBase )action ).invoke (invocationMessage .getArguments ()).blockingAwait ();
1563
- }
1564
- } catch (Exception e ) {
1565
- logger .error ("Invoking client side method '{}' failed:" , invocationMessage .getTarget (), e );
1566
- if (handler .getHasResult ()) {
1567
- resultException = e ;
1568
- }
1569
- }
1542
+ private void startInvocationProcessing () {
1543
+ this .resultInvocationPool = Executors .newCachedThreadPool ();
1544
+ this .messages .observeOn (Schedulers .io ()).subscribe (invocationMessage -> {
1545
+ // if client result expected, unblock the invocation processing thread
1546
+ if (invocationMessage .getInvocationId () != null ) {
1547
+ this .resultInvocationPool .submit (() -> handleInvocation (invocationMessage ));
1548
+ } else {
1549
+ handleInvocation (invocationMessage );
1570
1550
}
1551
+ }, (e ) -> {
1552
+ stop (e .getMessage ());
1553
+ }, () -> {
1554
+ });
1555
+ }
1571
1556
1557
+ private void handleInvocation (InvocationMessage invocationMessage )
1558
+ {
1559
+ List <InvocationHandler > handlers = this .connection .handlers .get (invocationMessage .getTarget ());
1560
+ boolean expectsResult = invocationMessage .getInvocationId () != null ;
1561
+ if (handlers == null ) {
1572
1562
if (expectsResult ) {
1573
- if (resultException != null ) {
1574
- sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1575
- null , resultException .getMessage ()));
1576
- } else if (hasResult ) {
1577
- sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1578
- result , null ));
1563
+ logger .warn ("Failed to find a value returning handler for '{}' method. Sending error to server." , invocationMessage .getTarget ());
1564
+ sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1565
+ null , "Client did not provide a result." ));
1566
+ } else {
1567
+ logger .warn ("Failed to find handler for '{}' method." , invocationMessage .getTarget ());
1568
+ }
1569
+ return ;
1570
+ }
1571
+ Object result = null ;
1572
+ Exception resultException = null ;
1573
+ Boolean hasResult = false ;
1574
+ for (InvocationHandler handler : handlers ) {
1575
+ try {
1576
+ Object action = handler .getAction ();
1577
+ if (handler .getHasResult ()) {
1578
+ FunctionBase function = (FunctionBase )action ;
1579
+ result = function .invoke (invocationMessage .getArguments ()).blockingGet ();
1580
+ hasResult = true ;
1579
1581
} else {
1580
- logger .warn ("Failed to find a value returning handler for '{}' method. Sending error to server." , invocationMessage .getTarget ());
1581
- sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1582
- null , "Client did not provide a result." ));
1582
+ ((ActionBase )action ).invoke (invocationMessage .getArguments ()).blockingAwait ();
1583
1583
}
1584
+ } catch (Exception e ) {
1585
+ logger .error ("Invoking client side method '{}' failed:" , invocationMessage .getTarget (), e );
1586
+ if (handler .getHasResult ()) {
1587
+ resultException = e ;
1588
+ }
1589
+ }
1590
+ }
1591
+
1592
+ if (expectsResult ) {
1593
+ if (resultException != null ) {
1594
+ sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1595
+ null , resultException .getMessage ()));
1584
1596
} else if (hasResult ) {
1585
- logger .warn ("Result given for '{}' method but server is not expecting a result." , invocationMessage .getTarget ());
1597
+ sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1598
+ result , null ));
1599
+ } else {
1600
+ logger .warn ("Failed to find a value returning handler for '{}' method. Sending error to server." , invocationMessage .getTarget ());
1601
+ sendHubMessageWithLock (new CompletionMessage (null , invocationMessage .getInvocationId (),
1602
+ null , "Client did not provide a result." ));
1586
1603
}
1587
- }, (e ) -> {
1588
- stop (e .getMessage ());
1589
- }, () -> {
1590
- });
1604
+ } else if (hasResult ) {
1605
+ logger .warn ("Result given for '{}' method but server is not expecting a result." , invocationMessage .getTarget ());
1606
+ }
1591
1607
}
1592
1608
1593
1609
@ Override
0 commit comments