ERLEDIGT
JA
JA
ANTWORTEN
5
5
ZUGRIFFE
514
514
EMPFEHLEN
-
Hallo!
Ich habe eine SingleFrameApplication wo ich einen TaskMonitor regestriere.
Wenn ich nun einen Task ausführe, so wird der PropertyChangeListener zwei mal aktiviert.
Also er kommt zwei mal in "started" und am Ende zweimal in "done".
Hier das Beispielprogramm:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
package swing; import org.jdesktop.application.Application; import org.jdesktop.application.FrameView; import org.jdesktop.application.SingleFrameApplication; import org.jdesktop.application.Task; import org.jdesktop.application.TaskMonitor; public class TaskMonitorTest extends SingleFrameApplication { /** * At startup create and show the main frame of the application. */ @Override protected void startup() { FrameView main = new MainFrame(this); show(main); } @Override protected void shutdown() { // The default shutdown saves session window state. super.shutdown(); // Now perform any other shutdown tasks you need. } /** * This method is to initialize the specified window by injecting resources. Windows shown in our application come * fully initialized from the GUI builder, so this additional configuration is not needed. */ @Override protected void configureWindow(java.awt.Window root) { } /** * Main method launching the application. */ public static void main(String[] args) { launch(TaskMonitorTest.class, args); } public class MainFrame extends FrameView { public MainFrame(Application arg0) { super(arg0); TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if ("started".equals(propertyName)) { System.out.println("started"); } else if ("done".equals(propertyName)) { System.out.println("done"); } else if ("message".equals(propertyName)) { String text = (String)(evt.getNewValue()); System.out.println("test: " + text); } else if ("progress".equals(propertyName)) { System.out.println("progress"); } } }); TestTask test = new TestTask(); System.out.println("Task wird gestartet"); getApplication().getContext().getTaskService().execute(test); } private class TestTask extends Task<Void,Void> { public TestTask() { super(Application.getInstance()); } @Override protected Void doInBackground() throws Exception { setMessage("Opening the file"); Thread.sleep(2000); System.out.println("im Task"); return null; } @Override protected void finished() { super.finished(); } } } }
Output:
Code :1 2 3 4 5 6 7
Task wird gestartet started started test: Opening the file im Task done done
-
01.12.11 14:13 #2
Schwer zu sagen, da ich nicht weiß, was "launch" macht.
Zum Prüfen könntest Du das Objekt und den aktuellen Thread (Thread.currentThread()) mit ausgeben, um zu erfahren, ob vielleicht zwei Deiner Objekte erzeugt werden.CU schnuffie
Fragliche Fragen stellende Fragensteller sind für verantwortungslose Antworten antwortender verantwortlicher Antworter selbst verantwortlich.
-
01.12.11 16:31 #3
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
wenn du den PropertyChangeListener an den TaskMonitor hängst bekommst du so wie es ausschaut die Property Change Events für den Monitor UND die einzelnen Tasks mit...
Ich denke was du eigentlich machen willst ist die Property Changes eines Tasks zu überwachen. Dazu könntest du einfach einen PropertyChangeListener am Task anstatt am TaskMonitor registieren. Dann bekommst du die State Changes wie gewünscht nur einmal mit.
schau mal hier:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
package de.tutorials.training; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.SwingUtilities; import org.jdesktop.application.Application; import org.jdesktop.application.FrameView; import org.jdesktop.application.SingleFrameApplication; import org.jdesktop.application.Task; import org.jdesktop.application.TaskMonitor; public class TaskMonitorTest extends SingleFrameApplication { /** * At startup create and show the main frame of the application. */ @Override protected void startup() { FrameView main = new MainFrame(this); show(main); } /** * Main method launching the application. */ public static void main(String[] args) { launch(TaskMonitorTest.class, args); } public class MainFrame extends FrameView { public MainFrame(Application app) { super(app); final TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); TestTask test = new TestTask(); test.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if ("started".equals(propertyName)) { System.out.println("started"); } else if ("done".equals(propertyName)) { System.out.println("done"); } else if ("message".equals(propertyName)) { String text = (String) evt.getNewValue(); System.out.println("test: " + text); } else if ("progress".equals(propertyName)) { System.out.println("progress"); } } }); System.out.println("Task wird gestartet"); getApplication().getContext().getTaskService().execute(test); } private class TestTask extends Task<Void, Void> { public TestTask() { super(Application.getInstance()); } @Override protected Void doInBackground() throws Exception { // setMessage("Opening the file"); Thread.sleep(2000); System.out.println("im Task"); return null; } } } }
Ausgabe:
Code :1 2 3 4 5 6 7 8
Task wird gestartet Dez 01, 2011 4:32:06 PM org.jdesktop.application.LocalStorage getId Warnung: unspecified resource Application.id using TaskMonitorTest Dez 01, 2011 4:32:06 PM org.jdesktop.application.LocalStorage getId Warnung: unspecified resource Application.vendorId using UnknownApplicationVendor started im Task done
Btw. mit Java 7 könntest du auch einfach switch mit Strings verwenden um deine if-Kaskade los zu werden:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
... test.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { switch (evt.getPropertyName()) { case "started": System.out.println("started"); break; case "done": System.out.println("done"); break; case "message": System.out.println("test: " + evt.getNewValue()); break; case "progress": System.out.println("progress"); break; default: /* ignore */ ; } } }); ...
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Kann das Beispiel gerade nicht testen, aber es wird schon der Grund sein warum es doppelt ausgegeben wird.
Aber was ich eigentlich wollte war: den TaskMonitor zu testen. Ich wollte wissen was das Ding macht und wie es funktioniert.
So wie ich es verstanden habe kann man der Applikation den TaskMonitor zuweisen und egal wo man in der Applikation dann per "getApplication().getContext().getTaskService().execute" einen Task aufruft, der TaskMonitor bekommt dies mit und gibt dann dementsprechende Sachen am Bildschirm aus.
Verstehe ich das so richtig?
-
01.12.11 17:57 #5
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
hast du dir schon die Doku unter: http://www.jarvana.com/jarvana/view/...skMonitor.html durchgelesen?
Scheinbar ist der TaskMonitor wirklich nur dazu da die aktuell laufenden Tasks und den Tasks im "Vordergrund" zu halten / zu unterscheiden. Der TaskMonitor ist IMHO für eine "direkte" Task Überwachung ohne etwas "unterbau" nicht gemacht...
...ich hab mir mal schnell selbst eine Task Visualisierung gebaut:
Vielleicht hilfts dir ja weiter...Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
package de.tutorials.training; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.concurrent.TimeUnit; import javax.swing.JComponent; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.Timer; import javax.swing.table.AbstractTableModel; import org.jdesktop.application.Application; import org.jdesktop.application.FrameView; import org.jdesktop.application.SingleFrameApplication; import org.jdesktop.application.Task; import org.jdesktop.application.TaskMonitor; public class TaskMonitorTest extends SingleFrameApplication { @Override protected void startup() { FrameView main = new MainFrame(this); show(main); } public static void main(String[] args) { launch(TaskMonitorTest.class, args); } public class MainFrame extends FrameView { private TaskVisualizer taskVisualizer; public MainFrame(Application app) { super(app); taskVisualizer = new TaskVisualizer(getContext().getTaskMonitor()); setComponent(taskVisualizer); Timer taskGenerator = new Timer(1000,new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(getContext().getTaskMonitor().getTasks().size() < 6){ //limit to max 6 task at once getContext().getTaskService().execute(new TestTask("Task_" + Integer.toString(System.identityHashCode(e),36))); } } }); taskGenerator.setCoalesce(true); taskGenerator.setRepeats(true); taskGenerator.start(); } private class TestTask extends Task<Void, Void> { public TestTask(String title) { super(Application.getInstance()); setTitle(title); } @Override protected Void doInBackground() throws Exception { for(int i = 1; i <= 3; i++){ setMessage("Step " + i); TimeUnit.MILLISECONDS.sleep((long) (Math.random() * 3000)); } return null; } @Override protected void succeeded(Void arg) { System.out.println("succeeded " + getTitle()); } } } public static class TaskVisualizer extends JComponent { private static final long serialVersionUID = 1L; private TaskMonitor taskMonitor; private JTable table; private PropertyChangeListener taskMonitorPropertyChangeListener; public TaskVisualizer(TaskMonitor taskMonitor) { attach(taskMonitor); createTaskVisualization(); } private void createTaskVisualization() { table = new JTable(new TaskTableModel(taskMonitor)); setLayout(new BorderLayout()); add(new JScrollPane(table), BorderLayout.CENTER); } private void attach(TaskMonitor taskMonitor) { this.taskMonitor = taskMonitor; this.taskMonitorPropertyChangeListener = new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { refresh(); } }; this.taskMonitor.addPropertyChangeListener(this.taskMonitorPropertyChangeListener); } private void refresh() { table.updateUI(); } } public static class TaskTableModel extends AbstractTableModel { private static final long serialVersionUID = 1L; private final TaskMonitor taskMonitor; private String[] columns = { "ID", "Title", "State", "Message" }; public TaskTableModel(TaskMonitor taskMonitor) { this.taskMonitor = taskMonitor; } @Override public int getRowCount() { return this.taskMonitor.getTasks().size(); } @Override public int getColumnCount() { return columns.length; } @Override public String getColumnName(int column) { return columns[column]; } @Override public Object getValueAt(int rowIndex, int columnIndex) { Task<?,?> task = this.taskMonitor.getTasks().get(rowIndex); Object value = null; switch (columns[columnIndex]) { case "ID": value = System.identityHashCode(task); break; case "Title": value = task.getTitle(); break; case "State": value = task.getState(); break; case "Message": value = task.getMessage(); break; default: throw new UnsupportedOperationException("XXX"); } return value; } } }
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Finde es immer wieder beeindruckend wie du schnell mal was zusammen bastelst

Ja, das hilft mir auf alle Fälle weiter. Vielen Dank
Ähnliche Themen
-
Verhindern, dass ein Freund zweimal hinzugefügt wird...
Von maxikey im Forum PHPAntworten: 11Letzter Beitrag: 16.05.08, 08:48 -
Warum wird die Funktion zweimal aufgerufen?
Von Mammuth im Forum Flash PlattformAntworten: 3Letzter Beitrag: 14.10.07, 00:06 -
EventHandler wird zweimal abgearbeitet
Von Kona im Forum .NET Web und KommunikationAntworten: 0Letzter Beitrag: 02.07.07, 14:56 -
JList - Listener-Ereignis wird zweimal ausgeführt
Von Ein_Freund im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 2Letzter Beitrag: 19.04.05, 11:52 -
Datensatz wird zweimal angezeigt
Von arrayhunter im Forum PHPAntworten: 4Letzter Beitrag: 08.02.05, 17:52





Zitieren

Login





