Skip to main content

Spring MVC Job Scheduling

Create a Spring MVC project 


Open spring tool suite
File > New > Spring Project > Spring Mvc Project
Test whether the project is working, if it's ok you should see a hello page in your browser.

Adding Dependency


Add following to pom.xml.

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>

 Create the scheduler

This is the class you want to schedule. Name it as ScheduledTasks.java and place with homecontroller.java

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;


public class ScheduledTasks {

    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
   
    @Scheduled(fixedRate=5000)
    public void reportCurrentTime(){
        System.out.println("time is : "+dateFormat.format(new Date()));
    }
   
}
 reportCurrentTime() methos will be called by Spring Scheduler every 5 seconds as the value set is 5000.

Configurations

This sample will use annotation based approach, so we shall create a AppConfig.java class for configurations.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class AppConfig {

    @Bean
    public ScheduledTasks bean(){
        return new ScheduledTasks();
    }
}
This @EnableScheduling annotation will ensure all the methods with @scheduled to add to scheduler.

Execution

Once your run the web application check the console output. You will notice time is been logged every 5 seconds.


Further

We can improve our application and change the pool size.  Let's change the pool size to 10. Change is made to AppConfig.java
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {

    @Bean
    public ScheduledTasks bean(){
        return new ScheduledTasks();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecuter());
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskExecuter() {
       
        return Executors.newScheduledThreadPool(10);
    }
}

Comments

Popular posts from this blog

Oracle Database 12c installation on Ubuntu 16.04

This article describes how to install Oracle 12c 64bit database on Ubuntu 16.04 64bit. Download software  Download the Oracle software from OTN or MOS or get a downloaded zip file. OTN: Oracle Database 12c Release 1 (12.1.0.2) Software (64-bit). edelivery: Oracle Database 12c Release 1 (12.1.0.2) Software (64-bit)   Unpacking  You should have following two files downloaded now. linuxamd64_12102_database_1of2.zip linuxamd64_12102_database_2of2.zip Unzip and copy them to \tmp\databases NOTE: you might have to merge two unzipped folders to create a single folder. Create new groups and users Open a terminal and execute following commands. you might need root permission. groupadd -g 502 oinstall groupadd -g 503 dba groupadd -g 504 oper groupadd -g 505 asmadmin Now create the oracle user useradd -u 502 -g oinstall -G dba,asmadmin,oper -s /bin/bash -m oracle You will prompt to set to password. set a momorable password and write it down. ...

DBCA : No Protocol specified

when trying to execute dbca from linux terminal got this error message. now execute the command xhost, you probably receiving No protocol specified xhost:  unable to open display ":0" issue is your user is not allowed to access the x server. You can use xhost to limit access for X server for security reasons. probably you are logged in as oracle user. switch back to default user and execute xhost again. you should see something like SI:localuser:nuwan solution is adding the oracle to access control list xhost +SI:localuser:oracle now go back to oracle user and try dbca it should be working

How to Summarize Real Time Event Data Using Siddhi CEP

These days I am working on a task to improve the performance of a wso2telco analytics which is based wso2 DAS. Our product does data summarizing using apache spark. When the data increases it hangs even though we have applied wso2 incremental processing. Hence we decided to do one level data summarizing on real time as soon as data arrives at wso2 siddhi event processor. spark will do summarize per hour while siddhi will summarize per minute. To summarize data in real time we can use siddhi window feature. it can hold the events arrived up to a specified time and then release all the accumulated events at once. Since the events received are released at once we can use summarizing methods to summarize them. Following describes few attempts how I  tried to do this. Attempt 01 This was the attempt suggested by wso2 support team https://github.com/wso2/analytics-apim/blob/master/features/org.wso2.analytics.apim.feature/src/main/resources/template-manager/executionplans/AP...