@EnableScheduling 似乎不适用于 Java 1.7 代码

我正在使用 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 clean install 并将它部署为一个war文件时,我必须明确地点击我的休息端点才能让它工作(查看我的日志文件中的“name = true”)......


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


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


茅侃侃
浏览 124回答 2
2回答

呼如林

我认为你需要拆分你的方法。在计划中,它不必有回报。我的意思是它需要:@Scheduled(fixedDelay = 10000)&nbsp;&nbsp; &nbsp; public void getProperty(){&nbsp; &nbsp; &nbsp; &nbsp; String value = caculateValueFromProperties();&nbsp; &nbsp; &nbsp; &nbsp; //do something you want. Bellow my example.&nbsp; &nbsp; &nbsp; &nbsp; log.info("Value after calculate "+value);&nbsp; &nbsp; }//拆分新方法&nbsp;public String caculateValueFromProperties() {&nbsp; &nbsp; Properties properties = new Properties();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStreamReader in = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String value = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in = new InputStreamReader(new FileInputStream("/opt/myapp/app.properties"), "UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;properties.load(in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value =&nbsp; properties.getProperty("name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logger.info("\n\n\t\tName: " + value + "\n\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (null != in) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (IOException ex) {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; }使用 @Scheduled 注释的方法必须有 void 返回,并且不能有任何参数。这是因为它是周期性的,传递参数或接收返回值没有多大意义。

忽然笑

通过创建一个 Spring Config 文件让它工作:@Configuration@EnableSchedulingpublic class PropertiesUtilConfig {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public PropertiesUtil task() {&nbsp; &nbsp; &nbsp; &nbsp; return new PropertiesUtil();&nbsp; &nbsp; }}PropertiesUtil 不需要@EnableScheduling 注解,只需要@Controller:@Controllerpublic class PropertiesUtil {&nbsp; &nbsp; @Scheduled(fixedDelay = 10000)&nbsp;&nbsp; &nbsp; public String getPropertyValue() throws IOException {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // inline code&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java