You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@activemq.apache.org by "Justin Lebar (JIRA)" <ji...@apache.org> on 2008/08/05 06:31:52 UTC

[jira] Created: (AMQ-1875) Transport threads spawned from daemon threads should themselves be daemon threads

Transport threads spawned from daemon threads should themselves be daemon threads
---------------------------------------------------------------------------------

                 Key: AMQ-1875
                 URL: https://issues.apache.org/activemq/browse/AMQ-1875
             Project: ActiveMQ
          Issue Type: Bug
          Components: Transport
    Affects Versions: 5.1.0
         Environment: Windows XP, JDK 1.6.0r6
            Reporter: Justin Lebar


When a thread opens a connection over TCP, ActiveMQ spawns a transport thread.  The transport thread doesn't die until the connection is closed.  That's fine if the connection was opened by a non-daemon thread.  However, if the connection was opened by a daemon thread, the transport thread spawned is still set as a non-daemon thread.  If the daemon thread keeps its connection open, the process never dies, because a non-daemon is still alive.

I think AMQ transport threads should inherit their daemon-ness from their parent thread, and they should be responsible for cleaning themselves up when the application quits, if necessary.

Below is some code which exhibits the described behavior.  Run it and examine it with jstack or put a breakpoint right after the connection.start() to see that the transport thread is non-daemon.

{code}
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;

public class DaemonThreadTest {
   public static void main(String[] args) throws Exception {
       {
           Thread t = new Thread(new Runnable() {
               public void run() {
                   try {
                       ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "", "", "tcp://localhost:61616"));
                       Connection connection = connectionFactory.createConnection();
                       connection.start();

                       while (true) {
                            // Do some stuff, expecting the JVM to kill us when we're done.
                       }

                       // Unreached
                       connection.close();

                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           });

           t.setDaemon(true);
           t.start();

           // An infinite wait because I'm lazy.  You could replace this with a sleep, so long as you wait
           // long enough for the daemon to initialize its connection.
           Object lock = new Object();
           synchronized(lock) {
               lock.wait();
           }
       }
   }
}
{code}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (AMQ-1875) Transport threads spawned from daemon threads should themselves be daemon threads

Posted by "Gary Tully (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/activemq/browse/AMQ-1875?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=52734#action_52734 ] 

Gary Tully commented on AMQ-1875:
---------------------------------

you can achieve what you want though config by adding daemon=true as a query parameter to the brokerUrl passed to your ActiveMQConnectionFactory

{code}tcp://localhost:61616?daemon=true{code}

> Transport threads spawned from daemon threads should themselves be daemon threads
> ---------------------------------------------------------------------------------
>
>                 Key: AMQ-1875
>                 URL: https://issues.apache.org/activemq/browse/AMQ-1875
>             Project: ActiveMQ
>          Issue Type: Bug
>          Components: Transport
>    Affects Versions: 5.1.0
>         Environment: Windows XP, JDK 1.6.0r6
>            Reporter: Justin Lebar
>             Fix For: 5.4.0
>
>
> When a thread opens a connection over TCP, ActiveMQ spawns a transport thread.  The transport thread doesn't die until the connection is closed.  That's fine if the connection was opened by a non-daemon thread.  However, if the connection was opened by a daemon thread, the transport thread spawned is still set as a non-daemon thread.  If the daemon thread keeps its connection open, the process never dies, because a non-daemon is still alive.
> I think AMQ transport threads should inherit their daemon-ness from their parent thread, and they should be responsible for cleaning themselves up when the application quits, if necessary.
> Below is some code which exhibits the described behavior.  Run it and examine it with jstack or put a breakpoint right after the connection.start() to see that the transport thread is non-daemon.
> {code}
> import javax.jms.*;
> import org.apache.activemq.ActiveMQConnectionFactory;
> import org.apache.activemq.pool.PooledConnectionFactory;
> public class DaemonThreadTest {
>    public static void main(String[] args) throws Exception {
>        {
>            Thread t = new Thread(new Runnable() {
>                public void run() {
>                    try {
>                        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "", "", "tcp://localhost:61616"));
>                        Connection connection = connectionFactory.createConnection();
>                        connection.start();
>                        while (true) {
>                             // Do some stuff, expecting the JVM to kill us when we're done.
>                        }
>                        // Unreached
>                        connection.close();
>                    } catch (Exception e) {
>                        e.printStackTrace();
>                    }
>                }
>            });
>            t.setDaemon(true);
>            t.start();
>            // An infinite wait because I'm lazy.  You could replace this with a sleep, so long as you wait
>            // long enough for the daemon to initialize its connection.
>            Object lock = new Object();
>            synchronized(lock) {
>                lock.wait();
>            }
>        }
>    }
> }
> {code}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.