title: How to trigger the Block and Unblock events in RabbitMQ?
body: <p>I want to know when the RabbitMQ blocks or unblocks a connection so i
  added the following to my application:-</p>
  <p>1- added the blocked listener to the RabbitMQ connection and implemented the <strong>handleUnblocked()</strong> and <strong>handleBlocked()</strong> methods.</p>
  <p>2- i tried to make the brocker low on resources by sending 100k messages each of size 1MB.</p>
  <p>3- added a delay inside the consumer so that the messages can't be consumed fast.</p>
  <p>But still the <strong>handleBlocked()</strong> not called.</p>
  <p>Here is my code:-</p>
  <p><strong>Producer</strong></p>
  <pre><code>@Component
  class Producer {
      private RabbitTemplate template;
  
      @Autowired
      public Producer(RabbitTemplate template) {
          this.template = template;
      }
  
      public void sendMessage(String exchange, String routingKey, String message) {
          this.template.convertAndSend(exchange, routingKey, message);
      }
  }
  </code></pre>
  <p><strong>Consumer</strong></p>
  <pre><code>@Component
  class Consumer {
      
      @RabbitListener(queues = &quot;${apress.amqp.queue}&quot;)
      public void process(String message) throws InterruptedException {
          
          Thread.sleep(5000L);
      }
  }
  </code></pre>
  <p><strong>Configuration</strong></p>
  <pre><code>
  @Configuration
  class AMQPConfig {
      
      @Bean
      public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
          
          RabbitTemplate template = new RabbitTemplate(connectionFactory);
          
          template.execute(new ChannelCallback&lt;Object&gt;() {
  
              @Override
              public Object doInRabbit(Channel channel) throws Exception {
  
                  channel.getConnection().addBlockedListener(new BlockedListener() {
                      
                      
                      public void handleUnblocked() throws IOException {
                          
                          System.out.println(&quot;UnBlocked&quot;);
                      }
  
                      public void handleBlocked(String reason) throws IOException {
                          
                          System.out.println(&quot;Blocked&quot;);
                      }
                  });
                  
                  //when u stop the rabbitMQ service, this event will be triggered.
                  channel.getConnection().addShutdownListener(new ShutdownListener() {
                      public void shutdownCompleted(ShutdownSignalException cause) {
                          
                          System.out.println(&quot;Rabbit Shutdown&quot;);
                      }
                  });
  
                  return null;
              }
  
          });
          
          return template;
      }
      
  
      //this bean is used to create the queue programmatically..
      @Bean
      public Queue queue(@Value(&quot;${apress.amqp.queue}&quot;) String queueName) {
          return new Queue(queueName, false);
      }
      
  }
  </code></pre>
  <p><strong>Application Main</strong></p>
  <pre><code>@SpringBootApplication
  public class AmqpDemoApplication {
      public static void main(String[] args) {
          SpringApplication.run(AmqpDemoApplication.class, args);
      }
  
      @Bean
      CommandLineRunner simple(@Value(&quot;${apress.amqp.exchange:}&quot;) String exchange,
              @Value(&quot;${apress.amqp.queue}&quot;) String routingKey, Producer producer) {
          return args -&gt; {
              for(int i = 0; i &lt; 100000; i++) {
                  //send a big size messages to consume the memory and 
                  //to force the rabbitMQ to make queue enters the &quot;flow&quot; state
                  String messageOfSize1MB = ExamplesUtil.createDataSize(1024);
                  producer.sendMessage(exchange, routingKey, messageOfSize1MB);
              }
          };
      }
  }
  </code></pre>
  <p><strong>Util class</strong></p>
  <pre><code>class ExamplesUtil {
  
      /**
       * Creates a message of size @msgSize in KB.
       */
      public static String createDataSize(int msgSize) {
          // Java chars are 2 bytes
          msgSize = msgSize / 2;
          msgSize = msgSize * 1024;
          StringBuilder sb = new StringBuilder(msgSize);
          for (int i = 0; i &lt; msgSize; i++) {
              sb.append('a');
          }
          return sb.toString();
      }
  }
  </code></pre>
  <p><strong>application.properties</strong></p>
  <pre><code>apress.amqp.queue=spring-boot-queue
  spring.rabbitmq.host=localhost
  spring.rabbitmq.port=5672
  spring.rabbitmq.username=guest
  spring.rabbitmq.password=guest
  spring.rabbitmq.virtual-host=/
  </code></pre>
  <p><strong>pom.xml</strong></p>
  <pre><code>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;org.javaworld&lt;/groupId&gt;
      &lt;artifactId&gt;code-snippet&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.5.2&lt;/version&gt;
          &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;   
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-amqp&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>