Spring Cloud AWS – Messaging Support – Spring Cloud AWS – 消息支持

最后修改: 2018年 1月 12日

中文/混合/英文(键盘快捷键:t)

In the final article, we move on to AWS Messaging Support.

在最后一篇文章中,我们将继续讨论AWS消息支持。

1. AWS Messaging Support

1.AWS消息传递支持

1.1. SQS (Simple Queue Service)

<1.1 简单队列服务(SQS)

We can send messages to an SQS queue using the QueueMessagingTemplate.

我们可以使用QueueMessagingTemplate向一个SQS队列发送消息。

To create this bean, we can use an AmazonSQSAsync client which is available by default in the application context when using Spring Boot starters:

为了创建这个Bean,我们可以使用一个AmazonSQSAsync客户端,在使用Spring Boot启动器时,该客户端在应用上下文中默认可用。

@Bean
public QueueMessagingTemplate queueMessagingTemplate(
  AmazonSQSAsync amazonSQSAsync) {
    return new QueueMessagingTemplate(amazonSQSAsync);
}

Then, we can send the messages using the convertAndSend() method:

然后,我们可以使用convertAndSend()方法发送消息。

@Autowired
QueueMessagingTemplate messagingTemplate;
 
public void send(String topicName, Object message) {
    messagingTemplate.convertAndSend(topicName, message);
}

Since Amazon SQS only accepts String payloads, Java objects are automatically serialized to JSON.

由于Amazon SQS只接受String有效载荷,Java对象被自动序列化为JSON。

We can also configure listeners using @SqsListener:

我们还可以使用 @SqsListener配置监听器。

@SqsListener("spring-cloud-test-queue")
public void receiveMessage(String message, 
  @Header("SenderId") String senderId) {
    // ...
}

This method will receive messages from spring-cloud-test-queue and then process them. We can also retrieve message headers using the @Header annotation on method parameters.

这个方法将从spring-cloud-test-queue接收消息,然后处理它们。我们还可以使用方法参数上的@Header注解来检索消息头。

If the first parameter is a custom Java object instead of String, Spring will convert the message to that type using JSON conversion.

如果第一个参数是一个自定义的Java对象而不是String,Spring将使用JSON转换将消息转换为该类型。

1.2. SNS (Simple Notification Service)

1.2.SNS(简单通知服务)

Similar to SQS, we can use NotificationMessagingTemplate to publish messages to a topic.

与SQS类似,我们可以使用NotificationMessagingTemplate来发布消息到一个主题。

To create it, we need an AmazonSNS client:

为了创建它,我们需要一个AmazonSNS客户端。

@Bean
public NotificationMessagingTemplate notificationMessagingTemplate(
  AmazonSNS amazonSNS) {
    return new NotificationMessagingTemplate(amazonSNS);
}

Then, we can send notifications to the topic:

然后,我们可以向该主题发送通知。

@Autowired
NotificationMessagingTemplate messagingTemplate;

public void send(String Object message, String subject) {
    messagingTemplate
      .sendNotification("spring-cloud-test-topic", message, subject);
}

Out of the multiple SNS endpoints supported by AWS – SQS, HTTP(S), email and SMS, the project only supports HTTP(S).

在AWS支持的多个SNS端点中–SQS、HTTP(S)、电子邮件和SMS,该项目只支持HTTP(S)

We can configure the endpoints in an MVC controller:

我们可以在MVC控制器中配置端点。

@Controller
@RequestMapping("/topic-subscriber")
public class SNSEndpointController {

    @NotificationSubscriptionMapping
    public void confirmUnsubscribeMessage(
      NotificationStatus notificationStatus) {
        notificationStatus.confirmSubscription();
    }
 
    @NotificationMessageMapping
    public void receiveNotification(@NotificationMessage String message, 
      @NotificationSubject String subject) {
        // handle message
    }

    @NotificationUnsubscribeConfirmationMapping
    public void confirmSubscriptionMessage(
      NotificationStatus notificationStatus) {
        notificationStatus.confirmSubscription();
    }
}

We need to add the topic name to the @RequestMapping annotation on the controller level. This controller enables an HTTP(s) endpoint – /topic-subscriber which be used by an SNS topic to create a subscription.

我们需要在控制器层面的@RequestMapping注解中添加主题名称。该控制器启用了一个HTTP(s)端点–/topic-subscriber,SNS主题使用该端点来创建一个订阅。

For example, we can subscribe to a topic by calling the URL:

例如,我们可以通过调用URL来订阅一个主题。

https://host:port/topic-subscriber/

The header in the request determines which of the three methods is invoked.

请求中的标头决定了三种方法中的哪一种被调用。

The method with @NotificationSubscriptionMapping annotation is invoked when the header [x-amz-sns-message-type=SubscriptionConfirmation] is present and confirms a new subscription to a topic.

当标头[x-amz-sns-message-type=SubscriptionConfirmation]出现时,带有@NotificationSubscriptionMapping注解的方法被调用,并确认对一个主题的新订阅。

Once subscribed, the topic will send notifications to the endpoint with the header [x-amz-sns-message-type=Notification]. This will invoke the method annotated with @NotificationMessageMapping.

一旦订阅,该主题将向端点发送通知,标题为[x-amz-sns-message-type=Notification]。这将调用用@NotificationMessageMapping注释的方法。

Finally, when the endpoint unsubscribes from the topic, a confirmation request is received with the header [x-amz-sns-message-type=UnsubscribeConfirmation].

最后,当端点从主题退订时,会收到一个确认请求,标题为[x-amz-sns-message-type=UnsubscribeConfirmation]

This calls the method annotated with @NotificationUnsubscribeConfirmationMapping which confirms unsubscribe action.

这将调用以@NotificationUnsubscribeConfirmationMapping注释的方法,该方法确认退订行动。

Please note that the value in @RequestMapping has nothing to do with the topic name to which it’s subscribed.

请注意,@RequestMapping中的值与它所订阅的主题名称没有关系。

2. Conclusion

2.结论

In this final article, we explored Spring Cloud’s support for AWS Messaging – which concludes this quick series about Spring Cloud and AWS.

在这最后一篇文章中,我们探讨了Spring Cloud对AWS Messaging的支持–这就结束了这个关于Spring Cloud和AWS的快速系列。

As usual, the examples are available over on GitHub.

像往常一样,这些例子可以在GitHub上找到over

« Previous

Spring Cloud AWS – RDS