Announcement Announcement Module
Collapse
No announcement yet.
Accept requests at scheduled times Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accept requests at scheduled times

    I am nube to Spring Integration so, please bear with me. My need is to accept incoming messages during a particular time frame (ex: M-F 6AM - 6PM) and rest of the times messages be directed to a pending transactions channel. This requirement is to cope up with the nightly cycle and not to error out requests received during this time (they be processed asynchronously). My first thought was to use a job scheduler like Quartz but, I am not sure how appropriate that is - as I am not initiating any jobs to be fired, but be responsive to the requests.

    Appreciate your help.

    Thanks.

  • #2
    I would say of the top of my head I would create a custom router which would have references to pending-channel and processing-channel. It would also have an instance variable of currentChannel which would be set by Cron-based tasks using Spring 3.0 scheduling support
    Something like this:
    Code:
    public class TimeBasedRouter {
    	private MessageChannel processingChannel;
    	private MessageChannel pendingChannel;
    	private MessageChannel currentChannel;
    	
    	public TimeBasedRouter(MessageChannel processingChannel, MessageChannel pendingChannel){
    		this.processingChannel = processingChannel;
    		this.pendingChannel = pendingChannel;	
    	}
    	public void resetCurrentChannelToPending(){
    		this.currentChannel = pendingChannel;
    	}
    	public void resetCurrentChannelToProcessing(){
    		this.currentChannel = processingChannel;
    	}
    	public MessageChannel route(){
    		return currentChannel;
    	}
    }
    Code:
    <int:router ref="timeBasedRouter" method="route" input-channel="input"/>
    	
    <int:channel id="processingChannel"/>
    	
    <int:channel id="pendingChannel">
    	<int:queue/>
    </int:channel>
    	
    
    <bean id="timeBasedRouter" class="foo.bar.TimeBasedRouter">
    	<constructor-arg ref="processingChannel"/>
    	<constructor-arg ref="pendingChannel"/>
    </bean>
    	
    <task:scheduled-tasks>
    	<task:scheduled ref="timeBasedRouter" method="resetCurrentChannelToProcessing" cron="0 0 6 ? * MON-FRI"/>
    	<task:scheduled ref="timeBasedRouter" method="resetCurrentChannelToPending" cron="0 0 18 ? * MON-FRI"/>
    </task:scheduled-tasks>
    As you can see the current channel will be set by the scheduled task on the actual router.

    Comment


    • #3
      Thank you very much! I will try it and post the result.

      Comment


      • #4
        You can also try a different strategy.
        Have only one channel (Queue).
        Configure a polling consumer (see: http://static.springsource.org/sprin...oint-namespace ) and only poll for messages from the Queue within a particular interval defined by the crone expression.

        I believe this one will be simpler and you won't have to worry about what to do with Messages in the pending channel at 6 AM

        Comment


        • #5
          I am getting an error on namespace handler - "Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/task]". I am assuming I need to include a jar for this and don't know what it is? I looked around; but there seems to be spring-task jar for 3.0 but, I am using Spring 2.5.6. Any help is greatly appreciated.
          Thanks.

          Comment


          • #6
            You need to use Spring 3.0 to use <task> support.
            I would also suggest to read this:
            http://blog.springsource.com/2010/01...in-spring-3-0/

            Comment


            • #7
              Looks like the scheduler is not running-so no channel switching is taking place; the current channel being returned is null. What am I missing? Should the router class be implementing some interface?

              Comment


              • #8
                My bad. It was my mistake. I got it working. I *almost* forgot that I need to initialize what my current channel is and the cron job simply switches it over at the scheduled time. Works like a charm now! Thank you very much!

                Comment

                Working...
                X