Results 1 to 4 of 4

Thread: Why is System.exit() required for use of AmqpTemplate?

  1. #1
    Join Date
    Nov 2008
    Posts
    23

    Default Why is System.exit() required for use of AmqpTemplate?

    Hello All,

    I'm looking at a very simple example sending a message using AmqpTemplate#convertAndSend. I don't understand why the program won't terminate w/o an explicit System.exit(0).

    Is it because it's waiting on an ACK?

    Can someone please explain?

    Thanks!

    --john

    @Grapes([
    @Grab(group='org.springframework.amqp', module='spring-rabbit', version='1.1.3.RELEASE')
    ])

    public class SendAmqp {
    public static void main(String[] argv) throws Exception {
    ConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.host = "localhost"

    AmqpAdmin admin = new RabbitAdmin(connectionFactory);
    admin.declareQueue(new Queue("myqueue"));

    AmqpTemplate template = new RabbitTemplate(connectionFactory);
    template.convertAndSend("myqueue", "foo")

    //without this, program won't exit
    System.exit(0)
    }
    }

  2. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,036

    Default

    Well, the CachingConnectionFactory, er, caches the connection; the connection has a non-daemon thread.

    Change

    Code:
    ConnectionFactory connectionFactory ...
    to

    Code:
    CachingConnectionFactory connectionFactory ...
    and add

    Code:
    connectionFactory.destroy();

    BTW, when posting, always surround code/config with [ code ] ... [ / code ] (no spaces inside brackets).
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  3. #3
    Join Date
    Nov 2008
    Posts
    23

    Default

    Thanks for the prompt response Gary. Given that the CachingConnectionFactory is the reason, I'm surprised that the following terminates w/o the System.exit nor the connectionFactory.destroy()

    Code:
    public class SendAmqp {
    	public static void main(String[] argv) throws Exception {
    		CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    		connectionFactory.host = "localhost"
    	}
    }
    --john

  4. #4
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,036

    Default

    Because in that case you never use the factory, so the connection is not opened/cached.

    As soon as you declare the queue and/or send a message, the connection is opened and cached; the rabbit library maintains a thread for each connection to handle asynchronous incoming data.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •