How to send events (push notifications) from a Java EE application?

I have a Java EE application where the model is updated very often from various sources. In addition, I have a rich client application that launches some actions using remote EJBs, but should also display model changes at least every second.

What is the easiest / best option to push changes from a Java EE application to a Java client application? So far I have the following options:

  • poll remote EJB every second from the client
  • servlet polling (is it preferable to use json / xml instead of serializing a Java object? Any other serialization?)
  • websockets (Is it possible to send Java objects here? Or do I need the result to be serialized for Json for example?)
  • Connecting tcp socket (the server will provide the port to which the client connects at startup. Model changes are sent through standard serialization of the object. Is this "allowed" in the Java EE application?)
+4
source share
1 answer

Option 1 is the simplest, you can use asynchronous EJB methods there:

SERVER

@Asynchronous public Future<String> getUpdatedModel() { //here create blocking process until something interesting happen return new AsyncResult<String>("model has changed!"); } 

CUSTOMER

  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 worry when using a simple servlet.

Option 3 looks interesting, since Web sites will be included in Java EE7 (now you can use open source comet implementations for servlets). In my opinion, it is not intended for communication in corporate applications, but it may be good for your use. There are many JSON serializers available (e.g. gson), I use this kind of connection between JS and java and it works great.

Option 4 violates the basic principles of Java EE (opening your own sockets is prohibited) and I will prevent you from using it.

Option 5 Listen to Xie and use JMS! If you use the JMS theme, you can simply send a message when a specific event occurs and all connected clients will receive the message asynchronously. Naturally, Java EE is a way to solve such problems, without unnecessary transactions, delivery and storage of messages, if necessary.

+3
source

Source: https://habr.com/ru/post/1489002/


All Articles