execute new java code in existing jvm process
Asked Answered
T

2

14

I have a java process currently running under a windows shell.

One of the threads responsible for serialisation is blocked indefinitely and as a result important information which is stored in memory is no longer being written to disk.

If I shutdown the process, the information will be lost.

It would be convenient if I could write and compile some new code and have it execute in the same memory space so that the said information could be serialised once more before I shutdown the process.

The process was started using a java -jar command.

With the hotspot VM features, is there any way to achieve this?

Twotone answered 25/1, 2010 at 5:18 Comment(4)
Can you attach a debugger to the process? If so, then you may be able to trigger the code to save the data.Planck
I have used btrace in the past in a somewhat similar situation. kenai.com/projects/btrace/pages/HomeCumulostratus
You should say what version of the JVM you're using.Melanous
Just a note for anyone planning on trying this .. you sometimes only get one shot at attaching to the process, so make sure you have everything ready and tested when you do so. I found often the 2nd time you attempt to attach to the process nothing would happen. Good luck!Twotone
M
12

You can use the Attach API to attach to a virtual machine. Here's an article that explains how to use it

Here's a code example:

String agentJAR = "myAgent.jar";
VirtualMachine vm = VirtualMachine.attach (processid);
vm.loadAgent(agentJAR);

Where the agent is the name of your jar.

The agent jar contains an Agent, which can interface with the JVM using the Instrumentation API.

To create an agent that gets loaded at runtime, you implement an agentmain function like this:

public static void agentmain(String agentArgs, Instrumentation inst); 

or

public static void agentmain(String agentArgs); 

The Instrumentation object is used to modify classes at runtime, which you probably don't need. But hopefully you can just put whatever code you need to run in agentmain and then use the attach API to run it in the target JVM.

Good luck!!

Melanous answered 25/1, 2010 at 5:48 Comment(7)
thanks, that's probably the best bet, however i doubt i'll be able to do what i want - which is someone get a handle on an objects field (which has no static reference) and serialise it. i'll do my best (worst) though!Twotone
actually, i could resolve my issue by throwing an exception on a particular thread. this thread is currently hung on a socket indefinitely. i know how to get a handle on the thread and will either need to throw an exception in it, or i may be able to terminate and restart it. will try. thx.Twotone
You could just try interrupting it.Melanous
@Chad : but it's definitely not sleeping, so i don't think that will work.Twotone
Ah. You might want to try replicating the situation on another machine (or another JVM) before you try it on your instance with the data.Melanous
WOW! got it to work! had to use reflection to create a duplicate of my blocked thread, and start the duplicate. i can't believe that worked. thanks again!Twotone
@Twotone Wow, that's awesome! congrats!Melanous
M
0

You might try registering a signal handler, this is more limited on Windows than on other platforms.

Examples and description http://www.ibm.com/developerworks/java/library/i-signalhandling/

But the question to ask is why is the thread blocked?

Mcgough answered 25/1, 2010 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.