grpc中的Java多线程
我有一个关于JAVA多线程的问题 我有一个带有grpc流媒体客户端的jetty webapp。一切都很好,但我如何建立一个模型来获取流数据 webapp是用jsf构建的。因为我有一个控制器,它调用处理程序类来启动流:grpc中的Java多线程,java,multithreading,runnable,grpc,Java,Multithreading,Runnable,Grpc,我有一个关于JAVA多线程的问题 我有一个带有grpc流媒体客户端的jetty webapp。一切都很好,但我如何建立一个模型来获取流数据 webapp是用jsf构建的。因为我有一个控制器,它调用处理程序类来启动流: public void startStream(){ if(streamHandler!=null & activatedStream == false){ streamHandler.startStreamClient();
public void startStream(){
if(streamHandler!=null & activatedStream == false){
streamHandler.startStreamClient();
activatedStream = true;
}else{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Could not initialize the StreamHandler and Client Class or a Stream still runs. Please check the logs.", "Stream is running: "+String.valueOf(activatedStream));
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
此方法简单地启动客户端和流
public void startStreamClient(){
log.info("Calling startMethod of Handler............");
CountDownLatch finishLatch;
if(this.client.isChannelShutdown()& this.client!=null){
this.client=new StreamClient(this.serverHost, this.serverPort);
try{
finishLatch = this.client.imageStream(this.startRequest);
}catch(Exception e){
log.warn("Error while starting the imageStream: "+e.getLocalizedMessage(), e);
}
}else{
finishLatch = client.imageStream(this.startRequest);
}
}
仍然缺少检查CountDownLatch的实现。但在这种情况下,这并不重要
响应出现在这里:onNext()方法提供流数据:
public CountDownLatch imageStream(StreamRequest request){
log.info("Calling imageStream-asnychStub...............");
CountDownLatch finish = new CountDownLatch(1);
/**
* The asyncStub is calling the rpc-Function with a new StreamObserver for the given Responses from the Server.
*/
StreamObserver<StreamRequest> requestOberserver = asyncStub.streamImagaData(new StreamObserver<StreamResponse>() {
/**
* The onNext Method is getting the imageDate, if it is send
*/
@Override
public void onNext(StreamResponse response) {
System.out.println("Data-Input: "+response.getImageData().length());
}
/**
* The onError Method is getting an Exception Object if it is thrown
*/
@Override
public void onError(Throwable t) {
log.warn("Bidirectional Stream with Server an Client: "+t.getLocalizedMessage(), t);
}
/**
* The onCompleted is for ending the Stream and reduces the CountDownLatch by one
*/
@Override
public void onCompleted() {
log.info("Bidirectional Streaming has finished....");
finish.countDown();
}
});
/**
* This Block is for sending a StreamRequest to the Server.
*/
try{
log.info("Sending a Streaming-Request to Server with State: "+request.getStreamState().name());
requestOberserver.onNext(request);
}catch (RuntimeException ex) {
log.warn("Error sending requst to Server: "+ex.getLocalizedMessage(), ex);
requestOberserver.onError(ex);
}
requestOberserver.onCompleted();
return finish;
}
公共倒计时闩锁imageStream(流请求){
log.info(“将imageStream调用为NYCHSTUB…………”;
倒计时闩锁完成=新倒计时闩锁(1);
/**
*asyncStub正在使用新的StreamObserver调用rpc函数,以获得来自服务器的给定响应。
*/
StreamObserver requestOberserver=asyncStub.streamImagaData(新的StreamObserver(){
/**
*onNext方法正在获取imageDate(如果是send)
*/
@凌驾
public void onNext(流响应){
System.out.println(“数据输入:”+response.getImageData().length());
}
/**
*如果抛出异常对象,onError方法将获取该异常对象
*/
@凌驾
公共作废登记员(可丢弃的t){
log.warn(“与服务器和客户端的双向流:+t.getLocalizedMessage(),t”);
}
/**
*onCompleted用于结束流并将CountDownLatch减少1
*/
@凌驾
未完成的公共无效(){
log.info(“双向流媒体已完成…”);
完成。倒计时();
}
});
/**
*此块用于向服务器发送StreamRequest。
*/
试一试{
log.info(“向状态为“+Request.getStreamState().name()”的服务器发送流请求”);
requestOberserver.onNext(请求);
}捕获(RuntimeException ex){
log.warn(“将请求发送到服务器时出错:”+ex.getLocalizedMessage(),ex);
requestOberserver.OneError(ex);
}
requestOberserver.onCompleted();
返回完成;
}
图像数据简单地打印在屏幕上。我试图建立消费者-生产者模型,但失败了,因为响应返回StreamObserver的内部类型
如何实时获取这些数据。我是否必须创建StreamObserver的正式实现?或者我必须在哪里放置额外的线程?线程是唯一的选择吗?我需要打电话吗
提前谢谢 我通过实现观察者模式解决了这个问题
ManagedBean成为StreamClient的观察者。您还需要在
onError
回调中调用finish.countDown()
。