Adam@Work
Location : Canada
Member since : Mar 3, 2004
Messages : 1
|
Mar 3, 2004 at 12:37 AM
According to Swing rules, all drawing events must take place on the AWT Event thread, in order to be thread safe. However, PJA toolkit either doesn't use the AWT Event thread, or uses it incorrectly, because when I use SwingUtilities.invokeAndWait to draw my custom Swing component from a servlet running on a headless server (tomcat3.2.3 on HPUX) using PJA, my servlet simply hangs.
I'm trying to run the following Runnable using SwingUtilities.invokeAndWait (from the doGet method of my servlet), and it doesn't run.
Any idea what I'm doing wrong? Or, is it a PJA shortcoming?
private class SwingRunner implements Runnable
{
private Exception exception = null;
private ByteArrayOutputStream baos = null;
private String contextPath = null;
public SwingRunner(String contextPath,
ByteArrayOutputStream baos)
{
this.contextPath = contextPath;
this.baos = baos;
}
public Exception getException() {
return exception;
}
public void run()
{
final String text = "Hello World from " + contextPath;
//Test whether we can create swing lightweight component
JLabel label = new JLabel(text);
label.setOpaque(true);
label.setBackground(Color.cyan);
label.setForeground(Color.red);
Font font = label.getFont();
label.setFont(font.deriveFont(25f));
//Test whether we can get preffered size from swing
//lightweight component
Dimension size = label.getPreferredSize();
int width = size.width;
int height = size.height;
label.setSize(width, height);
// Create image
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// Get drawing context
// If this line blows up, your headless environment has a problem
Graphics g = image.getGraphics();
//Gotta set the clip, or else paint may throw an exception
g.setClip(0, 0, width, height);
label.paint(g);
// Dispose context
g.dispose();
//Encode swing component
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
try {
encoder.encode(image);
baos.flush();
baos.close();
}
catch (IOException e) {
exception = e;
}
}
}
Once hung, a SIGQUIT yields the following two interesting thread stacks:
"Thread-10" prio=10 tid=0x003b9270 nid=27 lwp_id=683 waiting on monitor [0x7462c000..0x7462c4b8]
at java.lang.Object.wait(Native Method)
- waiting on <0x7be53568> (a java.awt.EventQueue$1$AWTInvocationLock)
at java.lang.Object.wait(Unknown Source)
at java.awt.EventQueue.invokeAndWait(EventQueue.java:542)
- locked <0x7be53568> (a java.awt.EventQueue$1$AWTInvocationLock)
at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1141)
at demos.common.SwingTestServlet.doGet(SwingTestServlet.java:66)
AND
"AWT-EventQueue-0" prio=10 tid=0x000fe1c0 nid=40 lwp_id=696 waiting on monitor [0x73f65000..0x73f654b8]
at java.lang.Object.wait(Native Method)
- waiting on <0x7be53350> (a com.eteks.awt.PJAToolkit$PJAEventQueue)
at java.lang.Object.wait(Unknown Source)
at java.awt.EventQueue.getNextEvent(EventQueue.java:255)
- locked <0x7be53350> (a com.eteks.awt.PJAToolkit$PJAEventQueue)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:88)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:80)
|
Manu
Location : Paris / France
Member since : Apr 29, 2003
Messages : 394
|
Mar 11, 2004 at 7:15 AM
Event queue and then SwingUtilities invoke support un PJA Toolkit is minimal : if you get a close look at com.eteks.awt.PJAToolkit, you'll see that getSystemEventQueueImpl method simply returns a dummy event queue (this method is called by getSystemEventQueue AWT Toolkit to get an event queue)
Sorry, but I wouldn't be astonished that it's not enough to use SwingUtilities invoke methods...
If you look for a workaround, try to use simply java.lang.Thread class. --- Manu (moderator/modérateur)
|