Im currently trying to set up some netty client/server application. I know how to send messages and handle them via the netty classes. My only problem is, how can i receive messages out of the messageReceived()
method. For Example, i send an message via the ChannelHandlerContext
from a method getUserInformations()
and want to wait until i get the result to work with them. This is what i have tried so far.
I only post the client side. The netty server is the standard way to build it, nothing speciel.
Thats my main class, here i press a button on my UI and send an messages via the sender Thread.
public class FXMLDocumentController implements Initializable {
public static ChannelHandlerContext handler=null;
@FXML
private TextArea txt;
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException, ExecutionException {
Sender s = new Sender();
Thread th = new Thread(s);
th.start();
txt.setText(s.get());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Thats my sender thread:
public class Sender extends Task<String> implements DataChangeListenter{
DataChangeEvent event;
@Override
protected String call() throws Exception {
DataChangedHandler.registerDataChangeListener(this);
ChannelFuture f = handler.writeAndFlush("TestMessage");
return null;
}
@Override
public void dataChangeEvent(DataChangeEvent event) {
System.out.println("Listener: "+event.getMessage());
}
}
In my sender Thread you can see the method dataChangeEvent. I used the event system from this stackoverflow post: Netty - How to get server response in the client. It works well, when i print out: System.out.println("Listener: "+event.getMessage());
- The right messages get printet, but i cant get the result in my main class where i call the sender thread to start.Could you explain me how i can get the response in the handleButtonAction()
method in my main class?