Announcement Announcement Module
Collapse
No announcement yet.
What's the relationship between RabbitTemplate & instances of Publisher(or channel)? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What's the relationship between RabbitTemplate & instances of Publisher(or channel)?

    I am integrating Spring-amqp into my email-app.

    In my amqp config xml-file, I just declare a rabbitTemplate like this:
    Code:
     <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter"
          mandatory="true" exchange="#{prop['mq.exchange']}" />
    and in a RESTful service that exposed to the outside, i just inject this rabbitTemplate and use it for message publishing:
    Code:
    @Service("messageProducer")
    public class DefaultMessageProducer implements MessageProducer {
    
        private static final Logger logger = LoggerFactory.getLogger(DefaultMessageProducer.class);
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Override
        public void produce(EmailForm emailForm) {
            ... //generating routingKey, and so on
            this.rabbitTemplate.convertAndSend(routingKey, emailForm);
            ...
        }
    }
    Code:
    @Controller
    @RequestMapping("email")
    public class EmailRest extends AbstractController {
    
        private static final Logger logger = LoggerFactory.getLogger(EmailRest.class);
    
        @Autowired
        private MessageProducer messageProducer;
    
        @RequestMapping(method = RequestMethod.POST, value = "send")
        public HttpEntity<?> sendEmail(@Valid @RequestBody EmailForm emailForm)  {
            messageProducer.produce(emailForm);
            return new ResponseEntity<Integer>(1, HttpStatus.OK);
        }
    
    }
    this is work fine for me. after emial-app is startup, there is only one channel for message publishing as my expected. But the question is, After a period of time(probably a couple of days), i noticed that channel for message-publishing is more than one, become 2 or 3.

    Some points that i think:
    1 rabbitTemplate is singleton ,right?
    2 instances of channels is equivalent to instances of publisher, right?

    I'm worry about after a long time, the instances of channel for publishing is become more and more. So i want to know, why the instance of channel is more that one?

    Any response is greatly appreciated.

    Regards.

  • #2
    Have you increased your channelCacheSize in the CachingConnectionFactory?

    When your controller gets 2 (or more) concurrent requests, the factory will hand out a Channel for each.

    The channelCacheSize defaults to 1, which means any "extra" channels will be closed. Obviously, it makes sense to set the cache size so you have enough cached channels to handle your expected work load.

    Comment


    • #3
      Thanks for reply, Gary.

      Yes, i increased the channel-cache-size to 4 since i need some consumers that also need channel instance separately.

      I dig out a post that you posted on another thread:
      http://forum.springsource.org/showth...-configuration #7:
      For the RabbitTemplate, channels are retrieved from the cache or created as necessary. If three threads are calling the template concurrently, three channels will be used.
      Coupled with your interpretation of the above:
      When your controller gets 2 (or more) concurrent requests, the factory will hand out a Channel for each.
      I think my question was answerd prefect.

      Thanks again, Gary.

      Comment

      Working...
      X