Exploring threads in Java - java

I am looking forward to develop something to analyze the JVM threads of an application running on a server, the requirement is as below:
to access all the threads running in separate application
Print the stack of threads
Get to know the details of the events- logging the execution time alongside method details(executed in a particular thread)
I have worked out on 1 and 2 but unsure how to proceed with point 3 without actually updating the existing application (adding aop).
Is there any feature been provided by JVM to do so?

You'll want to look into JPDA (Java Platform Debugger Architecture).

You can go thorugh the eclipse plugin provided for JVM monitoring..
http://jvmmonitor.org/doc/index.html

Related

How to start Tanuki Software Wrapper automatically on interval of 2 hours

I am using Tanuki Software Wrapper for building a java application as Windows Service . I follow the example Simple HelloWorldServer Java Class and it works fine . I have made configure in wrapper.conf file wrapper.ntservice.starttype = AUTO_START for automatically starting the service on windows system starting .
But i want that my service would be automatically started on every two hours , how can i do it , if any one has idea please help me .
Thanks a lot in advance .
Finally I have done through following configuration in the wrapper.conf file as
wrapper.pausable=TRUE
wrapper.pause-on-startup=TRUE
wrapper.timer.1.interval=minute=120
wrapper.timer.1.action=restart, resume
wrapper.on_exit.default=PAUSE
It basically pause the wrapper action after main jvm(java application) is closed , and then after 2 hours it automatically restart wrapper's local JVM and resume the required output with updated data .
Thanks to all for trying to help me .
It's better to keep your java application running, and schedule tasks from within your application.
E.g. use http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
If you schedule a task in your main() method, a new Timer Thread is started, so the application will keep running after the main() has ended, and keep executing the scheduled task at the rate you specified.
Ajeet,
As GreyFairer said, it is usually a good idea to run tasks from within the JVM, especially if they happen often.
The Wrapper's ability to stop and start the JVM using the pausable feature definitely works as well. This approach can be better if your JVM is large, and the task it needs to complete is relatively infrequent. There is a bit of load to launch a JVM.
Relaunching the JVM as you are doing also has the benefit of allowing you to change the configuration for each invocation if you combine configuration include files with the wrapper.restart.reload_configuration=TRUE property. You can modify the include file as needed so each JVM runs with the needed information. (There are of course ways of getting the same results within a single JVM invocation if needed.)
Cheers, Leif

Why does VisualVm does not show all threads in a running tomcat?

