Skip to content

Commit 0215215

Browse files
committed
- Allow to make thread dump directly from the application.
- CSS improvements - Refactoring
1 parent 7fa09ed commit 0215215

24 files changed

+661
-327
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ JStackFX requires the latest JDK 8 available on your system.
1111

1212
# Build
1313

14-
As JStackFX is currently under development, you have to build it manually in order to use it. In order to build it, ensure the JDK 8 is available and execute the following command:
14+
As JStackFX is currently under development, you can build it manually in order to have the latest version. In order to build it, ensure the JDK 8 is available and execute the following command:
1515

1616
```shell
17-
gradlew clean distZip
17+
gradlew clean assemble
1818
```
1919

2020
# Execution
@@ -31,6 +31,14 @@ In order to start JStackFX and open directly a dump file you can use the followi
3131
java -jar jstackfx-<version>.jar --file=/path/to/dump.txt
3232
```
3333

34+
In order to start JStackFX and make a thread dump of a given process you can use the following command:
35+
36+
```shell
37+
java -jar jstackfx-<version>.jar --pid=<pid of the process to dump>
38+
```
39+
40+
**Warning:** if both `--pid` and `--file` parameters are used, `--file` is ignored.
41+
3442
# Screenshot
3543

3644
![Screenshot of JStackFX](src/site/screenshots/JStackFX_01.png)

src/main/java/io/twasyl/jstackfx/JStackFX.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@
77
import javafx.scene.Scene;
88
import javafx.scene.control.Alert;
99
import javafx.scene.control.ButtonType;
10-
import javafx.scene.control.Dialog;
1110
import javafx.stage.Stage;
1211

1312
import java.io.File;
14-
import java.io.IOException;
1513
import java.util.Map;
1614

1715
/**
16+
* Application class of JStackFX.
17+
*
1818
* @author Thierry Wasylczenko
19-
* @since jStackFX @@NEXT-VERSION@@
19+
* @since JStackFX 1.0
2020
*/
2121
public class JStackFX extends Application {
2222
protected static final String FILE_PARAMETER = "file";
23+
protected static final String PID_PARAMETER = "pid";
2324

2425
private File fileToOpenAtStartup = null;
26+
private String pidToDumpAtStartup = null;
2527

2628
@Override
2729
public void init() throws Exception {
2830
final Map<String, String> parameters = getParameters().getNamed();
2931

30-
if (parameters.containsKey(FILE_PARAMETER)) {
32+
if(parameters.containsKey(PID_PARAMETER)) {
33+
this.pidToDumpAtStartup = parameters.get(PID_PARAMETER);
34+
} else if (parameters.containsKey(FILE_PARAMETER)) {
3135
this.fileToOpenAtStartup = new File(parameters.get(FILE_PARAMETER));
3236
}
3337
}
@@ -37,7 +41,17 @@ public void start(Stage stage) throws Exception {
3741
final FXMLLoader loader = new FXMLLoader(JStackFX.class.getResource("/io/twasyl/jstackfx/fxml/jstackfx.fxml"));
3842
final Parent root = loader.load();
3943

40-
if (this.fileToOpenAtStartup != null) {
44+
if(this.pidToDumpAtStartup != null) {
45+
final JStackFXController controller = loader.getController();
46+
try {
47+
final long pid = Long.parseLong(this.pidToDumpAtStartup);
48+
controller.dumpPID(pid);
49+
} catch (Exception e) {
50+
final Alert errorDialog = new Alert(Alert.AlertType.ERROR, e.getMessage(), ButtonType.OK);
51+
errorDialog.setTitle("Can create thread dump for process " + this.pidToDumpAtStartup);
52+
errorDialog.showAndWait();
53+
}
54+
} else if (this.fileToOpenAtStartup != null) {
4155
final JStackFXController controller = loader.getController();
4256
try {
4357
controller.loadDumpFile(this.fileToOpenAtStartup);
@@ -52,7 +66,7 @@ public void start(Stage stage) throws Exception {
5266

5367
stage.setScene(scene);
5468
stage.setTitle("JStackFX");
55-
// stage.setMaximized(true);
69+
stage.setMaximized(true);
5670
stage.show();
5771
}
5872

src/main/java/io/twasyl/jstackfx/beans/Dump.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@
88
import javafx.scene.text.Text;
99

1010
import java.time.LocalDateTime;
11+
import java.time.format.DateTimeFormatter;
1112
import java.util.ArrayList;
1213
import java.util.List;
1314

1415
/**
16+
* Class representing a thread dump realized with the {@code jstack} tool.
17+
*
1518
* @author Thierry Wasylczenko
16-
* @since jStackFX @@NEXT-VERSION@@
19+
* @since JStackFX 1.0
1720
*/
18-
public class Dump {
21+
public abstract class Dump {
22+
protected static final DateTimeFormatter DATE_TIME_FORMATTER_OUTPUT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
23+
1924
protected final ObjectProperty<LocalDateTime> generationDateTime = new SimpleObjectProperty<>();
2025
protected final StringProperty description = new SimpleStringProperty();
21-
protected final ListProperty<ThreadInformation> elements = new SimpleListProperty<>(FXCollections.observableArrayList());
26+
protected final ListProperty<ThreadElement> elements = new SimpleListProperty<>(FXCollections.observableArrayList());
2227
protected final IntegerProperty numberOfJNIRefs = new SimpleIntegerProperty(0);
2328

2429
public ObjectProperty<LocalDateTime> generationDateTimeProperty() { return generationDateTime; }
@@ -29,9 +34,9 @@ public class Dump {
2934
public String getDescription() { return description.get(); }
3035
public void setDescription(String description) { this.description.set(description); }
3136

32-
public ListProperty<ThreadInformation> elementsProperty() { return elements; }
33-
public ObservableList<ThreadInformation> getElements() { return elements.get(); }
34-
public void setElements(ObservableList<ThreadInformation> elements) { this.elements.set(elements); }
37+
public ListProperty<ThreadElement> elementsProperty() { return elements; }
38+
public ObservableList<ThreadElement> getElements() { return elements.get(); }
39+
public void setElements(ObservableList<ThreadElement> elements) { this.elements.set(elements); }
3540

3641
public IntegerProperty numberOfJNIRefsProperty() { return numberOfJNIRefs; }
3742
public int getNumberOfJNIRefs() { return numberOfJNIRefs.get(); }
@@ -66,7 +71,7 @@ public List<Text> asText() {
6671
final Font normal = Font.font("Helvetica", FontWeight.NORMAL, 12);
6772
final Font code = Font.font("Courier New", FontWeight.NORMAL, 12);
6873

69-
Text text = new Text("Generated at " + this.getGenerationDateTime().toString());
74+
Text text = new Text("Generated at " + DATE_TIME_FORMATTER_OUTPUT.format(this.getGenerationDateTime()));
7075
text.setFont(bold);
7176
texts.add(text);
7277

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.twasyl.jstackfx.beans;
2+
3+
import javafx.beans.property.ObjectProperty;
4+
import javafx.beans.property.SimpleObjectProperty;
5+
6+
import java.io.File;
7+
8+
/**
9+
* An implementation of {@link Dump} for thread dumps stored within files.
10+
*
11+
* @author Thierry Wasylczenko
12+
* @since JStackFX 1.0
13+
*/
14+
public class FileDump extends Dump {
15+
private ObjectProperty<File> file = new SimpleObjectProperty<>();
16+
17+
public ObjectProperty<File> fileProperty() { return file; }
18+
public File getFile() { return file.get(); }
19+
public void setFile(File file) { this.file.set(file); }
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.twasyl.jstackfx.beans;
2+
3+
import javafx.beans.property.ListProperty;
4+
import javafx.beans.property.SimpleListProperty;
5+
import javafx.collections.FXCollections;
6+
import javafx.collections.ObservableList;
7+
8+
/**
9+
* An implementation of {@link Dump} for thread dumps realized in memory and which results haven't been stored within
10+
* a file.
11+
*
12+
* @author Thierry Wasylczenko
13+
* @since JStackFX 1.0
14+
*/
15+
public class InMemoryDump extends Dump {
16+
private final ListProperty<String> lines = new SimpleListProperty<>(FXCollections.observableArrayList());
17+
18+
public ListProperty<String> linesProperty() { return lines; }
19+
public ObservableList<String> getLines() { return lines.get(); }
20+
public void setLines(ObservableList<String> lines) { this.lines.set(lines); }
21+
}

src/main/java/io/twasyl/jstackfx/beans/Pair.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.twasyl.jstackfx.beans;
22

33
/**
4+
* Simple class for associating two objects.
5+
*
46
* @author Thierry Wasylczenko
5-
* @since jStackFX @@NEXT-VERSION@@
7+
* @since JStackFX 1.0
68
*/
79
public class Pair<K, V> {
810
protected K value1;

0 commit comments

Comments
 (0)