Skip to content

Sending AWS SNS Messages

The AWS SNS connector allows you to send messages to an AWS SNS topic.

Sending messages

Before you start, you need to have an AWS account and an SNS topic created. To send messages to an SNS topic, you need to create a method that produces messages to the topic.

Then, your application can send Message<String> to the prices channel. It can use String payloads as in the following snippet:

package sns.outbound;

import java.time.Duration;
import java.util.UUID;

import jakarta.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.reactive.messaging.Outgoing;

import io.smallrye.mutiny.Multi;

@ApplicationScoped
public class SnsProducer {

    @Outgoing("data")
    public Multi<String> generate() {
        // It emits a UUID every second
        return Multi.createFrom().ticks().every(Duration.ofSeconds(1))
                .map(x -> UUID.randomUUID().toString());
    }

}

Or, you can send Message<Double>, which affords the opportunity to explicitly specify metadata on the outgoing message:

package sns.outbound;

import java.time.Duration;
import java.util.UUID;

import jakarta.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.reactive.messaging.Message;
import org.eclipse.microprofile.reactive.messaging.Metadata;
import org.eclipse.microprofile.reactive.messaging.Outgoing;

import io.smallrye.mutiny.Multi;
import io.smallrye.reactive.messaging.aws.sns.SnsOutboundMetadata;

@ApplicationScoped
public class SnsMessageProducer {

    @Outgoing("prices")
    public Multi<Message<String>> generate() {
        // It emits a UUID every second
        return Multi.createFrom().ticks().every(Duration.ofSeconds(1))
                .map(x -> Message.of(UUID.randomUUID().toString(),
                        Metadata.of(SnsOutboundMetadata.builder()
                                .groupId("group-1")
                                .build())));
    }
}

Sending messages in batch

You can configure the outbound channel to send messages in batch of maximum 10 messages (AWS SNS limitation).

You can customize the size of batches, 10 being the default batch size, and the delay to wait for new messages to be added to the batch, 3000ms being the default delay:

1
2
3
4
5
mp.messaging.outgoing.prices.connector=smallrye-sns
mp.messaging.outgoing.prices.topic=prices
mp.messaging.outgoing.prices.batch=true
mp.messaging.outgoing.prices.batch-size=5
mp.messaging.outgoing.prices.batch-delay=3000

Serialization

When sending a Message<T>, the connector converts the message into a AWS SNS Message. How the message is converted depends on the payload type:

  • If the payload is of type PublishRequest it is sent as is.
  • If the payload is of type PublishRequest.Builder, the topic url is set and sent.
  • If the payload is of primitive types the payload is converted to String and the message attribute _classname is set to the class name of the payload.
  • If the payload is of any other object type, the payload is serialized (using the JsonMapping implementation discovered) and the message attribute _classname is set to the class name of the payload.

If the message payload cannot be serialized to JSON, the message is nacked.

Outbound Metadata

When sending Messages, you can add an instance of SnsOutboundMetadata to influence how the message is handled by AWS SNS. For example, you can configure the routing key, timestamp and headers:

1
2
3
4
5
6
7
8
9
final SnsOutboundMetadata metadata = SnsOutboundMetadata.builder()
        .messageAttributes(Map.of("my-attribute", MessageAttributeValue.builder()
                .dataType("String").stringValue("my-value").build()))
        .groupId("group-1")
        .smsPhoneNumber("+1234567890")
        .build();

// Add `metadata` to the metadata of the outgoing message.
return Message.of("Hello", Metadata.of(metadata));

Acknowledgement

By default, the Reactive Messaging Message is acknowledged when the send message request is successful. If the message is not sent successfully, the message is nacked.

Configuration Reference

Attribute (alias) Description Type Mandatory Default
batch When set, sends messages in batches of maximum 10 messages boolean false false
batch-delay In batch send mode, the maximum delay in milliseconds to wait for messages to be included in the batch. Defaults to 3000 ms int false 3000
batch-size In batch send mode, the maximum number of messages to include in batch, currently SNS maximum is 10 messages int false 10
credentials-provider The credential provider to be used in the client, defaults to AWS default provider chain string false
email.subject When set, sends messages with the specified subject string false
endpoint-override The endpoint override string false
group.id When set, sends messages with the specified group id string false
health-enabled Whether health reporting is enabled (default) or disabled boolean false true
message-structure Set to json if you want to send a different message for each protocol. string false
region The name of the SNS region, defaults to AWS region resolver string false
sms.phoneNumber When set, sends messages to the specified phone number. Not supported in combination with batching. string false
topic The name of the SNS topic, defaults to channel name if not provided string false
topic.arn The arn of the SNS topic string false