How can I send attachments as part of a Camel message exchange using producerTemplate

32 Views Asked by At

I send some binary files to a rest endpoint using a Multipart request.
Then I send the attachments in a message to Kafka using
exchange.getMessage(AttachmentMessage.class).addAttachment(multiPartFile.getName(), new DataHandler(new ByteArrayDataSource(multiPartFile.getBytes(), multiPartFile.getContentType())) :

producerTemplate.send("direct:notification", exchange -> {
                    AttachmentMessage attchmtMsg = //exchange.getIn(AttachmentMessage.class);
                            exchange.getMessage(AttachmentMessage.class);
                    attchmtMsg.setBody(emlNotfcnDto, MyDomainPojoDto.class);

                    for (MultipartFile file : attachments) {
                        attchmtMsg.addAttachment(file.getName(), new DataHandler(
                                new ByteArrayDataSource(file.getBytes(), file.getContentType())
                            )
                        );
                    }
            }
        );

Note: I'm not sending a fully formed email message to Kafka. The Email is created at the consuming side, and any attachments are to be added.

I'm using Camel to send to Kafka (a direct, named route, that sends to the kafka topic on my configured broker).

However, Camel doesn't seem to send the attachment through to Kafka, nor do I see it in the Kafka UI.
I see the DTO serialised as Json under the message > value tab. And the header I've set. But can't seem to find the attachment.
And consuming it on the consumer route processor also fails:

AttachmentMessage attchmtMsg = exchange.getIn(AttachmentMessage.class);
Map<String, DataHandler> msgAttchmts = attchmtMsg.getAttachments();
...
if (msgAttchmts !=null) {
                for (Map.Entry<String, DataHandler> entry : msgAttchmts .entrySet()) {
                    mimeMsgHlpr.addAttachment(entry.getKey(), entry.getValue().getDataSource());
                }
            }

Wherein, msgAttchmts is always received as null.

What am I missing?

I found this question: ProducerTemplate. Camel. How to add attachment where the OP is trying something similar, but the solution (https://stackoverflow.com/a/69677185/434145) doesn't work.
The Message API does not sport an addAttachment method.

0

There are 0 best solutions below