How to send multiple emails using Amazon SQS and SNS? - java

In my JAVA application, I'm using Amazon SQS and SNS; I did the below steps:
Step 1: I pushed the message to SQS like,
SendMessageResult aSendMessageStatus = Amazon_SQS_Client.sendMessage(new SendMessageRequest().withQueueUrl(AWS_SQS_URL).withMessageBody(theRequestString));
Step 2: Created topic in SNS like,
CreateTopicResult createRes = Amazon_SNS_Client.createTopic(createReq);
Step 3: Now I am trying to send email by receiving the messages from SQS to 100 of customer.
Can someone advice me on how to subscribe the topic in SNS and send the emails to multiple email addresses.

For SNS to deliver the message, the 100 email address would have to subscribe to the topic. The email address will get confirmation message which they will have to respond to.
For your scenario, another option could be to use the queue service. It can be done in multiple ways. I have a setup as follows:
An application component sends a message to a queue
Another application component polls the queue, retrieves the message
From the message an email is composed and use SES service to deliver emails.
Another option is to use SNS -> SQS -> SES setup, where initial notification goes to SNS, and SNS delivers the notification to SQS.
The notification message itself need not be the complete email message. It could be just a reference to the content and people to which the content is to be delivered. Your application could take care of forming the complete message.
For a scenario where email is delivered to a general application user, I think SES is the right solution rather than SNS.

Related

Processing just one service bus topic message at a time

I am subscribed to a Azure servicebus topic made by an external department. The way I want the code to work is as follows:
Trigger an http endpoint that starts processorClient and listens to the topic.
Fetches one message
Does required actions to that message
Closes processorClient connection.
Repeat
I am using the ServiceBusProcessorClient class as shown in the following documentation Receive messages from a subscription
Is there a way to utilize this code in order to only fetch one message from the topic at a time before calling the processorClient.close();
I have tried using setting maxConcurrentCalls and setting prefetchCount(0).

How to push notification with Cloud Messaging Firebase from the server

I already have an app and I want to start sending notification to the users. I already set up everything in the app(using react native) and I checked manually that I can send notification to the devices and it works.
Now I want to run a job in the server who will push the message (with the device token) to the cloud messaging in firebase.
I can't find a lot of details about how to do it. I would like if someone can give me any guide I can use with. my server is in Kotlin(java can be good too) and I m working with gradle.
Thank you so much for the help
From a Java server you can use the Firebase Admin SDK to send messages. From that documentation comes this minimal example:
// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setToken(registrationToken)
.build();
// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Note that this sends a data message, so that will always be delivered to your code, where you can decide to display a notification or not. To send a notification message, which is what the Firebase console does, you'd use:
Message message = Message.builder()
.setNotification(new Notification("This is the title", "This is the body"))
.setToken(registrationToken)
.build();
Both of these send the message to a specific registration token, so only to a single device/app instance. This means you will need to maintain a list of these tokens, in a way that allows you to send the messages to fit your needs. E.g. a common way is to store the tokens per user. For an example of that, see the functions-samples repo. While this example is in Node.js, the same logic could be applied to a Java server.
Finally: you can also send message to topics. For an example of that (again: using a Node.js server), have a look at this blog post Sending notifications between Android devices with Firebase Database and Cloud Messaging.

AWS SQS: Is it a way SQS call me consumer, every time a messages is pushed

