Class SnsMessageHandler

java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.handler.MessageHandlerSupport
org.springframework.integration.handler.AbstractMessageHandler
org.springframework.integration.handler.AbstractMessageProducingHandler
org.springframework.integration.aws.outbound.AbstractAwsMessageHandler<Map<String,software.amazon.awssdk.services.sns.model.MessageAttributeValue>>
org.springframework.integration.aws.outbound.SnsMessageHandler
All Implemented Interfaces:
org.reactivestreams.Subscriber<org.springframework.messaging.Message<?>>, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.core.Ordered, org.springframework.integration.context.ExpressionCapable, org.springframework.integration.context.Orderable, org.springframework.integration.core.MessageProducer, org.springframework.integration.handler.HeaderPropagationAware, org.springframework.integration.IntegrationPattern, org.springframework.integration.support.context.NamedComponent, org.springframework.integration.support.management.IntegrationManagement, org.springframework.integration.support.management.TrackableComponent, org.springframework.messaging.MessageHandler, reactor.core.CoreSubscriber<org.springframework.messaging.Message<?>>

public class SnsMessageHandler extends AbstractAwsMessageHandler<Map<String,software.amazon.awssdk.services.sns.model.MessageAttributeValue>>
The AbstractAwsMessageHandler implementation to send SNS Notifications (SnsAsyncClient.publish(PublishRequest)) to the provided topicArn (or evaluated at runtime against Message).

The SNS Message subject can be evaluated as a result of subjectExpression.

The algorithm to populate SNS Message body is like:

  • If the payload instanceof PublishRequest it is used as is for publishing.
  • If the bodyExpression is specified, it is used to be evaluated against requestMessage.
  • If the evaluation result (or payload) is instance of SnsBodyBuilder, the SNS Message is built from there and the messageStructure of the PublishRequest is set to json. For the convenience the package org.springframework.integration.aws.support is imported to the AbstractAwsMessageHandler.getEvaluationContext() to allow bypass it for the SnsBodyBuilder from the bodyExpression definition. For example:
     
     String bodyExpression =
     "SnsBodyBuilder.withDefault(payload).forProtocols(payload.substring(0, 140), 'sms')";
     snsMessageHandler.setBodyExpression(spelExpressionParser.parseExpression(bodyExpression));
     
     
  • Otherwise the payload (or the bodyExpression evaluation result) is converted to the String using IntegrationObjectSupport.getConversionService().
