title: How to send and receive file with Java RabbitMQ?
body: <p>How to send file with Java RabbitMQ?
  Especially using message converter. </p>
  
  <p>I'm using Spring Framework, can send String or ArrayList but can't send File. I'm only use <code>convertAndSend</code> and <code>convertAndReceive</code> to send File but get : </p>
  
  <pre><code>org.springframework.amqp.AmqpIOException: java.io.FileNotFoundException
  </code></pre>
  
  <p>I don't know how to use message converter. The code from <a href="https://github.com/spring-projects/spring-amqp-samples/tree/master/helloworld/src/main/java/org/springframework/amqp/helloworld/async" rel="nofollow">here</a> and change some class :</p>
  
  <p>HelloWorldHandler.java</p>
  
  <pre><code>package org.springframework.amqp.helloworld.async;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.ArrayList;
  
  import org.springframework.amqp.core.Message;
  
  public class HelloWorldHandler {
  
      public void handleMessage(File message) throws IOException {
          BufferedReader br = new BufferedReader(new FileReader(message));
          System.out.println(br.readLine());
      }
  }
  </code></pre>
  
  <p>ProducerConfiguration.java</p>
  
  <pre><code>package org.springframework.amqp.helloworld.async;
  
  import java.io.File;
  import java.util.concurrent.atomic.AtomicInteger;
  
  import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  import org.springframework.amqp.rabbit.core.RabbitTemplate;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.config.BeanPostProcessor;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.scheduling.annotation.Scheduled;
  import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
  
  @Configuration
  public class ProducerConfiguration {
  
      protected final String helloWorldQueueName = "hello.world.queue";
  
      @Bean
      public RabbitTemplate rabbitTemplate() {
          RabbitTemplate template = new RabbitTemplate(connectionFactory());
          template.setRoutingKey(this.helloWorldQueueName);
          return template;
      }
  
      @Bean
      public ConnectionFactory connectionFactory() {
          CachingConnectionFactory connectionFactory = new CachingConnectionFactory("x.x.x.x");
          connectionFactory.setUsername("username");
          connectionFactory.setPassword("password");
          return connectionFactory;
      }
  
      @Bean
      public ScheduledProducer scheduledProducer() {
          return new ScheduledProducer();
      }
  
      @Bean
      public BeanPostProcessor postProcessor() {
          return new ScheduledAnnotationBeanPostProcessor();
      }
  
  
      static class ScheduledProducer {
  
          @Autowired
          private volatile RabbitTemplate rabbitTemplate;
  
          private final AtomicInteger counter = new AtomicInteger();
  
          @Scheduled(fixedRate = 3000)
          public void sendMessage() {
              rabbitTemplate.convertAndSend(new File("test.txt"));
          }
      }
  }
  </code></pre>