Is there a way by which AWS SQS can call my REST API? Basically as soon as message is pushed to AWS SQS, I want to hear it and perform required action. I can schedule a listener that can pull messages every second but that won't be an optimizes solution and also the queue might be empty(sometimes).
Thanks In Advance!!
A couple of thoughts:
Use Publisher/Subscriber
Look into using a publisher-subscriber model with SNS/SQS, so that you publish a message to SNS and subscribe to it via SQS. If you absolutely need to handle a message as soon as it is published, you can publish to SNS and set another consumer in addition to your SQS subscription (such as a lambda subscriber that calls your Rest API?) to process it instead.
SQS Long Polling
Regarding SQS, it sounds like you would benefit from long polling. From the documentation:
Long polling helps reduce your cost of using Amazon SQS by reducing
the number of empty responses (when there are no messages available to
return in reply to a ReceiveMessage request sent to an Amazon SQS
queue) and eliminating false empty responses (when messages are
available in the queue but aren't included in the response):
Long polling reduces the number of empty responses by allowing Amazon
SQS to wait until a message is available in the queue before sending a
response. Unless the connection times out, the response to the
ReceiveMessage request contains at least one of the available
messages, up to the maximum number of messages specified in the
ReceiveMessage action.
Long polling eliminates false empty responses by querying all (rather than a limited number) of the servers.
Long polling returns messages as soon any message becomes available.
Also from the documentation, to enable long polling programmatically, use the following for any of these SQS actions:
ReceiveMessage: WaitTimeSeconds parameter
CreateQueue: ReceiveMessageWaitTimeSeconds attribute
SetQueueAttributes: ReceiveMessageWaitTimeSeconds attribute
Reference:
Publish–subscribe (PubSub) Pattern
SQS Documentation - Long Polling
Sounds like you would be much better of using SNS instead of SQS. What you are trying to get SQS to do, SNS was designed to do:
You can use Amazon SNS to send notification messages to one or more
HTTP or HTTPS endpoints. When you subscribe an endpoint to a topic,
you can publish a notification to the topic and Amazon SNS sends an
HTTP POST request delivering the contents of the notification to the
subscribed endpoint. When you subscribe the endpoint, you select
whether Amazon SNS uses HTTP or HTTPS to send the POST request to the
endpoint. If you use HTTPS, then you can take advantage of the support
in Amazon SNS for the following...
http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html

Spring JMS Subscriber not able to receive message?

I'm using Spring JMS and Web Sphere Default Messaging Provider for my Messaging Needs.
I have created a Topic and connection factory on WAS.
I am able to send message to Topic using JMSTemplate from my publisher.
But Whenever I call JmsTemplate.receive() in my Subscriber , it goes into waiting state and I am not able to receive message in my Subscriber.
However I am able to receive message in Subscriber using JmsTemplate if a use a Listener as Subscriber or if a start a new thread whenever I am publishing message using my Publisher.
Looking for help !
Messages are not stored in the topic. If you create a Publisher, send messages ans then start a subscriber no messages will be subscribed. At the time of publishing if there are any subscribers subscribed to that topic then only those subscribers will receive the message. If no subscribers exist then messages will be dropped.
It goes into waiting state that means it is waiting for messages from the JMS server and there are no currently available.
Whatever mode of subscribing you are using - asynchronous(MessageListener) or Synchronous(receive) you need to create subscriber before.
You can use
subscriber.receive(long timeout);
if you do not want to wait indefinitely.

Amazon SimpleEmail: how to check if an Email has been delivered?

I tried to send emails with Amazon SES, with the Java AWS SDK, and it worked. I would like to be able to check (at a later time) whether the delivery was successful. I will define it successful if the final mailserver accepted the mail for delivery.
I saw that when you send an email you can get a messageId that uniquely identifies your email:
SendEmailRequest request = new SendEmailRequest(from, destination, message);
SendEmailResult result = service.sendEmail(request);
String messageId = result.getMessageId();
However I saw that you can get only aggregated statistics, for example with SendDataPoint (Represents sending statistics data. Each SendDataPoint contains statistics for a 15-minute period of sending activity).
I'm not using SES to send bulk emails, but personalized notifications on a very low volume and I'd be interested to check every single message.
Did I overlook something? Is it possible to do this type of check with SES?
Amazon does provide a mechanism for you to capture bounces, which provides you with contrapositive verification.
You can create a mailbox to receive bounce notifications, then tell SES to forward bounce notifications there. e.g.:
request.setReturnPath("bounces#example.com");
You can then write code to periodically check that mailbox, and parse the messages for the destination email address.
Amazon provides a brief explanation of how they handle bounces & complaints here:
http://aws.amazon.com/ses/faqs/#37
However, if you want to check if the message avoided the spam filter or was read by the end user, that is beyond the scope of SES (although they work hard to ensure deliverability).
We use Bouncely.com. You simply set the ReturnPath to bounces#bouncely.com and it tracks all the bounces and spam reports. It also has an API that allows us to unsubscribe users automatically.
Use Amazon Simple Notification Service and define an HTTP endpoint to receive notification in case of email bounces. Works perfectly.

Categories