如何在React& amp;中设置AWS消息队列(SMS) Java Spring

时间:2017-11-22 10:46:08

标签: java spring reactjs amazon-web-services amazon-sqs

我在AWS EC2实例中有一个基于React的应用程序,在另一个AWS EC2实例中有一个基于Java Spring Boot的应用程序。我需要使用来自React应用程序的AWS'SQS发送POST请求。发送消息后,我需要在托管API端点的Java Spring应用程序中检索它们。关于如何完成发送和接收操作的指导将不胜感激。

1 个答案:

答案 0 :(得分:0)

我使用下面的代码从sqs中获取对象。

MessagingConfiguration.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import com.amazon.sqs.javamessaging.SQSConnectionFactory;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.es.spring.messaging.MessageReceiver;

@Configuration
public class MessagingConfiguration {

        @Value("${queue.endpoint}")
        private String endpoint;

        @Value("${queue.name}")
        private String queueName;

        @Autowired
        private MessageReceiver messageReceiver;

        @Bean
        public DefaultMessageListenerContainer jmsListenerContainer() {

            SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
                    .withAWSCredentialsProvider(new DefaultAWSCredentialsProviderChain())
                    .withEndpoint(endpoint)
                    .withAWSCredentialsProvider(awsCredentialsProvider)
                    .withNumberOfMessagesToPrefetch(10).build();

            DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
            dmlc.setConnectionFactory(sqsConnectionFactory);
            dmlc.setDestinationName(queueName);

            dmlc.setMessageListener(messageReceiver);

            return dmlc;
        }

        @Bean
        public JmsTemplate createJMSTemplate() {

            SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
                    .withAWSCredentialsProvider(awsCredentialsProvider)
                    .withEndpoint(endpoint)
                    .withNumberOfMessagesToPrefetch(10).build();

            JmsTemplate jmsTemplate = new JmsTemplate(sqsConnectionFactory);
            jmsTemplate.setDefaultDestinationName(queueName);
            jmsTemplate.setDeliveryPersistent(false);


            return jmsTemplate;
        }

        private final AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
            @Override
            public AWSCredentials getCredentials() {
                return new BasicAWSCredentials("accessKey", "Secretkey");
            }

            @Override
            public void refresh() {

            }
    };

}

MessageReceiver.java

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class MessageReceiver implements MessageListener{

     Logger LOG = LoggerFactory.getLogger(MessageReceiver.class);

    @Override
    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("message:"+textMessage.getText());
            CustomClass activeMq =new ObjectMapper().readValue(textMessage.getText(),CustomClass.class);
            LOG.info("Application : Active Mq : {}",activeMq);  

        } catch (Exception e) {     
            e.printStackTrace();            
        }

    }
}

在SQS中发送消息。

AmazonSQS awsSqs = new AwsSqsClient().getAWSSqsclient();        
        awsSqs.sendMessage(new SendMessageRequest().withDelaySeconds(60)
                .withQueueUrl("https://sqs-url/TestQueue").withMessageBody(input));

希望以上答案可以帮到你。