Author:
Artem Bilan, Christopher Smith
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.springframework.integration.support.management.IntegrationManagement

    org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides
  • Field Summary

    Fields inherited from class org.springframework.integration.aws.outbound.AbstractAwsMessageHandler

    DEFAULT_SEND_TIMEOUT

    Fields inherited from class org.springframework.integration.handler.AbstractMessageProducingHandler

    messagingTemplate

    Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport

    EXPRESSION_PARSER, logger

    Fields inherited from interface org.springframework.integration.support.management.IntegrationManagement

    METER_PREFIX, RECEIVE_COUNTER_NAME, SEND_TIMER_NAME

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    SnsMessageHandler(software.amazon.awssdk.services.sns.SnsAsyncClient amazonSns)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Map<String,?>
    additionalOnSuccessHeaders(software.amazon.awssdk.awscore.AwsRequest request, software.amazon.awssdk.awscore.AwsResponse response)
     
    protected CompletableFuture<? extends software.amazon.awssdk.awscore.AwsResponse>
    handleMessageToAws(org.springframework.messaging.Message<?> message, software.amazon.awssdk.awscore.AwsRequest request)
     
    protected software.amazon.awssdk.awscore.AwsRequest
    messageToAwsRequest(org.springframework.messaging.Message<?> message)
     
    protected void
     
    void
    setBodyExpression(org.springframework.expression.Expression bodyExpression)
    The Expression to produce the SNS notification message.
    void
    setMessageDeduplicationIdExpression(org.springframework.expression.Expression messageDeduplicationIdExpression)
    The Expression to determine the deduplication ID for this message.
    void
    setMessageGroupId(String messageGroupId)
    A fixed message-group ID to be set for messages sent to an SNS FIFO topic from this handler.
    void
    setMessageGroupIdExpression(org.springframework.expression.Expression messageGroupIdExpression)
    The Expression to determine the message group for messages sent to an SNS FIFO topic from this handler.
    void
    setSubject(String subject)
     
    void
    setSubjectExpression(org.springframework.expression.Expression subjectExpression)
     
    void
    setTopicArn(String topicArn)
     
    void
    setTopicArnExpression(org.springframework.expression.Expression topicArnExpression)
     
    void
    setTopicArnResolver(io.awspring.cloud.sns.core.TopicArnResolver topicArnResolver)
    Provide a custom TopicArnResolver; defaults to SnsAsyncTopicArnResolver.

    Methods inherited from class org.springframework.integration.handler.AbstractMessageProducingHandler

    addNotPropagatedHeaders, createOutputMessage, getNotPropagatedHeaders, getOutputChannel, isAsync, messageBuilderForReply, produceOutput, resolveErrorChannel, sendErrorMessage, sendOutput, sendOutputs, setAsync, setNotPropagatedHeaders, setOutputChannel, setOutputChannelName, setupMessageProcessor, shouldSplitOutput, updateNotPropagatedHeaders

    Methods inherited from class org.springframework.integration.handler.AbstractMessageHandler

    handleMessage, onComplete, onError, onNext, onSubscribe, setObservationConvention

    Methods inherited from class org.springframework.integration.handler.MessageHandlerSupport

    buildSendTimer, destroy, getComponentType, getIntegrationPatternType, getManagedName, getManagedType, getMetricsCaptor, getObservationRegistry, getOrder, getOverrides, isLoggingEnabled, isObserved, registerMetricsCaptor, registerObservationRegistry, sendTimer, setLoggingEnabled, setManagedName, setManagedType, setOrder, setShouldTrack, shouldTrack

    Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport

    afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentName, getConversionService, getExpression, getIntegrationProperties, getIntegrationProperty, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentName, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface reactor.core.CoreSubscriber

    currentContext

    Methods inherited from interface org.springframework.integration.support.management.IntegrationManagement

    getThisAs

    Methods inherited from interface org.springframework.integration.support.context.NamedComponent

    getBeanName, getComponentName
  • Constructor Details

    • SnsMessageHandler

      public SnsMessageHandler(software.amazon.awssdk.services.sns.SnsAsyncClient amazonSns)
  • Method Details

    • setTopicArn

      public void setTopicArn(String topicArn)
    • setTopicArnExpression

      public void setTopicArnExpression(org.springframework.expression.Expression topicArnExpression)
    • setTopicArnResolver

      public void setTopicArnResolver(io.awspring.cloud.sns.core.TopicArnResolver topicArnResolver)
      Provide a custom TopicArnResolver; defaults to SnsAsyncTopicArnResolver.
      Parameters:
      topicArnResolver - the TopicArnResolver to use.
      Since:
      3.0
    • setSubject

      public void setSubject(String subject)
    • setSubjectExpression

      public void setSubjectExpression(org.springframework.expression.Expression subjectExpression)
    • setMessageGroupId

      public void setMessageGroupId(String messageGroupId)
      A fixed message-group ID to be set for messages sent to an SNS FIFO topic from this handler. Equivalent to calling {setMessageGroupIdExpression(Expression) with a literal string expression.
      Parameters:
      messageGroupId - the group ID to be used for all messages sent from this handler
      Since:
      2.5.3
    • setMessageGroupIdExpression

      public void setMessageGroupIdExpression(org.springframework.expression.Expression messageGroupIdExpression)
      The Expression to determine the message group for messages sent to an SNS FIFO topic from this handler.
      Parameters:
      messageGroupIdExpression - the Expression to produce the message-group ID
      Since:
      2.5.3
    • setMessageDeduplicationIdExpression

      public void setMessageDeduplicationIdExpression(org.springframework.expression.Expression messageDeduplicationIdExpression)
      The Expression to determine the deduplication ID for this message. SNS FIFO topics require a message deduplication ID to be specified, either in the adapter configuration or on a PublishRequest payload of the request Message, unless content-based deduplication is enabled on the topic.
      Parameters:
      messageDeduplicationIdExpression - the Expression to produce the message deduplication ID
      Since:
      2.5.3
    • setBodyExpression

      public void setBodyExpression(org.springframework.expression.Expression bodyExpression)
      The Expression to produce the SNS notification message. If it evaluates to the SnsBodyBuilder the messageStructure of the PublishRequest is set to json. Otherwise, the IntegrationObjectSupport.getConversionService() is used to convert the evaluation result to the String without setting the messageStructure.
      Parameters:
      bodyExpression - the Expression to produce the SNS notification message.
    • onInit

      protected void onInit()
      Overrides:
      onInit in class AbstractAwsMessageHandler<Map<String,software.amazon.awssdk.services.sns.model.MessageAttributeValue>>
    • messageToAwsRequest

      protected software.amazon.awssdk.awscore.AwsRequest messageToAwsRequest(org.springframework.messaging.Message<?> message)
      Specified by:
      messageToAwsRequest in class AbstractAwsMessageHandler<Map<String,software.amazon.awssdk.services.sns.model.MessageAttributeValue>>
    • handleMessageToAws

      protected CompletableFuture<? extends software.amazon.awssdk.awscore.AwsResponse> handleMessageToAws(org.springframework.messaging.Message<?> message, software.amazon.awssdk.awscore.AwsRequest request)
      Specified by:
      handleMessageToAws in class AbstractAwsMessageHandler<Map<String,software.amazon.awssdk.services.sns.model.MessageAttributeValue>>
    • additionalOnSuccessHeaders

      protected Map<String,?> additionalOnSuccessHeaders(software.amazon.awssdk.awscore.AwsRequest request, software.amazon.awssdk.awscore.AwsResponse response)
      Specified by:
      additionalOnSuccessHeaders in class AbstractAwsMessageHandler<Map<String,software.amazon.awssdk.services.sns.model.MessageAttributeValue>>