Time needed to complete ~40 minutes
1.Introduction
This post will demonstrate how to use websockets with activeMQ. The client will receive
data from message broker using stomp protocol via websockets.
data from message broker using stomp protocol via websockets.
2.What is used in this tutorial:
1- Maven 3
2- Jdk 1.8
3- Tomcat 7.0.55
4- Eclipse Luna 4.4
5- Spring 3.1.1.
6- jQuery and plugins
7- Stomp protocol (stomp.js)
8-ActiveMQ 5.10.0 - Link to home Page
9- Web browser supporting websockets
6- jQuery and plugins
7- Stomp protocol (stomp.js)
8-ActiveMQ 5.10.0 - Link to home Page
9- Web browser supporting websockets
3.Project Structure
4. Controller
- HomeController Listing 4.1, is for displaying web page.
- LiveRandomData class, listing 4.2, will connect and send messages to message broker, new message will be generated every 150 ms, it will send messages to defined destination simData , each message is a list of company objects that contains five companies with the stock price and change, the prices and change data is generated in method generateRandomdata().
note: don't forget to change phoenix-PC in MESSAGE_BROKER_URL to ip address or computer name where ActiveMQ is running.
5. View
- Page stock.jsp contains a table Listing 5.1 where the data will be displayed.6. Configuration
-In /webapp/WEB-INF/spring/appServlet/servlet-context.xml Listing 6.1.
7. Connection & data display
- Javascript is used to connect to server and get the data Listing 7.1, it is assumed that ActiveMQ is running on the local machine if not change localhost to ip address where ActiveMQ is running, the default port for websocket in ActiveMQ is 61614 this value is defined in apache-activemq-5.10.0/conf/activemq.xml file, the connect() function will initialize stompClient, disable heartbeats(see note), connect to message broker, connect_callback will subscribe to simData topic and error_callback will handle error messages. When message is received callback will handle it.
Note: As to the date of writing this post in ActiveMQ 5.10.0 there is a bug that disconnets the client with message: "Whoops! lost conection to ws://localhost:61614" for this reason heart beats are disabled and in error_callback when connection is lost it will be automatically established again.
Note: As to the date of writing this post in ActiveMQ 5.10.0 there is a bug that disconnets the client with message: "Whoops! lost conection to ws://localhost:61614" for this reason heart beats are disabled and in error_callback when connection is lost it will be automatically established again.
8. Deploying to tomcat
Note: don't forget to change username and password for tomcat server in pom.xml file
- Start tomcat.
-Start ActiveMQ (E:/Downloads/apache-activemq-5.10.0/bin/win64/activemq.bat for 64bit windows OS)
- Start tomcat.
-Start ActiveMQ (E:/Downloads/apache-activemq-5.10.0/bin/win64/activemq.bat for 64bit windows OS)
- Deploy application from run configuration in goal type clean install tomcat7:redeploy
click Apply and Run buttons.
- In browser type http://localhost:8080/mb/ to see the form picture 7.1. Clicking Connect button, in eclipse run main method in LiveRandomData.
click Apply and Run buttons.
- In browser type http://localhost:8080/mb/ to see the form picture 7.1. Clicking Connect button, in eclipse run main method in LiveRandomData.
- Video 8.1, shows the display of data.
No comments:
Post a Comment