An example of message-oriented middleware

Let's look at a code example that publishes and consumes a message. Java Message Service (JMS) has been the industry standard for years. For sake of this example, we will use Amazon's Simple Queue Service (SQS) to avoid the additional overhead of setting up middleware and the queue infrastructure. 

AWS provides us with a simple mechanism to create the queue, but you can use any provider of your choice.

The following screenshot shows how a queue is created in AWS-SQS:

As you can see, all we need to provide is a queue name and a Quick Create Queue. If required, you can update default configurations, such as how long you want the message to be available in the queue, by clicking the Configure Queue button. Also, you can use the FIFO queue, which guarantees the order of messages being sent is in the form of "first in first out." For the sake of this example, we will use the default configurations.

Here is the code to demonstrate the read and write operations on the AWS queue:

import java.util.List;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.SendMessageRequest;
public class SendReceiveMessages
{
public static void main(String[] args)
{
AWSCredentialsProvider provider;
AWSCredentials credentials = newBasicAWSCredentials("Key","Secret");
provider = new StaticCredentialsProvider(credentials);
AmazonSQS sqs = AmazonSQSClientBuilder.standard().withCredentials(provider).withRegion(Regions.US_EAST_1).build();
String queueUrl = "https://sqs.us-east-1.amazonaws.com/305881070752/TestQueue";
SendMessageRequest send_msg_request = new SendMessageRequest()
.withQueueUrl("https://sqs.us-east-1.amazonaws.com/305881070752/TestQueue")
.withMessageBody("hello world")
.withDelaySeconds(5);
sqs.sendMessage(send_msg_request);
// receive messages from the queue
List<Message> messages = sqs.receiveMessage(queueUrl).getMessages();
System.out.println("message:"+messages.toString());
// delete messages from the queue
for (Message m : messages)
{
sqs.deleteMessage(queueUrl, m.getReceiptHandle());
}
}
}

This code sends and receives a message from the AWS queue. This is a straightforward implementation of point-to-point communication. AWS also supports the creation of topics through its Simple Notification Service (SNS). These topics can then be attached to queues, so when a new message comes into the system for a topic, it's delivered to all the queues listening to that topic, and hence delivered to listeners of the queue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.84.169