2
votes

While trying to send message to AWS SNS topic using com.amazonaws.services.sns java module, I am stuck on following error:

shaded.com.amazonaws.services.sns.model.InvalidParameterException: Invalid parameter: Message too long (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: 3b01ce49-a37d-5aba-bec2-9ab9d5446aea)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1587)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1257)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1029)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:741)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:715)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:697)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:665)
at shaded.com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:647)
at shaded.com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:511)
at shaded.com.amazonaws.services.sns.AmazonSNSClient.doInvoke(AmazonSNSClient.java:2270)
at shaded.com.amazonaws.services.sns.AmazonSNSClient.invoke(AmazonSNSClient.java:2246)
at shaded.com.amazonaws.services.sns.AmazonSNSClient.executePublish(AmazonSNSClient.java:1698)
at shaded.com.amazonaws.services.sns.AmazonSNSClient.publish(AmazonSNSClient.java:1675)

Following is the AmazonSNS helper Class. This class manages the client creation and publishing message to SNS topic.

import java.io.Serializable;

import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;

public class AWSSNS implements Serializable {

private static final long serialVersionUID = -4175291946259141176L;
protected AmazonSNS client;

public AWSSNS(){
    this.client=AmazonSNSClientBuilder.standard().withRegion("us-west-2").build();
}

public AWSSNS(AmazonSNS client) { 
    this.client=client;
}

public AmazonSNS getSnsClient(){
    return this.client;
}

public void setSqsClient(AmazonSNS client){
    this.client = client;
}

public boolean sendMessages(String topicArn, String messageBody){

    PublishRequest publishRequest = new PublishRequest(topicArn, messageBody);
    PublishResult publishResult = this.client.publish(publishRequest);

    if(publishResult != null && publishResult.getMessageId() != null){
        return true;
    }
    else{
        return false;
    }
}
}

Following is the code snippet from where the amazonSNS helper class is being called.It does nothing but create a message of String dataType and send it forward along with the topicARN.

HashMap<String, String> variable_a = new HashMap<String, String>();
Gson gson = new Gson();

for (Object_a revoke : Object_a) {
    Object_a operation = someMethod1(revoke);
    String serializedOperation = gson.toJson(operation);

    variable_a.put(revoke.someMethod2(), serializedOperation);
    String message = gson.toJson(variable_a);

    LOG.info(String.format("SNS message: %s", message));
    this.awsSNS.sendMessages(topicARN, message);
}

So basically the error is thrown from inside the sendMessage.

1
Can you provide a code snippet for the problem described in your postpiy26
Added code snippets. Let me know if you need anymore info.Kshitiz Agrawal

1 Answers

1
votes

Found the solution to the problem.

AWS SNS topic has a fixed maximum size. So, on publishing messages of larger size than the maximum size would result in invalidParameterException with message "message too long".

My message was more than that size and that was the reason for the error. I shredded the message until the size came under max size.