Sending JSON to a JMS Queue
home
Illustrates how to use ActiveMQ, an open source JMS implementation from Apache that supports JMS 1.1 specification.
Anypoint Studio offers easy-to-use components to connect to JMS Queues and Topics.
Example
In this example, an HTTP request holding the JSON sales data reaches the HTTP endpoint in the byte array format. A transformer then converts the JSON data from a byte array into a string. A success message is logged and the string is added to a JMS queue. Once a message reaches the JMS queue it can be viewed through the activeMQ admin console.
Set Up and Run the Example
Complete the following procedure to create, then run this example in your own instance of Anypoint Studio.
Download this example application to Anypoint Studio. Do not run the application yet.
Start the ActiveMQ server, navigate to the ActiveMQ home directory in the command terminal, and use the bin/activemq start command to start the activeMQ server. You should see a message that is similar to this indicating that your activeMQ server is running:
INFO: Using java '/System/Library/Frameworks/JavaVM.framework/Home/bin/java' INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details INFO: pidfile created : . . .
Configure the project build path by adding the ActiveMQ JAR file to the project folder. Right click the project folder in the Package Explorer, and click Properties > Java Build Path. Under the Libraries tab, click Add External Jars.
Browse to the ActiveMQ home directory and add the JAR file named activemq-all-5.4.3.jar - the ActiveMQ version at the time of this documentation was 5.4.4. Click Open > Ok.
In the Package Explorer, right-click the project, and click Run As > Mule Application. Studio runs the application on the embedded server and connects to the localhost on the port to which the HTTP endpoint is configured.
Send JSON Data to the URL using the REST console. You can use curl to do so. Use the following screenshot as a guide to fill in the required details within the REST console. Click Send to send the JSON data as an HTTP request to your localhost server.
Request URI: http://localhost:8081/sales Request method: POST Body : {"ITEM_ID"= 001, "ITEM_NAME" = "Shirt", "QTY" = 1, "PRICE" = 20}
Log in to ActiveMQ admin page at http://localhost:8161/admin/queues.jspwith the default username and password βadminβ. View if the message was added to the queueIn the ActiveMQ queue, click on the sales link and then on the link under the message ID column. On doing so, you can see details of the message that was added to the JMS queue:
Documentation
Read full documentation in GitHub
Examples to Try Next
If you understood this example | |
---|---|
JMS Message Rollback and Redelivery - Learn how to implement JMS rollback and redelivery using activeMQ MOM. | View |
If you struggled with this example | |
---|---|
Sending JSON Data to an AMQP Queue - Learn how to use the AMQP connector to send JSON data to RabitMQ. | View |