Sobes.tech
Junior — Middle

Did you save the internal state of the service in your last project?

sobes.tech AI

Answer from AI

In my last project, I stored the internal state of the service to ensure stability and correct operation during restarts or failures. For example, I used serialization of the state to a file or database so that upon recovery, the service could continue from the last saved state.

Example of saving state in Java:

public class MyService {
    private int counter = 0;

    public void increment() {
        counter++;
    }

    public void saveState() {
        // Save state to a file
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("service_state.dat"))) {
            oos.writeInt(counter);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void loadState() {
        // Load state from a file
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("service_state.dat"))) {
            counter = ois.readInt();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Did you save the internal state of the service in… - sobes.tech