Announcement Announcement Module
Collapse
No announcement yet.
Problems when sending a message while receiving using RabbitMQ / RabbitTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems when sending a message while receiving using RabbitMQ / RabbitTemplate

    HI there i am trying to send a message in the handleMessage method,
    this seems to fail somehow. Is there a reason why i cannot send a
    message to rabbitMQ when processing/receiving a message?

    Code:
    // receives a message and replies to the reply queue
    void handleMessage(Message message) {
    
        // send a message to the exchange and waits for the reply..
        String result =
    rabbitTemplate.convertSendAndReceive("myCustomExch", "myRoutingKey",
    "test content")
    
        // determine the reply queue
        def destQueue = message.messageProperties.replyTo
    
        // code omitted that processes the message and uses the previously result
        ....
    
        // send the content back to the reply queue
        rabbitSend destQueue, responseJson
    }
    When i put the rabbitTemplate.convertSendAndReceive("myCustomExch ",
    "myRoutingKey", "test content") method in a non handleMessage method
    it works without problems. Am i not allowed to send and receive inside
    a handleMessage method that is also handling stuf?

    /Marco
    Last edited by marcopas; Feb 22nd, 2013, 08:43 AM.

  • #2
    When posting code and config; please use [ code ] ... [ /code ] tags (no spaces in brackets).

    Can you explain what you mean by "fail somehow"? Turn on DEBUG logging and take a look at the logs.

    Can you show the Spring config for the container, template, connection factory etc?

    Comment


    • #3
      The fail consists on not delivering the message to the rabbitmq container.

      When receiving the message in:
      Code:
      void handleMessage(Message message) {
      
      // send a message to the exchange and waits for the reply..
          String result =
      rabbitTemplate.convertSendAndReceive("myCustomExch", "myRoutingKey",
      "test content")
      It waits on the convertSendAndReceive method for 5 seconds (default timeout) and the consumer never gets the message. The reply is null! Moving this line to a Grails controller for example then the method is executed without any problems. The consumer gets the message and a reply is received.

      So my guess is that i am not allowed to make a call to a convertAndSend or convertSendAndReceive when i am already inside a handleMessage method.

      The config used in the grails project is al follows..

      Code:
          queues = {
              exchange name: 'defaultExch', type: direct, durable: true, {
                  defaultQueue durable: true, binding: "myDefaultRoutingKey"
              }
      
              exchange name: 'myCustomExch', type: direct, durable: true, {
                  myQueue durable: true, binding: "myRoutingKey"
              }
          }
      So to elaborate a bit more..

      A message arrives on the 'defaultQueue' the handleMessage method is being called,
      inside the handleMessage method i try to send a message to another Queue but then this call actually seems to get
      a timeout. Both handleMessage methods are called by rabbitTemplate.convertSendAndReceive

      Code:
      rabbitTemplate.convertSendAndReceive("defaultExch", "myDefaultRoutingKey", "test content")
      Code:
      rabbitTemplate.convertSendAndReceive("myCustomExch", "myRoutingKey", "test content")
      Last edited by marcopas; Feb 22nd, 2013, 08:58 AM.

      Comment


      • #4
        Like I said, I need to see the configuration and you should look at the logs.

        That said, my best guess is your listener container is transactional; this means the send won't complete until the transaction commits (when the handler exits). This is because, by default, the rabbit template will use the same channel as the listener thread.

        I am not familiar with grails, but if you can disable transactions, that should solve it. Another alternative is the listener container has a property "exposeListenerChannel" which defaults to true; setting it to false will mean the template will get its own channel. However, this only works if an external transaction manager is NOT configured.

        Comment

        Working...
        X