I have a Java EE application where the model is updated very frequently from various sources. In addition I have a rich client application which triggers some actions via remote EJBs but which should also display the model changes ate least every second.
What is the easiest/ best option for sending the changes from the Java EE application to Java client application? Till now I have the following options:
Option 1 is the easiest one, you can use there asynchronous EJB methods:
SERVER
@Asynchronous
public Future<String> getUpdatedModel() {
//here create blocking process until something interesting happen
return new AsyncResult<String>("model has changed!");
}
CLIENT
Future<String> updatedModel = bean.getUpdatedModel();
while(true){
String response = updatedModel.get();
//process response here
}
Option 2 looks like option 1, but you have to take care of marshaling objects, so don't bother in using plain servlet.
Option 3 looks interesting, as websockets are going to be included in Java EE7 (now you can use opensource comet implementations for servlets). In my opinion it is not designed for communication in enterprise applications, but might be fine for your usecase. There is a plenty of JSON serializers available (e.g. gson), I use that kind of communication between JS and java, and works fine.
Option 4 breaks major Java EE principles (opening your own sockets is forbidden), and I would discourage you to use it.
Option 5 Listen to Xie and use JMS! If you will use JMS topic, you could just send the message when particular event occur, and all connected client will receive the message asynchronously. It is natural Java EE way of solving this kind of problems, with out of box transactions, message redelivery and persistence if nessesary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With