代码之家  ›  专栏  ›  技术社区  ›  PacificNW_Lover

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

  •  0
  • PacificNW_Lover  · 技术社区  · 6 年前

    AM使用Java 1.7和Spring 4.3.4发布

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

    /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安装并将其部署为一个war文件时,我必须显式地点击我的rest端点才能工作(要在我的日志文件中查看“name=true”)…

    正在尝试让Spring Web应用程序检查 /opt/myapp/app.properties 每10秒使用 @EnableScheduling @Scheduled(fixedDelay = 10000) 注释。

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   BaoTrung Tran    6 年前

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

    @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注释的方法必须具有无效返回,并且必须 没有任何参数。这是因为它是周期性的 传递参数或接收返回值没有多大意义。

        2
  •  0
  •   PacificNW_Lover    6 年前

    通过创建一个spring配置文件使其正常工作:

    @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
        }
    }