SpringBoot实现定时任务

本文介绍了一个基于Spring Boot的应用实例,详细展示了pom.xml文件的依赖管理和构建配置、Application.yml中的多环境配置以及Application.java中启动器的设置。此外,还探讨了如何通过Java配置实现定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.asiainfo.cc</groupId>
  <artifactId>mail-send</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/>
  </parent> 

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.7 ,默认是1.6 -->
    <java.version>1.7</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId> 
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId> 
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <build>  
     <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <configuration>  
                <source>${java.version}</source>  
                <target>${java.version}</target>  
                <encoding>UTF-8</encoding>  
            </configuration>   
        </plugin>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-resources-plugin</artifactId>  
            <configuration>  
                <encoding>UTF-8</encoding>  
            </configuration>  
        </plugin>  
     </plugins>  
  </build> 

</project>

Application.yml

# Server settings  
test:
  msg: 0/10 * * * * ?
  loginname: cc@sina.com
  password: 123

server: 
    profiles: server1
    port: 8080
    address: 127.0.0.1  
---
server: 
    profiles: server2
    port: 8081
    address: 127.0.0.2


# SPRING PROFILES  
spring:         
    # HTTP ENCODING  
    http:  
        encoding.charset: UTF-8  
        encoding.enable: true  
        encoding.force: true  

Application.java

@SpringBootApplication
@EnableScheduling 
//@ImportResource(locations={"classpath:dubbo-client.xml"})
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).properties(
                "spring.config.active=classpath:server1",
                "spring.config.active=classpath:server2").run(args);
    }
}

Task

@Component
public class MailSending {

    @Value("${test.loginname}")
    private String loginname;

    @Value("${test.password}")
    private String password;

//  @Autowired
//  private IDubboAppUserManagementSV iDubboAppUserManagementSV;

    private final Logger logger = LoggerFactory.getLogger(this.getClass()); 

    @Scheduled(cron="${test.msg}") //每十秒执行一次
//  @Scheduled(cron="0 0/1 * * * ?") //每十秒执行一次
    public void statusCheck() {    
        logger.info("每分钟执行一次。开始……");
        new MailSending().send();
        logger.info("每分钟执行一次。结束。");
    }

    void send(){
        System.out.println("发送成功");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值