You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot实现每周自动发送带报告邮件的方案咨询

Hey there! Sounds like you want to turn your manual report-and-email API into a fully automated weekly job—totally achievable in Spring Boot. Let’s walk through the most practical solutions tailored to your setup.

Option 1: Use Spring's Built-in @Scheduled (Simplest Approach)

This is the fastest way to add automation since it’s baked into Spring Boot, no extra dependencies needed. Here’s how to adapt your existing code:

Step 1: Enable Scheduling

First, add the @EnableScheduling annotation to your Spring Boot main application class to turn on the scheduler:

@SpringBootApplication
@EnableScheduling
public class YourAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourAppApplication.class, args);
    }
}

Step 2: Refactor Your Email Logic

Your current triggerEmail method is tied to a POST request with a RequestBody. To reuse it for automation, extract the core report generation and email-sending logic into a standalone method. This separates the HTTP trigger from the actual business logic:

@RestController
public class EmailController {

    @Autowired
    private JavaMailSender mailSender;

    // Existing manual trigger endpoint
    @RequestMapping(path = "/email/trigger", method = RequestMethod.POST)
    public String triggerEmail(@RequestBody Map<String,String> msg) {
        sendReportEmail(msg);
        return "Email sent successfully";
    }

    // Core logic: Generate report and send email
    private void sendReportEmail(Map<String,String> context) {
        SimpleMailMessage message = new SimpleMailMessage();
        // Populate message details (replace with your existing logic)
        message.setTo("recipient@yourcompany.com");
        message.setSubject("Weekly Performance Report - " + LocalDate.now());
        
        // Generate your report (replace with your actual report logic)
        String reportContent = generateWeeklyReport(context);
        
        message.setText(reportContent);
        mailSender.send(message);
    }

    // Your existing report generation logic
    private String generateWeeklyReport(Map<String,String> context) {
        // Example: Fetch data from DB, compile stats, format content
        return "Weekly Report:\nTotal Users: 1200\nRevenue: $45k\n" + context.get("additionalNotes");
    }
}

Step 3: Add the Scheduled Task

Add a new method annotated with @Scheduled to run your weekly job. Use a cron expression to define the execution time (e.g., every Monday at 1 AM):

@RestController
public class EmailController {
    // ... existing code ...

    // Weekly automated task: Runs every Monday at 1 AM
    @Scheduled(cron = "0 0 1 ? * MON")
    public void scheduledWeeklyEmail() {
        // Create a default context map if your report needs parameters
        Map<String,String> weeklyContext = new HashMap<>();
        weeklyContext.put("additionalNotes", "Generated automatically via weekly schedule");
        
        sendReportEmail(weeklyContext);
    }
}

Cron Expression Breakdown:

0 0 1 ? * MON translates to:

  • 0: Second (0 seconds past the minute)
  • 0: Minute (top of the hour)
  • 1: Hour (1 AM)
  • ?: Day of month (ignored since we’re specifying day of week)
  • *: Month (every month)
  • MON: Day of week (Monday)

Key Notes for This Approach:

  • Test First: Use a shorter cron like 0/10 * * * * ? (runs every 10 seconds) to verify your logic works before setting the weekly schedule.
  • Exception Handling: Wrap the scheduled method in a try-catch to log failures and prevent unexpected stops:
    @Scheduled(cron = "0 0 1 ? * MON")
    public void scheduledWeeklyEmail() {
        try {
            // ... your logic ...
        } catch (Exception e) {
            // Log the error (use a logger like SLF4J)
            log.error("Failed to send weekly report email", e);
            // Optional: Add retry logic here or flag the failure for manual follow-up
        }
    }
    
  • Thread Pool: By default, Spring uses a single thread for scheduled tasks. If you have multiple jobs, configure a thread pool in application.properties:
    spring.task.scheduling.pool.size=5
    spring.task.scheduling.thread-name-prefix=weekly-report-
    

Option 2: Use Quartz Scheduler (For Advanced Needs)

If you need more control (e.g., dynamic schedule changes, job persistence, or complex triggers), go with Quartz. Here’s a quick setup:

Step 1: Add Quartz Dependency

Add this to your pom.xml (Maven):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

Step 2: Define a Quartz Job

Create a job class that calls your email logic:

@Component
public class WeeklyReportJob implements Job {

    @Autowired
    private EmailController emailController;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Map<String,String> weeklyContext = new HashMap<>();
        weeklyContext.put("additionalNotes", "Generated via Quartz scheduler");
        emailController.sendReportEmail(weeklyContext);
    }
}

Step 3: Configure Job and Trigger

Create a configuration class to register the job and its schedule:

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail weeklyReportJobDetail() {
        return JobBuilder.newJob(WeeklyReportJob.class)
                .withIdentity("weeklyReportJob")
                .storeDurably() // Keep job even if no trigger is attached
                .build();
    }

    @Bean
    public Trigger weeklyReportTrigger() {
        CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule("0 0 1 ? * MON");
        return TriggerBuilder.newTrigger()
                .forJob(weeklyReportJobDetail())
                .withIdentity("weeklyReportTrigger")
                .withSchedule(schedule)
                .build();
    }
}

Best Practice Tip

For cleaner code, move your email and report logic into a dedicated @Service class instead of keeping it in the controller. This makes it easier to test and reuse across both the HTTP endpoint and scheduled tasks.

内容的提问来源于stack exchange,提问作者saifeddine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:44:13