如何使用Java RabbitMQ发送和接收文件?

时间:2015-07-27 08:16:15

标签: java file rabbitmq messaging

如何使用Java RabbitMQ发送文件? 尤其是使用消息转换器

我正在使用Spring Framework,可以发送String或ArrayList但不能发送File。我只使用convertAndSendconvertAndReceive发送文件,但获取:

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

我不知道如何使用消息转换器。来自here的代码并更改了一些类:

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

1 个答案:

答案 0 :(得分:1)

您可以将文件内容转换为字节数组,并发送如下字节[]。

byte [] fileData = //从文件中获取内容为byte [] Refer Here String fileType = //从文件中获取文件类型

消息消息= MessageBuilder.withBody(fileData).setHeader(" ContentType",fileType).build();

rabbitTemplate.send(" exchnage name"," routing key",message);