ic3
ic3

Reputation: 7690

Dumping a Java program into a file and restarting it

I was just wondering if it's possible to dump a running Java program into a file, and later on restart it (same machine)

It's sounds a bit weird, but who knows

--- update -------

Yes, this is the hibernate feature for a process instead of a full system. But google 'hibernate jvm process' and you'll understand my pain.

There is a question for linux on this subject (here). Quickly, it's possible to hibernate a process (far from 100% reliable) with CryoPID.

A similar question was raised in stackoverflow some years ago.

With a JVM my educated guess is that hibernating should be a lot easier, not always possible and not reliable at 100% (e.g. UI and files).


Serializing a persistent state of the application is an option but it is not an answer to the question.

Upvotes: 16

Views: 2917

Answers (10)

user1213856
user1213856

Reputation: 86

I believe : 1- the only generic way is to implement serialization. 2- a good way to restore a running system is OS virtualization 3- now you are asking something like single process serialization.

The problem are IOs. Says your process uses a temporary file which gets deleted by the system after 'hybernation', but your program does not know it. You will have an IOException somewhere.

So word is , if the program is not designed to be interrupted at random , it won't work. Thats a risky and unmaintable solution so i believe only 1,2 make sense.

Upvotes: 3

Suzan Cioc
Suzan Cioc

Reputation: 30127

Probably Terracotta can this: http://www.terracotta.org

I am not sure but they are supporting server failures. If all servers stop, the process should saved to disk and wait I think.

Otherwise you should refactor your application to hold state explicitly. For example, if you implement something like runnable and make it Serializable, you will be able to save it.

Upvotes: 0

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

What you want is impossible from the very nature of computer architecture.

Every Java program gets compiled into Java intermediate code and this code is then interpreted into into native platform code (when run). The native code is quite different from what you see in Java files, because it depends on underlining platform and JVM version. Every platform has different instruction set, memory management, driver system, etc... So imagine that you hibernated your program on Windows and then run it on Linux, Mac or any other device with JRE, such as mobile phone, car, card reader, etc... All hell would break loose.

You solution is to serialize every important object into files and then close the program gracefully. When "unhibernating", you deserialize these instances from these files and your program can continue. The number of "important" instances can be quite small, you only need to save the "business data", everything else can be reconstructed from these data. You can use Hibernate or any other ORM framework to automatize this serialization on top of a SQL database.

Upvotes: 0

Ali
Ali

Reputation: 12684

First off you need to design your app to use the Memento pattern or any other pattern that allows you to save state of your application. Observer pattern may also be a possibility. Once your code is structured in a way that saving state is possible, you can use Java serialization to actually write out all the objects etc to a file rather than putting it in a DB.

Just by 2 cents.

Upvotes: 0

Peter Svensson
Peter Svensson

Reputation: 6173

There has been some research in "persisting" the execution state of the JVM and then move it to another JVM and start it again. Saw something demonstrated once but don't remember which one. Don't think it has been standardized in the JVM specs though...

Found the presentation/demo I was thinking about, it was at OOPSLA 2005 that they were talking about squawk

Good luck!

Other links of interest: Merpati

Aglets

M-JavaMPI

Upvotes: 7

aviad
aviad

Reputation: 8278

How about using SpringBatch framework?

As far as I understood from your question you need some reliable and resumable java task, if so, I believe that Spring Batch will do the magic, because you can split your task (job) to several steps while each step (and also the entire job) has its own execution context persisted to a storage you choose to work with.

In case of crash you can recover by analyzing previous run of specific job and resume it from exact point where the failure occurred.

You can also pause and restart your job programmatically if the job was configured as restartable and the ExecutionContext for this job already exists.

Good luck!

Upvotes: 3

Jörg Beyer
Jörg Beyer

Reputation: 3671

There are a lot restrictions any solution to your problem will have: all external connections might or might not survive your attempt to freeze and awake them. Think of timeouts on the other side, or even stopped communication partners - anything from a web server to a database or even local files.

You are asking for a generic solution, without any internal knowledge of your program, that you would like to hibernate. What you can always do, is serialize that part of the state of your program, that you need to restart your program. It is, or at least was common wisdom to implement restart point in long running computations (think of days or weeks). So, when you hit a bug in your program after it run for a week, you could fix the bug and save some computation days.

The state of a program could be surprisingly small, compared to the complete memory size used.

You asked "if it's possible to dump a running Java program into a file, and later on restart it." - Yes it is, but I would not suggest a generic and automatic solution that has to handle your program as a black box, but I suggest that you externalize the important part of your programs state and program restart points.

Hope that helps - even if it's more complicated than what you might have hoped for.

Upvotes: 7

Arshed
Arshed

Reputation: 341

I guess IDE supports debugging in such a way. It is not impossible, though i don't know how. May be you will get details if you contact some eclipse or netbeans contributer.

Upvotes: 2

Óscar López
Óscar López

Reputation: 236094

I believe what the OP is asking is what the Smalltalk guys have been doing for decades - store the whole programming/execution environment in an image file, and work on it.

AFAIK there is no way to do the same thing in Java.

Upvotes: 7

Frankie
Frankie

Reputation: 25165

This may me a bit overkill but one thing you can do is run something like VirtualBox and halt/save the machine.

There is also:
- JavaFlow from Apache that should do just that even though I haven't personally tried it.
- Brakes that may be exactly what you're looking for

Upvotes: 10

Related Questions