【问题标题】:config,properties file does not update in real time while running service via the browser通过浏览器运行服务时,config,properties 文件不会实时更新
【发布时间】:2016-05-24 05:02:57
【问题描述】:

我尝试为阅读邮件服务实现一些休息 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;
        }

    }

【问题讨论】:

    标签: java spring rest servlets


    【解决方案1】:

    我不太了解您的问题,但至少,我认为您在 runService 方法中存在问题: 注意:在代码中添加了一些以 ''

    开头的 cmets
        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");
    
                }
    

    【讨论】:

    • 是的,我刚刚找到它,谢谢!但我读到这不是每次更新后阅读它的有效方式。还有其他解决方案吗?
    • 当然!您可以直接更新属性。 prop.setProperty("status", status.RUNNING.toString()); 例如在成功调用 updateConfigfile 之后。我的意思是你可以更新文件和属性对象中的属性。
    • 所以如果我理解你是正确的,我可以避免使用 updateConfigfile,直接更新 prop 对象然后重新加载它?
    • 没有。请参阅答案末尾的代码。我无法在此处格式化代码。
    • 请注意,现在,“循环内”的消息可能缺乏意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多