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

How to send and receive file with Java RabbitMQ?

如何使用 Java RabbitMQ 发送文件? 特别是使用消息转换器。

我正在使用 Spring 框架,可以发送 String 或 ArrayList 但不能发送 File。我只使用 convertAndSendconvertAndReceive 发送文件但得到:

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

我不知道如何使用消息转换器。来自 here 的代码并更改一些 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"));
        }
    }
}

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

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