0

I have successfully configured via Java an Inbound Channel Adapter for AWS SQS using a direct channel. This project uses a combination of JDBC and RabbitMQ with SQS as the inbound flow for 3 separate queues. I need durable messages so I'm trying to figure out how to leverage RabbitMQ. I'm confused with how to reference the AMQP channel. How do I reference the AMQP message channel from the setOutPutChannel? My goal is to only remove the message out of SQS if message is successfully published to RabbitMQ durable queue.

@Bean
public MessageProducer getSQSChannel() {
    SqsMessageDrivenChannelAdapter adapter = new SqsMessageDrivenChannelAdapter(this.amazonSqs, MY_SQS_QUEUE);
    adapter.setOutputChannel(????);  
    return adapter;
}

@Bean
public AmqpChannelFactoryBean messageDriven(ConnectionFactory connectionFactory) {
    AmqpChannelFactoryBean factoryBean = new AmqpChannelFactoryBean(true);
    factoryBean.setConnectionFactory(connectionFactory);
    factoryBean.setQueueName("bar");
    factoryBean.setPubSub(false);
    return factoryBean;
}
Jim Hankins
  • 1,055
  • 1
  • 11
  • 27

1 Answers1

1

Use

adapter.setOutputChannelName("messageDriven");

and the channel created by the factory bean will be resolved from its name at runtime.

EDIT

Or , as Artem said...

MessageProducer getSQSChannel(MessageChannel messageDriven) { 
    ... 
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • 1
    You also can do method argument injection `MessageProducer getSQSChannel(MessageChannel messageDriven) { ... }` – Artem Bilan Jan 12 '19 at 16:06
  • Thank you! To confirm my understanding. If I have an integration flow that, for example polls for SQS messages, transforms then sends out to RabbitMQ with a DirectChannel as the incoming channel, there are scenarios whereby a message can be lost? SQS>>>InboundChannelAdapter>>>DirectChannel>>ObJTransformer>>PayloadEnricher>>RabbitOutboundAdapter In my testing of doing things like killing Rabbit, setting the sqs adapters deletion policy to onSuccess, the message seems to correctly not be deleted (but increases Read Count). So is it safe to use the direct channel in this scenario? – Jim Hankins Jan 12 '19 at 21:00
  • You shouldn't ask unrelated questions in comments to existing answers; it doesn't help the community find questions/answers. A `DirectChannel` is "safe" in that the the publishing will occur on the calling thread. I am not familiar with the SQS extension so I can't comment on when the `onSuccess` policy applies and whether it will work in this case (my guess is it might be ok because it's a message-driven adapter so the flow runs on the adapter thread). – Gary Russell Jan 12 '19 at 21:35
  • That said, RabbitMQ publishing is async anyway, so you would need to use a RabbitMQ transaction to ensure the broker accepted the message; transactions are a significant overhead with RabbitMQ publishing; especially when only publishing one message. – Gary Russell Jan 12 '19 at 21:35
  • Thanks Gary. I'll keep that in mind. To be fair, I stated in my goal of the question the desire to not loose a message, so I would argue that it's related to the question. But I recognize there could be a separate question subject line on just that specific topic so I'll keep that in mind. Thanks again! – Jim Hankins Jan 12 '19 at 22:23