The Spring Cloud AWS configuration is currently done using custom elements provided by Spring Cloud AWS namespaces. JavaConfig will be supported soon.Soon!! huh?!! Well, this is a problem since I did not want to use XML config in my project.
However, the documentation adds
It is recommended to use the XML messaging namespace to createI needed to use custom JSON convertor to convert my message and I was not going to use xml config. So I decided to give it a go and create the java config I need. I went on to read the spring-cloud-aws project code and some of the examples online. Then, borrowed some code from here and there that helped me accomplish my task. Here is the code, hopefully, it will make someone's life a little bit easier.QueueMessagingTemplate
as it will set a more sophisticatedMessageConverter
that converts objects into JSON when Jackson is on the classpath.
package com.coffeebeans.config; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSAsync; import com.amazonaws.services.sqs.AmazonSQSAsyncClient; import com.amazonaws.services.sqs.buffered.AmazonSQSBufferedAsyncClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.aws.context.annotation.ConditionalOnMissingAmazonClient; import org.springframework.cloud.aws.messaging.config.QueueMessageHandlerFactory; import org.springframework.cloud.aws.messaging.config.SimpleMessageListenerContainerFactory; import org.springframework.cloud.aws.messaging.listener.QueueMessageHandler; import org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; import org.springframework.messaging.converter.MessageConverter; import org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import java.util.ArrayList; import java.util.List; /** * Created by muhamadto on 8/18/15. */ @Configuration @ComponentScan("com.coffeebeans") public class SqsConfig { @Value("${queue.endpoint}") private String queueEndPoint; @Value("${aws.region}") private String awsRegion; @Autowired MessageConverter messageConverter; @Bean public SimpleMessageListenerContainer simpleMessageListenerContainer() { SimpleMessageListenerContainer messageListenerContainer = simpleMessageListenerContainerFactory().createSimpleMessageListenerContainer(); messageListenerContainer.setMessageHandler(queueMessageHandler()); return messageListenerContainer; } @Bean public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(){ SimpleMessageListenerContainerFactory messageListenerContainerFactory = new SimpleMessageListenerContainerFactory(); messageListenerContainerFactory.setAmazonSqs(amazonSQS()); messageListenerContainerFactory.setDeleteMessageOnException(false); return messageListenerContainerFactory; } @Bean public QueueMessageHandler queueMessageHandler() { QueueMessageHandlerFactory queueMessageHandlerFactory = new QueueMessageHandlerFactory(); queueMessageHandlerFactory.setAmazonSqs(amazonSQS()); QueueMessageHandler messageHandler = queueMessageHandlerFactory.createQueueMessageHandler(); List<HandlerMethodArgumentResolver> list = new ArrayList<>(); HandlerMethodArgumentResolver resolver = new PayloadArgumentResolver(this.messageConverter); list.add(resolver); messageHandler.setArgumentResolvers(list); return messageHandler; } @Lazy @Bean(destroyMethod = "shutdown") @ConditionalOnMissingAmazonClient(AmazonSQS.class) public AmazonSQSAsync amazonSQS() { AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(new DefaultAWSCredentialsProviderChain()); amazonSQSAsyncClient.setEndpoint(this.queueEndPoint); amazonSQSAsyncClient.setRegion(Region.getRegion(Regions.fromName(this.awsRegion))); return new AmazonSQSBufferedAsyncClient(amazonSQSAsyncClient); } }Now annotate a method that belongs to any class in com.coffeebeans package with @MessageMapping("queue_name") where MessageMapping is org.springframework.messaging.handler.annotation.MessageMapping
For example
package com.coffeebeans.listener; import com.coffeebeans.message.Message; import org.springframework.messaging.handler.annotation.MessageMapping; /** * Created by muhamadto on 8/18/15. */ public class QueueListener { @MessageMapping("queue_name") public void onMessage(Message message) throws Exception{ System.out.println(message); } }As mentioned in the documentation and quoted above you will need Jackson in your class path.