Send XML File JMS Queue
Many techniques are available to send XML file using JMS queue as it provides asynchronous communication channel between publisher and subscriber where both don’t know each other still getting required information. One common thing they need to follow is to registered with Queue or Topic to send and receive the messages. Sending text message is simple but sending file over the network is resource intensive. Imagine if you want to send large file over the network then you will have to serialized the data before sending it and serialized object will be loaded into memory before sending it.
To make thing simple without loading large file object into memory just create common location to upload file where both publisher and subscriber have access and send URL link through message so that subscriber could download and process file accordingly. If you are thinking what will happen to the file after subscriber processed it. Well, it’s up to you and your requirement if you want keep file for some time or whatever your purge policy is, otherwise once file process by subscriber successfully it can be deleted immediately or keep it as backup on other location for some time. In this demo I will show you how to crate command location and send file URL through message to process it.
- JBoss application server jboss-6.1.0.Final community version (You can use any version of JBoss application server which you could download from here). If you are using version other than jboss-6.1.0.Final community version only JMS configuration will be different and use this tutorial to configure Queue and topic.
- Eclipse Kepler (You could use any version of eclipse above 4.2)
- Configure JMS Queue destination on jboss server : Go to jboss-6.1.0.Final\server\default\deploy\hornetq\hornetq-jms.xml and add below Queue
- Now create maven project name JMSSendXMLFile with two class publisher and subscriber where one will publish message on queue and other will consume it (Please note: It’s publisher responsibility to create XML file send its path location through message to the subscriber so that he can consume it).
- Create publisher class: JMSQueuePublisher.java:
- Create queue subscriber class: JMSQueueSubscriber.java:
- Configure JBoss application server in eclipse using this tutorial
- To include include all JBoss related jars to to class path: right click project –> properties –> Java build path –> on library tab click add library… –> Server runtime –> if you configured Jboss in eclipse you will see JBoss Runtime server choose it –> click finish.
- Start JBoss server: Go to %JBOSS_HOME%/bin –> Double click run.bat to start the server. Once server is started it will pick up configured topic automatically.
- Both class has main method start them separately as java application. Give file path name on publisher console and hit enter:
- You will see once publisher publish file path message on queue, subscriber will pick up message and process file and print output on console:
- To file process simple to understand I didn’t use XML parser to parse file and get its value. If you are interested how to parse XML file please refer this tutorial : http://javahonk.com/read-xml-file-using-dom-parser/ OR http://javahonk.com/read-xml-xalan-java
Download Project: JMSSendXMLFile