I write a simple program by ActiveMQ as following:
public static void main(String[] args) throws Throwable
{
final ActiveMQConnectionFactory conFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
final QueueConnection connection = conFactory.createQueueConnection();
final Session session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = new ActiveMQQueue("MJ_SAF");
final MessageProducer producer = session.createProducer(destination);
Message message = session.createTextMessage("test");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 20);
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1);
message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
producer.send(message);
}
when I executing this program, message send for ActiveMQ broker properly but my program don’t exit and stay running .
When I add connection.close(); statement to end of above program, my program complete and exit properly.
My question is: What is reason of this behavior?
As long as there are any connections open, the (AMQ-)assigned threads that handle reading and writing messages from/to these connections keep the VM running (these are no daemon threads).
You can see the threads when debugging your program.