Announcement Announcement Module
Collapse
No announcement yet.
JMS Item Writer/Reader example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JMS Item Writer/Reader example

    I haven't found a definitive answer to this, and before I got further, thought I'd take a shot to see if others have done this:

    I'm being tasked with trying to reuse an existing system whose only interface is asynchronous and queue-based, I'm wondering if the following is possible:

    Step 1: read some data and build a message. Step 2: Drop message on queue using JMSItemWriter. Step 3: Wait for message to come back using JMSItemReader on the response queue. Step 4: Do some other stuff ... Rinse and repeat, a few thousand times a day.

    Or in other words, essentially using Spring Batch to force synchronous interaction with an asynchronous resource. I'd like to make sure before I get further in research, that this is A) possible, and B) not shameless abuse of the framework that will cause major headaches down the road.

    It seems like this is possible, and possibly may be one of the uses of JMS Item Reader/Writer.

  • #2
    The bit where you "wait for message to come back" is something you'd have to implement yourself (but Batch has a Poller interface and one simple implementation that we use for simliar purposes internally so you might find that useful). Unless you need the other features of Spring Batch though, it might be simpler to just use Spring JMS or Spring Integration (the latter for an extra level of abstraction).

    Comment


    • #3
      My apologies - I had meant to respond sooner. Thank you very much for the reply. We're going to start prototyping a solution similar to what you've described. Thanks again.

      Comment


      • #4
        Sending Synchronous Messages in the itemProcessor

        Hello,

        i want to send synchronous Messages on the JMS Queue, but to implement this in the itemProcessor. Anyone has an idea how i can go about this, or is there an example anywhere which shows how one can do this? I will be very grateful for any prompt response. Thank you in advance.

        Best regards

        Comment


        • #5
          I was additionally crippled by XA requirements, and a few other things. I chose not to use the poller interface, and instead implemented a set of parallel tasklets that did the following:

          tasklet 1:
          -reader: read from database
          -processor: put message on queue and got correlation id
          -write: correlation id to database

          tasklet 2:
          -reader: read from queue
          -processor: find record to correlate with and update
          -write: persist updated data from processor

          I was able to control when the second tasklet ended by using a JMSTemplate timeout, which when a timeout occurs, returns an empty object to the reader. If I remember correctly, if the reader method returns a null value, the tasklet ends gracefully.

          Not saying this is the *best* implementation, but worked for me given the scenario I was in.

          Comment


          • #6
            I am kinda of looking for same scenario in Spring Batch. Is it possible for you to provide sample implementation that uses JMS?

            Thanks in advance

            Comment


            • #7
              Can someone help on the issue faced by Manish? I too have the same problem

              Comment


              • #8
                Hello,

                I implemented the Spring batch which read the data into database, treats them and insert data to another database then I got this error message:

                log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment) .
                log4j:WARN Please initialize the log4j system properly.
                log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
                Exit status :FAILED
                Exit status :[java.lang.ClassCastException: com.entity.HolidayDay cannot be cast to com.entity.BusinessDay]
                Fait

                You can found the jobbatch.xml below:

                <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:batch="http://www.springframework.org/schema/batch"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:context="http://www.springframework.org/schema/context"
                xsi:schemaLocation="http://www.springframework.org/schema/batch
                http://www.springframework.org/schem...-batch-2.2.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.2.xsd
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schem...-beans-3.2.xsd ">

                <import resource="../config/context.xml" />
                <import resource="../config/datasource.xml" />

                <context:component-scan base-package="com.service"/>

                <bean id="validator" class="org.springframework.validation.beanvalidati on.LocalValidatorFactoryBean"/>

                <batch:job id="holidayBusinessDayJob">
                <batch:step id="step1">
                <batch:tasklet>
                <batch:chunk reader="holidayItemReader" writer="businessItemWriter"
                processor="holidayItemProcessor" commit-interval="10">
                </batch:chunk>
                </batch:tasklet>
                </batch:step>
                </batch:job>

                <bean id="holidayItemReader" class="org.springframework.batch.item.database.Jdb cCursorItemReader" scope="step">
                <property name="dataSource" ref="dataSource"/>
                <property name="sql" value="SELECT HOLIDAY_DAYS_ID, COUNTRY, YEAR, HOLIDAY_DATE, DESCRIPTION, CREATION_DATE, UPDATE_DATE FROM HOLIDAY_DAYS" />
                <property name="rowMapper">
                <bean class="com.businesscalendar.batch.itemreader.Holid ayRowMapper"/>
                </property>
                </bean>

                <bean id="businessItemWriter" class="org.springframework.batch.item.database.Jdb cBatchItemWriter">
                <property name="dataSource" ref="dataSource" />
                <property name="sql">
                <value>
                <![CDATA[
                insert into BUSINESS_DAYS(BUSINESS_DAYS_ID,COUNTRY,YEAR,BUSINE SS_DATE,CREATION_DATE,UPDATE_DATE)
                values (?, ?, ?, ?, ?, ?)
                ]]>
                </value>
                </property>
                <property name="itemPreparedStatementSetter" ref="preparedStatementSetter" />
                </bean>
                <bean id="preparedStatementSetter" class="com.businesscalendar.batch.CustomBusinessDa yItemPreparedStatementSetter"/>

                <bean id="batchValidator" class="com.businesscalendar.batch.validator.BeanVa lidation" />

                <bean id="holidayItemProcessor" class="org.springframework.batch.item.validator.Va lidatingItemProcessor">
                <property name="validator" ref="batchValidator" />
                </bean>

                </beans>

                Could you help me to solve this defect please?

                regards,
                Brice

                Comment

                Working...
                X