0

How to send file with Java RabbitMQ? Especially using message converter.

I'm using Spring Framework, can send String or ArrayList but can't send File. I'm only use convertAndSend and convertAndReceive to send File but get :

org.springframework.amqp.AmqpIOException: java.io.FileNotFoundException

I don't know how to use message converter. The code from here and change some class :

HelloWorldHandler.java

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());
    }
}

ProducerConfiguration.java

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"));
        }
    }
}
  • Hello and welcome to StackOverflow. Please take some time to read the [help page](http://stackoverflow.com/help), especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – galath Jul 27 '15 at 08:19
  • Convert file byte data in base64 encoded string. Base64 encoding is standard to convert byte data to string and vice-versa, – Naveen Ramawat Jul 27 '15 at 08:20

1 Answers1

1

You can convert the file content into byte array and send the byte[] as below.

byte[] fileData = // get content from file as byte[]  [Refer Here][1]
String fileType  = // get file type from file

Message message = MessageBuilder.withBody(fileData).setHeader("ContentType", fileType).build();

rabbitTemplate.send("exchnage name", "routing key", message); 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Manmay
  • 835
  • 7
  • 11
  • Thanks for your answer, @manmay. But, is it possible for using MessageConverter? If yes, can you tell me the implementation, because I'm already googling about it but can't find the works one ^^ – Kamila Dini Nabilati Jul 28 '15 at 08:49
  • You can write your own converter by implementing the interface MessageConverter. Check http://docs.spring.io/spring-amqp/reference/html/amqp.html (Section : 3.6 Message Converters) – Manmay Jul 28 '15 at 14:27
  • if i use the code you give to send file, how I convert it from message to file in consumer side? thanks before, @manmay – Kamila Dini Nabilati Jul 30 '15 at 07:17
  • On the consumer side you will get the message body as byte[] then you can handle it as per your requirement. – Manmay Jul 30 '15 at 07:43
  • then, how can I know file type and file name in consumer side? – Kamila Dini Nabilati Jul 30 '15 at 08:25
  • I see then set the file type as header on the producer side while publishing the message. like : Header Name : fileType & Header Value : text . Updated the example in the answer above. – Manmay Jul 30 '15 at 09:10