Announcement Announcement Module
No announcement yet.
writing JMS messages to File Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • writing JMS messages to File

    Hello everyone,
    We have a big process running and send messages to JMS Queue as and when it finishes the validation with that record.
    another process which is a subsriber to JMS queue will receive messages as and when it is available and write to file until no messages available in the queue.

    My doubt is how the message listener can write to a file.?
    Is there any pattern or way to do this/

    Any help with code snippet is much appreciated.

  • #2
    There is really no limit to the message listener. Are you using a message-driven bean or a Spring-configured MessageListener?

    If it's a Spring-configured MessageListener, you can provide a filename in the configuration. The message listener can then create a stream when it's initialized to write to this file.

    If you have concurrent message processing, you want to avoid writing to the same file in multiple threads. You can either write to different files by appending something like the thread-name to the filename or use a singleton component (also can be injected by Spring) to write to the file in a thread-synchronized way. Hope this helps.



    • #3
      Thanks for your reply Jess,
      I am thinking of using Spring-configured MessageListener.
      FileName is dynamic, which will be part of the message.
      From your reply I kind of got an idea, but still not clear.

      Is it possible to provide some code snippet please.?

      Look forward to your reply.


      • #4
        Without knowing anything more something like this may work (rough code sketch):

        public class MessageProcessor implements MessageListener {
             * Keeps a mapping of open file handles so we don't have to re-open it
             * every time.
            private Map files = new HashMap();
             * The directory where files will be written to.
            private String parentDir;
             * Message processing method uses the JMS message to write data
             * to a file.
            private void onMessage(Message m) {
                /* IOException will have to be taken care of in this method */
                String filename = null;
                /* use Message to determine filename */
                FileWriter fw = (FileWriter)files.get(filename);
                /* If we don't already have this file open, open it and save it in the map */
                if(fw == null) {
                    fw = new FileWriter(new File(parentDir, filename));
                    files.put(filename, fw);
                /* use FileWriter and Message to write to file */
             * Release all file handles.
            public void destroy() {
                /* do something with IOException below */
                for(Map.Entry ent : files.entrySet()) {
                    FileWriter fw = (FileWriter)ent.getValue();
             * Set the parent directory for files.
            public void setParentDir(String parentDir) {
                this.parentDir = parentDir;
        And then when you set it up you can do something like provide the directory where the files will go:

        <bean id="processor" class="MessageProcessor" destroy-method="destroy">
            <!-- Specify which directory to write JMS files here -->
            <property name="parentDir" value="/tmp/jmsFiles"/>
        <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <!-- Your message processor -->
            <property name="messageListener" ref="messageListener" />
            <!-- Standard JMS configuration -->
            <property name="concurrentConsumers" value="5"/>
            <property name="connectionFactory" ref="connectionFactory" />
            <property name="destination" ref="destination" />
        Hope this helps and makes sense in the context of what you want.



        • #5
          Thanks Jess,
          Now it is much clear.


          • #6
            jms + file

            log4j has a jmsappender, i intent to try this out, maybe this
            could work for you ?