如何用Java RabbitMQ发送和接收文件? [英] How to send and receive file with Java RabbitMQ?

查看:0
本文介绍了如何用Java RabbitMQ发送和接收文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我使用的是Spring框架,可以发送字符串或数组列表,但不能发送文件。我只使用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"));
        }
    }
}

推荐答案

您可以将文件内容转换为字节数组并发送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); 

这篇关于如何用Java RabbitMQ发送和接收文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