簡體   English   中英

Java Spring Boot。 Cron問題。 變量未實例化

[英]Java Spring Boot. Cron issue. Variable is not instantiated

我的Spring Boot應用程序無法正常運行。 主要思想是用戶選擇他想知道溫度所在的城市。溫度值以城市名稱作為文件名保存到txt文件中。 然后我要計划Cron,因此,每小時將同一城市的新溫度值保存到同一文件中。 目前,除了CronManager類之外,其他所有東西都可以正常工作。 我不知道如何將城市名稱傳遞給它。 這是下面我的代碼的一部分:

應用加載器

package com.boris2barak.samplemvc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication

public class ApplicationLoader extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ApplicationLoader.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationLoader.class, args);
}
}

調節器

package com.boris2barak.samplemvc.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;

@Controller
public class myFirstController {

@RequestMapping("/hello")
public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    model.addAttribute("name", name);
    return "hello";
}

@RequestMapping("/temperature")
public String whatIsTheTemperature(Model model, @RequestParam(value = "city", required = false, defaultValue = "World") String city) throws IOException {
    model.addAttribute("city", city);
    WeatherApp data = new WeatherApp();
    return data.getTemperatureForCity(city);
}
}

WeatherApp

package com.boris2barak.samplemvc.app;
import com.google.gson.Gson;
import org.springframework.web.client.RestTemplate;
import java.io.*;

public class WeatherApp {

public String getTemperatureForCity(String city) throws IOException {
    String URL = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&APPID=9fff4e627587b84fca1ed835321da768";
    RestTemplate restTemplate = new RestTemplate();
    String json = restTemplate.getForObject(URL, String.class);
    WeatherData weatherData = new Gson().fromJson(json, WeatherData.class);

    ////......... (here i have the code to get the other data like coordinates of the city, humidity, etc which is not relevant to my problem)

    String theTemperature = weatherData.getTemperature();
    FileManager myFile = new FileManager();
    myFile.saveTheFile(theTemperature, city);
    return theTemperature;
}
}

文件管理器

package com.boris2barak.samplemvc.app;
import org.apache.commons.io.IOUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.apache.commons.io.IOUtils.toInputStream;

public class FileManager {

public void saveTheFile(String theTemperature, String cityName) throws IOException {
    InputStream streamIn = toInputStream(theTemperature, "UTF-8");
    OutputStream streamOut = new FileOutputStream(cityName + ".txt", true);
    try {
        IOUtils.copy(streamIn, streamOut);
    } finally {
        IOUtils.closeQuietly(streamIn);
        IOUtils.closeQuietly(streamOut);
    }

}
}

CronManager

package com.boris2barak.samplemvc.app;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class CronManager {

@Scheduled(cron = "0 * * ? * *")
public void cronTask() throws IOException {

WeatherApp data = new WeatherApp();
data.getTemperatureForCity(city <<<< how to instantiate this??);


}
}

我已經有2天的時間試圖解決這個問題。 非常感謝您的幫助!

問題已經解決了。 答案是這樣的: https : //stackoverflow.com/a/45287683/8258820

@Component
public class CronManager {
private Set<String> cities = new HashSet();// or CopyOnWriteArraySet/ConcurrentSkipListSet

public void addCity(String cityName){
     cities.add(cityName);
}

@Scheduled(cron = "0 * * ? * *")
public void cronTask() throws IOException {

        //iterate cities and execute logic in parallel no not block 
        WeatherApp data = new WeatherApp();
        data.getTemperatureForCity(city <<<< how to instantiate this??);
}
}

將CronManager作為組件注入到服務/控制器中,並在添加新城市時將新城市添加到CronManager.cities中。 如果您需要停止編寫城市溫度,只需將其從城市集中刪除即可。 最好創建一個新的組件CityHolder並將城市合並為一個,在這種情況下,您需要將其注入cronManager和service / controller中,但是service / controller不會依賴cron manager。 由於所有文件都有一個cron,因此最好由某些線程執行程序或@Async服務/方法並行執行此代碼。

您也可以使用spring事件。 在控制器/服務使用中,發送自定義事件AddNewCity,並在CronManager中添加此事件的偵聽器,並將事件源中的城市放入設置的城市中。


@Controller
public class myFirstController {
    @Autowired
    private CronManager cron;

    @RequestMapping("/temperature")
    public String whatIsTheTemperature(Model model, 
       @RequestParam(value = "city",required = false,defaultValue = "World") String city) 
          throws IOException {
        model.addAttribute("city", city);
        WeatherApp data = new WeatherApp();
        myCron.addCity(city);
        return data.getTemperatureForCity(city);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM