title: RabbitMQ direct reply to. I'm getting the AlreadyClosedException
body: <p>SOLVED: Move the </p>
  
  <blockquote>
    <p>channel.basicPublish("", QUEUE, props, message.getBytes());</p>
  </blockquote>
  
  <p>below</p>
  
  <blockquote>
    <p>channel.basicConsume(replyQueue, ...)</p>
  </blockquote>
  
  <p>That fixed the issue.</p>
  
  <hr>
  
  <p>I'm trying to figure out how to use the RabbitMQ direct reply-to feature. Since the <a href="https://www.rabbitmq.com/direct-reply-to.html" rel="nofollow noreferrer">documentation</a> is rather vague on how to implement it, I tried to use the <a href="https://www.rabbitmq.com/tutorials/tutorial-six-java.html" rel="nofollow noreferrer">RPC</a> example, adopting it to use direct reply-to instead.</p>
  
  <pre><code>private final static String QUEUE = "Test_chan";
  private void directReplyToClient(ConnectionFactory factory) {
      Connection connection = null;
      Channel channel = null;
      String replyQueue;
  
      try {
          connection = factory.newConnection();
          channel = connection.createChannel();
  
          //replyQueue = channel.queueDeclare().getQueue();
          replyQueue = "amq.rabbitmq.reply-to";
          AMQP.BasicProperties props = new AMQP.BasicProperties
                  .Builder()
                  .replyTo(replyQueue)
                  .build();
          String message = "Hello World";
          channel.basicPublish("", QUEUE, props, message.getBytes());
  
          final BlockingQueue&lt;String&gt; response = new ArrayBlockingQueue&lt;&gt;(1);
  
          channel.basicConsume(replyQueue, true, new DefaultConsumer(channel) {
              @Override
              public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws UnsupportedEncodingException {
  
                  response.offer(new String(body, "UTF-8"));
  
              }
          });
  
          System.out.println(response.take());
  
      } catch (IOException | TimeoutException | InterruptedException e) {
          e.printStackTrace();
      } finally {
          try {
              if (channel != null)
                  channel.close();
              if (connection != null)
                  connection.close();
          } catch (IOException | TimeoutException _ignore) {}
      }
  }
  </code></pre>
  
  <p>Setting reply address to </p>
  
  <blockquote>
    <p>channel.queueDeclare().getQueue()</p>
  </blockquote>
  
  <p>works, but setting it to </p>
  
  <blockquote>
    <p>amq.rabbitmq.reply-to</p>
  </blockquote>
  
  <p>gives the following exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" com.rabbitmq.client.AlreadyClosedException:
    channel is already closed due to channel error; protocol method:
    method(reply-code=406, reply-text=PRECONDITION_FAILED - fast reply consumer does not exist, class-id=60, method-id=40)</p>
  </blockquote>
  
  <p>Does anyone see where I'm doing something wrong? Any pointers will be appreciated.</p>