[grpc-io] Re: StreamObservers in multi-thread Java application

2023-09-18 Thread Quyen Pham Ngoc
Thanks for your help. 

I found a class that ensures synchronization of StreamObserver methods.
Link: https://github.com/stargate/stargate

public class SynchronizedStreamObserver implements StreamObserver {

  private final StreamObserver streamObserver;

  public SynchronizedStreamObserver(StreamObserver streamObserver) {
this.streamObserver = streamObserver;
  }

  @Override
  public void onNext(V value) {
synchronized (streamObserver) {
  streamObserver.onNext(value);
}
  }

  @Override
  public void onError(Throwable t) {
synchronized (streamObserver) {
  streamObserver.onError(t);
}
  }

  @Override
  public void onCompleted() {
synchronized (streamObserver) {
  streamObserver.onCompleted();
}
  }
}

On Saturday, September 16, 2023 at 2:55:09 AM UTC+7 Terry Wilson wrote:

> Yes, you should not have two threads calling onNext() on the same 
> StreamObserver. If you do, you can get messages interleaved with each other 
> and most likely just produce garbage. 
>
> You need to make sure that your application acquires a lock before calling 
> any of the methods on StreamObserver. Note that you CAN concurrently call 
> the separate StreamObserver instances you have for incoming an outgoing 
> messages. 
>
> On Wednesday, September 6, 2023 at 9:41:25 PM UTC-7 Quyen Pham Ngoc wrote:
>
>> I use grpc in Java multithread application. The documentation says: 
>> "Since individual StreamObservers are not thread-safe, if multiple threads 
>> will be writing to a StreamObserver concurrently, the application must 
>> synchronize calls." 
>>
>> Is it true that with every onNext request of each StreamObservers I have 
>> to be thread-safe? What do I do when using StreamObserver in a multi-thread 
>> application environment? What happens if thread-safety is not guaranteed?
>>
>> Sincerely thank you for your help
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to grpc-io+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/2369210b-5b6b-4f02-86c2-c0b89e3a239en%40googlegroups.com.


[grpc-io] Re: StreamObservers in multi-thread Java application

2023-09-15 Thread 'Terry Wilson' via grpc.io
Yes, you should not have two threads calling onNext() on the same 
StreamObserver. If you do, you can get messages interleaved with each other 
and most likely just produce garbage. 

You need to make sure that your application acquires a lock before calling 
any of the methods on StreamObserver. Note that you CAN concurrently call 
the separate StreamObserver instances you have for incoming an outgoing 
messages. 

On Wednesday, September 6, 2023 at 9:41:25 PM UTC-7 Quyen Pham Ngoc wrote:

> I use grpc in Java multithread application. The documentation says: "Since 
> individual StreamObservers are not thread-safe, if multiple threads will be 
> writing to a StreamObserver concurrently, the application must synchronize 
> calls." 
>
> Is it true that with every onNext request of each StreamObservers I have 
> to be thread-safe? What do I do when using StreamObserver in a multi-thread 
> application environment? What happens if thread-safety is not guaranteed?
>
> Sincerely thank you for your help
>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to grpc-io+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/8129bb36-f694-4002-a224-a468dde883c7n%40googlegroups.com.