Announcement Announcement Module
Collapse
No announcement yet.
Why is System.exit() required for use of AmqpTemplate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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).

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment

        Working...
        X