Sending message with JMS hangs on exit

2020-04-11 05:02发布

I am trying to create a Java Application Client project that sends a JMS message to a queue on a Glassfish server.

The problem is that after the app sends the message, it hangs when it's supposed to exit. The message is transmitted successfully, but for some reason the app doesn't exit. I have tried to debug the application, and I can step trough it all the way to the end of static void main, and that's where it hangs.

Here is the code:

import javax.jms.*;
import javax.naming.InitialContext;


public class Main {

public void SendMessage() throws Exception {
    InitialContext ctx = new InitialContext();
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/TestFactory");
    Queue queue = (Queue)ctx.lookup("jms/TestQueue");
    Connection conn = cf.createConnection();
    Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer prod = s.createProducer(queue);

    TextMessage txt = s.createTextMessage("testing");
    prod.send(txt);

    prod.close();
    s.close();
    conn.close();
}

public static void main(String[] args) throws Exception {
    Main m = new Main();
    m.SendMessage();
}

public Main() {
    super();
}

}

How can I make it stop hanging?

2条回答
Ridiculous、
2楼-- · 2020-04-11 05:37

Good call on the thread dump. Try issuing a Conn.stop(). It seems the JMS client still has non daemon threads running

查看更多
干净又极端
3楼-- · 2020-04-11 05:50

It's been a bug in Glassfish for a long time.

There is a bug recorded here (reported back in version 9 of Sun App Server, predating Glassfish), but I suspect there will be lots of duplicate reports:

http://java.net/jira/browse/GLASSFISH-1429

My only known fix is to System.exit(0) (in a finally block), which closes all threads.

Horrible, yes.

查看更多
登录 后发表回答