@EnableScheduling在Java 1.7代码中似乎不起作用

时间:2019-01-23 22:21:52

标签: java spring

Am使用Java 1.7和Spring 4.3.4.RELEASE

在以下位置具有属性文件:

/opt/myapp.properties

这仅包含以下条目:

name = true

Java代码

@EnableScheduling
@Controller
public class PropertiesUtil {

    @Scheduled(fixedDelay = 10000) 
    public String getPropertyValue() throws IOException {
        Properties properties = new Properties();
        InputStreamReader in = null;
        String value = null;
        try {
             in = new InputStreamReader(new FileInputStream("/opt/myapp/app.properties"), "UTF-8");
             properties.load(in);
             value =  properties.getProperty("name");
             logger.info("\n\n\t\tName: " + value + "\n\n");
        } 
        finally {
            if (null != in) {
                try {
                    in.close();
                } 
                catch (IOException ex) {}
            }
        }
        return value;
    }
}

我的休息端点:

@RestController
public class PropertyController {
    @RequestMapping(value="/checkProperty", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<Object> checkProperty() throws IOException {
        PropertiesUtil propertiesUtil = new PropertiesUtil();
        String value = propertiesUtil.getPropertyValue();
        return new ResponseEntity<Object>("Check for Property", headers, HttpStatus.OK);
    }
}

在构建此mvn全新安装并将其部署为war文件时,我必须明确命中我的其余端点才能正常工作(以查看日志文件中的“ name = true”)...

正在尝试让Spring Web App使用/opt/myapp/app.properties@EnableScheduling批注每10秒检查一次@Scheduled(fixedDelay = 10000)文件。

现在,我必须手动点击我的Rest Endpoint来查看属性的值。

2 个答案:

答案 0 :(得分:0)

我认为您需要拆分方法。按计划,它不需要退货。我的意思是它需要:

@Scheduled(fixedDelay = 10000) 
    public void getProperty(){
        String value = caculateValueFromProperties();
        //do something you want. Bellow my example.
        log.info("Value after calculate "+value);

    }

//拆分新方法

 public String caculateValueFromProperties() {
    Properties properties = new Properties();
            InputStreamReader in = null;
            String value = null;
            try {
                 in = new InputStreamReader(new FileInputStream("/opt/myapp/app.properties"), "UTF-8");
                 properties.load(in);
                 value =  properties.getProperty("name");
                 logger.info("\n\n\t\tName: " + value + "\n\n");
            } 
            finally {
                if (null != in) {
                    try {
                        in.close();
                    } 
                    catch (IOException ex) {}
                }
            }
            return value;
        }
  

以@Scheduled注释的方法必须具有无效的返回值并且必须   没有任何论据。这是因为它是周期性的   传递参数或接收返回值没有多大意义。

答案 1 :(得分:0)

通过创建Spring Config文件使其正常工作:

@Configuration
@EnableScheduling
public class PropertiesUtilConfig {

    @Bean
    public PropertiesUtil task() {
        return new PropertiesUtil();
    }

}

PropertiesUtil不需要@EnableScheduling批注,只需要@Controller:

@Controller
public class PropertiesUtil {

    @Scheduled(fixedDelay = 10000) 
    public String getPropertyValue() throws IOException { 
        // inline code
    }
}