All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.interact.sqsdw.MessageDispatcher Maven / Gradle / Ivy

Go to download

aws-dropwizard is a utility library that integrates the Amazon SQS and SNS offerings with the Dropwizard REST framework. It contains convenience classes for sending messages to - and receiving from - SQS queues while being managed by the Dropwizard framework.

There is a newer version: 1.0.7
Show newest version
package io.interact.sqsdw;

import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import com.amazonaws.services.sqs.model.SendMessageRequest;

/**
 * Helps clients to dispatch messages to SQS, that can be handled by a matching
 * {@link MessageHandler}.
 * 
 * @author Bas Cancrinus
 */
public class MessageDispatcher {

    /**
     * Dispatches a message to SQS. {@link MessageHandler}s will handle the
     * message based on a matching value of messageType.
     * 
     * @param messageBody
     *            The body of the message.
     * @param queueUrl
     *            The SQS queue URL.
     * @param messageType
     *            The messageType.
     * @param sqs
     *            The SQS client.
     */
    public static void dispatch(String messageBody, String queueUrl, String messageType, AmazonSQS sqs) {
        sendMessage(messageBody, queueUrl, prepareMessageAttributes(messageType), sqs);
    }

    private static Map prepareMessageAttributes(String messageType) {
        Map messageAttributes = new HashMap<>();
        messageAttributes.put(MessageHandler.ATTR_MESSAGE_TYPE, new MessageAttributeValue().withDataType("String")
                .withStringValue(messageType));
        return messageAttributes;
    }

    private static void sendMessage(String messageBody, String queueUrl, Map messageAttributes,
            AmazonSQS sqs) {
        SendMessageRequest request = new SendMessageRequest();
        request.withMessageBody(messageBody);
        request.withQueueUrl(queueUrl);
        request.withMessageAttributes(messageAttributes);
        sqs.sendMessage(request);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy