Site icon TechSling Weblog

Integrate JMS & Web Sockets In Java EE7 With CDI events

The main objective of writing this blog is to clarify integration issue between Java EE technologies and web socket API 1.0. And it is also necessary for java programmers to connect with the bugs submitted in CDI specifications, JMS and web sockets. The presence of a java programmer is highly beneficial during specification definition process. Every time when you think about web development project, web socket is kept on the top. Just think you wanted to integrate web socket with whatever is available in your hands. If you are familiar with Java EE7, you must be sure be sure about its rich functionalities like JMS 2.0 that helps in writing simple and effective code lines in no time.

You must be shocked how I am successful in sending asynchronous messages to all website sessions. The answer is quite simple, it is CDI. It helps you in establishing communication between distinct parts of application. Let us discuss java programming and how some web socket application will send and receive messages through JMS.

First of all, create a web socket server end point that will receive the message from other end. For this purpose, you need to use three key methods with the web socket, these are – onOpen, onClose, onMessage key methods.

For this purpose, you have to simply integrate JMS and web socket so that messages can be exchanged between the two. Create a session bean that will send messages to JMS queue and create JMS context to inject JMS resources inside. For more understanding, we will advise you to check practical implementation of the session bean in java programming. Once you are completed with session bean, now attach session bean and web socket together for exchanging messages.

We have already attached session bean and web socket together, now call sendMessage method inside onMessage key method. You are advised to opt for constructor injection for avoiding bugs and errors. After this step, one way communication has been established. You can send web socket message to JMS destination. Let us do the reverse function now. Now we will pull out some messages through JMS destination.

In Java programming, MessageDrive Bean is not just limited to JMS application only; it can be used for other purposes also. The basic structure for MessageDrive Bean is defined below –

  @Named

  @MessageDriven(mappedName = “jms/myQueue”)

  public class WebSocketMDB implements MessageListener {

  @Override

  public void onMessage(Message msg) { … }

  }

With CDI events, it is possible to connect with observer’s object through which message exchange process can be continued. Once you have fired CDI events, now you must know how to listen these CDI events from server end point. By following all these steps, you can establish two-way communication between JMS and web socket successfully. In case of any confusion, you can check coding section for full implementation. Thanks to the tutorial as we are able to scale up server pushed data.

Exit mobile version