How do I send events (push notifications) from Java EE applications?

I have a Java EE application where the model is updated very frequently from various sources In addition, I have a rich client application that triggers some operations through remote EJBs, but should also display model changes at least once per second

What is the easiest / best choice to send changes from a Java EE application to a java client application? Until now, I have the following options:

>Polling a remote EJB from the client every second > polling a servlet (is it better to use JSON / XML instead of Java object serialization? Is there any other serialization?) > WebSockets (can I send Java objects here? Or must I serialize the results to JSON, for example?) > TCP socket connection (the server will provide the port to which the client connects at startup. Model changes are sent through standard object serialization. Is this "allowed" in Java EE applications?)

Solution

Option 1 is the simplest. You can use the asynchronous EJB method:

The 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 deal with marshalling objects, so don't bother using a normal servlet

Option 3 looks interesting because WebSockets will be included in Java EE 7 (now you can use the servlet's opensource Implementation) In my opinion, it is not designed for communication in enterprise applications, but it may apply to your use cases There are many JSON serializers available (such as gson). I use this communication between JS and Java and it works normally

Option 4 breaks the main Java EE principle (prohibit opening your own socket), and I don't encourage you to use it

Option 5 listen to Xie and use JMS! If you will use JMS topics, you can send messages when specific events occur, and all connected clients will receive messages asynchronously It is a natural Java EE way to solve such problems, with out of the box transactions, message re delivery and persistence (if any)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>