My tomcat (version: 5.5.25) runs an application which I try to profile with VisualVM (ver: 1.3.2).
Everything looks nice but not all classes and methods are shown in visualVM. They ones that are missing run in thread [main]. I know this because this is the thread's name I receive if I a breakpoint has been hit. Classes which run outside main e.g. [worker1] , [worker2], ... are shown correctly.
Any idea what the reasons might be? Or what I could try?
Since the application I run (it is called Assentis Docbase) is closed-source they might have customized the default tomcat configuration. But they allowed me to extend the framework with my own classes and that are the ones I want to profile.
VisualVM I run with the default configuration as downloaded.
You probably need to customize profiling root methods. See Profiling With VisualVM, Part 1 and Profiling With VisualVM, Part 2. You can also use 'Sampler' tab to get high-level picture of what is your Tomcat doing.
Here are a couple of reasons why you may not be able to see the "main" thread:
The thread could have exited.
The thread could have changed its name by calling Thread.setName().
If you want to figure out the real reason, you will probably need to look at the Tomcat source code.
This page tells you where the settings are. Google is your friend.
The reason why VisualVM did not show my method calls in thread [main] is that VisualVM allows only to profile up to 32 threads simoultanously. It is NOT possible to allow more threads to be watched. This has been documented in Profiling With VisualVM, Part 2, section "Comparison With The NetBeans Profiler" they say:
"Profiled threads limit is always 32."
:-(
You've probably badly configured "Start profiling from classes" in the plugin config.
Say you've configured org.acme.competition.* (A) for profiling:
but you've accidentally profiled the class org.acme.reference.ReferenceImpl (B) using a command lie this:
$ cat source.txt | java -Xverify:none \
-agentpath:/usr/share/visualvm/profiler/lib/deployed/jdk16/linux-amd64/libprofilerinterface.so=/usr/share/visualvm/profiler/lib,5140 \
-cp bin/ org.acme.reference.ReferenceImpl
then this would be the wrong result:
When configuring VisualVM's Startup plugin config "Start profiling from classes" with org.acme.reference.* instead, the result is correct:
See the Startup profiler guide too.

Java5 on Windows service app - get Full Thread Dump need clarification

I've looked through couple of articles here such as:
java stack dump on windows
Thread dump programmatically /JDI (Java Debugger Interface)
But didnt catch the exact answer.
The problem:
There is a Java5 Application on Windows that's runs as a service (so we dont have a console where we are able to use Ctrl+Break for Dumping).
And sometimes Application hangs and we need a thread dump.
We've tried "jstack" but it doesnt work in our env (we found out that its Java6 only compatible).
So we made a C++ app that calls thread dump via .dll call method attaching to the Java app process, and because of this it needs Local Admin rights, that is not so good.
So we'd like other options that works without admin rights and works with Java 5 without lots of rework of existing code.
Method with Printing in LOOP thread dumps (Thread.getAllStackTraces()) is not an option because we need to refactor lots of applications in order to make it work.
So that just an util that works from "outside" of applications would be a best option.
Thanks in advance!
One option is to dump all the information using jmap, and then analyzing it using other tool.
jmap -dump:format=b,file=<filename>.hprof <jvm_pid>
I am not sure, buy I think it will work on Java 5.
References:
HPjmeter-like graphical tool to view -agentlib:hprof profiling output
You can attach to the process with JConsole to detect deadlocks and get stack traces of the threads. For more information, see here: http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

How to find problematic thread in Eclipse remote debugger?

I have a web application running in a jboss application server (But it is not jboss specific so we could also assume it is a tomcat or any other server). Now I have the problem that one thread seems to be in dead-lock situation. It uses 100% CPU all the time. I have started the server with enabled debug port and I can connect Eclipse to it. But the problem is: There are a lot of threads running. How can I find the right thread? I know the process id (from Linux "top" command) but I think this will not help. Do I really have to open each thread separately and check what they are currently doing? Or is there a way to filter the threads for "most active" or something like that in Eclipse?
You can try and generate a thread dump (CTRL+Break as shown in this thread).
Or you could attach a JConsole to the remote session (so leaving Eclipse aside for now), monitor the threads and generate a thread dump.
alt text http://www.jroller.com/dumpster/resource/tdajconsole.png
Seems to be you need to narrow things down to the code that has the bug by identifying which thread is eating the CPU first, then which code is being executed by that thread and at that point you can remote debug.
I would suggest using something like JProfiler, jvisualvm, jconsole or something similar. Using one of these tools will allow you to get some insight into what the thread is doing and should allow you to sort the threads by cpu cycles used so you kind find the offending thread quickly.

Controlling maximum Java standalone running in Linux

We've developed a Java standalone program. We've configured in our Linux (RedHat ES 4) cron
schedule to execute this Java standalone every 10 minutes. Each standalone
may sometime take more than 1 hour to complete, or sometime it may complete
even within 5 minutes.
My problem/solution I'm looking for is, the number of Java standalones executing
at any time should not exceed, for example, 5 process. So, for example,
before even a Java standalone/process starts, if there are already 5 processes running,
then this process should not be started; otherwise this would indirectly start
creating OutOfMemoryError problems. How do I control this? I would also like to make this 5 process limit configurable.
Other Information:
I've also configured -Xms and -Xmx heap size settings.
Is there any tool/mechanism by which we can control this?
I also heard about Java Service Wrapper. What is this all about?
You can create 5 empty files (with names "1.lock",...,"5.lock") and make the app to lock one of them to execute (or exit if all files are already locked).
First, I am assuming you are using the words "thread" and "process" interchangably. Two ideas:
Have the cron job be a script that will check the currently running processes and count them. If less than threshold spawn new process, otherwise exit, here threshold can be defined in your script.
Have the main method in your executing java file check some external resource (a file, database table, etc) for a count of running processes, if it is below threshold increment and start process, otherwise exit (this is assuming the simple main method will not be enough to cause your OOME problem). You may also need to use an appropriate locking mechanism on the external resource (though if your job is every 10 minutes, this may be overkill), here you could defin threshold in a .properties, or some other configuration file for your program.
Java Service Wrapper helps you set up a java program as a Windows service or a *nix daemon. It doesn't really deal with the concurrency issue you are looking at--the closest thing is a config setting that disallows concurrent instances if its a Windows service.

Categories