Announcement Announcement Module
Collapse
No announcement yet.
Calling an existing service layer method every hour Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calling an existing service layer method every hour

    I would like to reuse my service layer call(running within Spring container @Service) and invoke it every hour as a batch job since normal run fails because the request times out. How can I go about running this as a batch. I understand from other posts that running it within the container might cause it to time out.

    Still trying to understand Spring batch samples so a base level explanation would help.

  • #2
    Unrelated but another question... I have defined the following and created classes...

    How do I schedule this to run from my ear

    Code:
    	<job id="parallelJob" xmlns="http://www.springframework.org/schema/batch">
    		<step id="staging">
    			<tasklet>
    				<chunk reader="sampleReaderArticle1" 
    				       processor="sampleProcessorArticle1" 
    				       writer="sampleWriterArticle1" 
    				       commit-interval="2"/>
    			</tasklet>
    		</step>
    	</job>
    	
    	<bean id="sampleReaderArticle1" class="com.project.batch.SampleReader">
    	</bean>
    	<bean id="sampleProcessorArticle1" class="com.project.batch.SampleProcessor">
    	</bean>
    	<bean id="sampleWriterArticle1" class="com.project.batch.SampleWriter">
    	</bean>

    Comment


    • #3
      you can use Spring Scheduling support to schedule the execution of your batch jobs. This module is part of the Spring Framework, not Spring Batch. You can choose to call your business service directly (no Spring Batch at all) or wrap the call in a Tasklet (itself in a job). With the latter, you'll benefit from Spring Batch execution metadata features (monitoring, restart, etc.)

      Comment


      • #4
        Thats useful information. I didn't know that the Framewrk had scheduling support.

        Assuming that I head the Batch direction,
        call in a Tasklet
        How do I invoke the job by the hour.
        Code:
        <batch:job-repository id="jobRepository"/>
        
        <!-- Do I need jobParametersIncrementer -->		
        <job id="loopJob" incrementer="jobParametersIncrementer" xmlns="http://www.springframework.org/schema/batch">
        	<step id="step1">
        		<tasklet>
        			<chunk reader="reader" writer="writer" commit-interval="3"/>
        		</tasklet>
        	</step>
        </job>
        
        <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        	<property name="jobRepository" ref="jobRepository" />
        	<property name="taskExecutor">
        		<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
        	</property>
        </bean>		
        
        <!-- From the Spring samples -->
        <bean id="reader" class="com.testProject.batch.InfiniteLoopReader" />
        <bean id="writer" class="com.testProject.batch.InfiniteLoopWriter" />
        
        <!-- Do I need this -->
        <bean id="jobParametersIncrementer" 
        	    class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>

        Comment


        • #5
          somthing like this :
          Code:
                  <task:scheduled-tasks>
          		<task:scheduled ref="fooJobLaucher" method="launch" cron="0 0 0/1 * * ?" />
          	</task:scheduled-tasks>

          Comment


          • #6
            I think I now have got all my configurations right. So how do I trigger the job the first time.
            Code:
            public void triggerJobFromTempFlex(String trial) {
            	try {
            		System.out.println("*******Batch Job begin***********");
            //		jobLauncher.run(job, new JobParameters());
            		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
            		System.out.println(jobExecution.getStatus());
            	} catch (Exception e) {
            		e.printStackTrace();
            	}
            	System.out.println("***********Batch Job End*************");
            	return null;
            }
            [9/28/11 14:48:59:423 IST] 0000002e SystemErr     R org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=1, version=0, JobParameters=[{}], Job=[loopJob]

            Comment


            • #7
              Thanks ticino, but what namespace should I define.

              Caused by: org.xml.sax.SAXParseException: The prefix "task" for element "task:scheduled-tasks" is not bound.

              Comment


              • #8
                looks like there's already something running for this job. You should read this section in the manual: http://static.springsource.org/sprin...html#domainJob . It explains the relations between job, job instances, job executions and job parameters.

                Comment


                • #9
                  Originally posted by Java Developer View Post
                  Thanks ticino, but what namespace should I define.

                  Caused by: org.xml.sax.SAXParseException: The prefix "task" for element "task:scheduled-tasks" is not bound.
                  you must add the task namespace, just like you added the batch namespace. This is Spring stuff, not Spring Batch.

                  Comment


                  • #10
                    Should I mix both Spring's scheduling and Spring Batch.... Is it a good practice mixing both in. Can I not schedule using Spring batch alone ?

                    About the job running in the background. The only place where I think this might be configured is listed below. Can you help point which one triggers the job.
                    Code:
                    <bean id="jobRepository"
                    	class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
                    	<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
                    	<property name="dataSource" ref="dataSource" />
                    	<property name="transactionManager" ref="transactionManager" />
                    </bean>
                    
                    <!-- Do I need jobParametersIncrementer -->		
                    <job id="loopJob" incrementer="jobParametersIncrementer" xmlns="http://www.springframework.org/schema/batch">
                    	<step id="step1">
                    		<tasklet>
                    			<chunk reader="reader" writer="writer" commit-interval="3"/>
                    		</tasklet>
                    	</step>
                    </job>
                    
                    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
                    	<property name="jobRepository" ref="jobRepository" />
                    	<property name="taskExecutor">
                    		<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
                    	</property>
                    </bean>		
                    
                    <!-- From the Spring samples -->
                    <bean id="reader" class="com.testProject.batch.InfiniteLoopReader" />
                    <bean id="writer" class="com.testProject.batch.InfiniteLoopWriter" />
                    
                    <!-- Do I need this -->
                    <bean id="jobParametersIncrementer" 
                    	    class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>

                    Comment


                    • #11
                      there's no scheduling support in Spring Batch, so it's very common to mix Spring for scheduling with Spring Batch.

                      take a look at the Spring reference documentation to learn how Spring Scheduling works. You'll need to write a wrapper around the Spring Batch's JobLauncher to launch the job, then you'll need to schedule this wrapper with Spring Scheduling. You can take a look at the "scheduling" sample in this workshop: https://github.com/acogoluegnes/Spring-Batch-Workshop

                      Comment


                      • #12
                        I suggest you install STS tools or suite, there you have very good support for namespaces.
                        What is doing your job exacly ? Is named infinite loop and you start every hours, it look like to me it never finish.

                        If you are planning using batch i you may buy batch in action book (there is a final version since last week). I readed some meap versions and used in my project, it's was/is very useful.

                        Comment

                        Working...
                        X