Results 1 to 6 of 6

Thread: writing JMS messages to File

  1. #1
    Join Date
    Jun 2006
    Location
    UK
    Posts
    21

    Default 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. #2
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    122

    Default

    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.

    Jess

  3. #3
    Join Date
    Jun 2006
    Location
    UK
    Posts
    21

    Default

    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. #4
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    122

    Default

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

    Code:
    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();
                fw.close();
            }
        }
    
        /**
         * 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:

    Code:
    <bean id="processor" class="MessageProcessor" destroy-method="destroy">
        <!-- Specify which directory to write JMS files here -->
        <property name="parentDir" value="/tmp/jmsFiles"/>
    </bean>
    
    <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" />
    </bean>
    Hope this helps and makes sense in the context of what you want.

    Jess

  5. #5
    Join Date
    Jun 2006
    Location
    UK
    Posts
    21

    Default

    Thanks Jess,
    Now it is much clear.

  6. #6
    Join Date
    Aug 2006
    Posts
    129

    Default jms + file

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •