Results 1 to 8 of 8

Thread: Accept requests at scheduled times

  1. #1
    Join Date
    Sep 2006
    Posts
    100

    Default 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. #2
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    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.

  3. #3
    Join Date
    Sep 2006
    Posts
    100

    Default

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

  4. #4
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    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

  5. #5
    Join Date
    Sep 2006
    Posts
    100

    Default

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    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/

  7. #7
    Join Date
    Sep 2006
    Posts
    100

    Default

    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?

  8. #8
    Join Date
    Sep 2006
    Posts
    100

    Default

    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!

Tags for this Thread

Posting Permissions

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