java - @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 才能查看该属性的值。
解决方案
通过创建一个 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
}
}
推荐阅读
- javascript - 使用角度 10 排除 LINT 中的参考库项目文件夹
- python - AttributeError:模块“cv2”没有属性“dnn_DetectionModel”
- elixir - 在不使用片段的情况下查询嵌入式模式
- tibco - 如何将列表中的元素映射到 Tibco 中的单个元素?
- c++ - 单击“运行”后编辑 C++ 程序
- java - 500 != 500 java Integer 请解释
- javascript - 未捕获(承诺中)TypeError:使用sweetalert进行更新确认时结果为空
- google-cloud-platform - Google Speech to text 可以离线使用吗?
- firebase - 如何在 Flutter 中正确调用 FirebaseFirestore.instance.clearPersistence()?
- linux - 如何让我的 glob 在 Linux 上为 cogs 工作