gpt4 book ai didi

java - 通过浏览器运行服务时,config、properties 文件不会实时更新

转载 作者:行者123 更新时间:2023-12-01 10:25:33 25 4
gpt4 key购买 nike

我尝试实现一些用于阅读邮件服务的 REST API。现在我有一个可以停止/运行该服务的命令。当我出于某种原因运行服务后,当我将 runService 方法中的配置文件更新为“RUNNING”时,我仍然发现服务的状态为“ONHOLD”,并且我不明白为什么。 do while 循环仅在 1 次迭代后终止。

我们在程序代码中处理文件时有延迟吗?这是我的完整代码:

    package com.javacodegeeks.snippets.enterprise;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
private final String layout = "ViewLayout";
private static enum Status{
RUNNING,ONHOLD,FINISHED,ERROR
}
Status status;

//setup method
@RequestMapping(value = "/setup/{username}/{password}/{host}/", method = RequestMethod.GET)
public String Setup(@PathVariable("username") String username,@PathVariable("password") String pass,@PathVariable("host") String host ,ModelMap model) throws IOException {
model.addAttribute("tag","Setup configuration");
OutputStream output = null;
File myfile = new File(username+".properties");
try{
if(!checkIfFileExists(myfile)){
myfile.createNewFile();
output = new FileOutputStream(myfile,false);
}
else{
model.addAttribute("msg","Error: Setup Failed, configuration for the user ="+" "+username+" "+"already exists!");
return layout;
}
Properties prop = new Properties();
prop.setProperty("username", username);
prop.setProperty("password", pass);
prop.setProperty("host", host);
prop.setProperty("status", status.FINISHED.toString());
prop.store(output, null);
}
catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

model.addAttribute("msg","Configuration successfully updated!");
return layout;
}
//run service method
@RequestMapping(value = "/run/{username}/{password}/", method = RequestMethod.GET)
public String runService(@PathVariable("username") String username,@PathVariable("password") String pass,ModelMap model){

model.addAttribute("tag","Running service procedure");
File myfile = new File(username+".properties");
if(!checkIfFileExists(myfile)){
model.addAttribute("msg","Error: Run Failed, configuration for the user ="+" "+username+" "+"not found!");
return layout;
}
else{
int i=0;
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(myfile);
prop.load(input);
if(!authenticatePassword(prop,pass)){
model.addAttribute("msg","Error: Run Failed, The password is inccorrect");
return layout;
}

} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String stat = prop.getProperty("status");
if(stat.equals(status.FINISHED.toString()) || stat.equals(status.ONHOLD.toString()))
{
updateConfigfile(username+".properties","status",status.RUNNING.toString());
do{
i++;
model.addAttribute("msg","inside loop"+" "+"Counter is:"+" "+i+" "+"status is =" +prop.getProperty("status"));
}while(prop.getProperty("status").equals(status.RUNNING.toString()));

}else{
model.addAttribute("msg","Service is already running");

}

}
return layout;
}
//get status
@RequestMapping(value = "/getstatus/{username}/{password}/", method = RequestMethod.GET)
public String getServiceSatus(@PathVariable("username") String username,@PathVariable("password") String pass,ModelMap model) {
model.addAttribute("tag","Get Status");
Properties prop = loadProperties(username+".properties");
if(prop == null){
model.addAttribute("msg","Error: Status is not available: can not read properties file!");
return layout;
}
if(!authenticatePassword(prop,pass)){
model.addAttribute("msg","Error: Get status failed, password or username is inccorrect");
return layout;
}
String status = prop.getProperty("status");
model.addAttribute("msg", "Service status is:"+" "+status);

return layout;
}
//stop service
@RequestMapping(value = "/stop/{username}/{password}/", method = RequestMethod.GET)
public String stopService( @PathVariable("username") String username,@PathVariable("password") String pass,ModelMap model) {
String message = "";
Properties prop = loadProperties(username+".properties");
if(prop == null){
model.addAttribute("msg","Error: Can not stop service, properties file does not exist or username is inccorrect!");
return layout;
}
if(!authenticatePassword(prop,pass)){
model.addAttribute("msg","Error: Can not stop service, password or username is inccorrect");
return layout;
}
String stat = prop.getProperty("status");
if(stat.equals(status.RUNNING.toString()))
{
updateConfigfile(username+".properties","status",status.ONHOLD.toString());
message = "Service was stoped";
}else{
message = "service is not running status is = "+ " "+prop.getProperty("status");
}
model.addAttribute("tag","Stop Service");
model.addAttribute("msg",message);
return layout;
}

public boolean checkIfFileExists(File filename){
if(!filename.exists()){
return false;
}
return true;
}

//function that updating properties file
public void updateConfigfile(String filename ,String key,String val){
FileInputStream in = null;
Properties props = new Properties();
try {
in = new FileInputStream(filename);
props.load(in);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
props.setProperty(key, val);
props.store(out, null);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
//function that load properties file
public Properties loadProperties(String filename){
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(filename);
prop.load(input);

} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else{
return null;
}
}
return prop;

}
public boolean authenticatePassword(Properties prop,String pass){
if(!(prop.getProperty("password").equals(pass))){
return false;
}
return true;
}

}

最佳答案

我不太明白你的问题,但至少,我认为你的 runService 方法内部有问题:注意:在以 '<--'

开头的代码中添加了一些注释
    String stat = prop.getProperty("status"); <-- you read the property here
if(stat.equals(status.FINISHED.toString()) || stat.equals(status.ONHOLD.toString()))
{
updateConfigfile(username+".properties","status",status.RUNNING.toString());
do{
i++;
model.addAttribute("msg","inside loop"+" "+"Counter is:"+" "+i+" "+"status is =" +prop.getProperty("status")); <-- status contains FINISHED or ONHOLD! It is the same as using stat variable.
}while(prop.getProperty("status").equals(status.RUNNING.toString())); <--contains FINISHED or ONHOLD! so, always return false

}else{
model.addAttribute("msg","Service is already running");

}

换句话说,如果您更改文件,您需要再次读取它,以便将更新的属性写入属性对象。

希望这有帮助!

我的意思是这指的是我最后的评论!

String stat = prop.getProperty("status");
if(stat.equals(status.FINISHED.toString()) || stat.equals(status.ONHOLD.toString()))
{
updateConfigfile(username+".properties","status",status.RUNNING.toString());
prop.setProperty("status",status.RUNNING.toString());
i++;
model.addAttribute("msg","inside loop"+" "+"Counter is:"+" "+i+" "+"status is =" +prop.getProperty("status"));
}else{
model.addAttribute("msg","Service is already running");

}

关于java - 通过浏览器运行服务时,config、properties 文件不会实时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35372896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com