





















































Hi ,
Welcome to this week’s edition of ProgrammingPro!
In today’sExpert Insight, we bring you an excerpt from the recently published book, Java Concurrency and Parallelism, which demonstrates building a data processing pipeline using a microservices architecture with Java.
News Highlights: C language falls to its lowest position in the Tiobe index; TypeScript 5.6 adds new compiler options and diagnostics; Vapor 5 leverages Swift 6 concurrency; and Rust 1.81 stabilizes the Error trait.
My top 5 picks from today’s learning resources:
But there’s more, so dive right in.
Stay Awesome!
Divya Anne Selvaraj
Editor-in-Chief
PS: Thismonth’ssurvey is still live. Do take the opportunity to tell us what you think of ProgrammingPro, request learning resources, and earn your one Packt Credit for this month.
#![no_std]
libraries, and introduces improved sort algorithms for better performance.std::regex
into a C interface, providing a way to use C++ regular expressions in C projects without dealing with C++ directly.Discover strategies and best practices to develop high performance Java applications
Proven recipes for building modern and robust Java web applications with Spring Boot
Master Continuous Integration with Jenkins: Automate, Test, and Deploy Like a Pro
LaunchedEffect
within a composable and ViewModel.init()
.Here’s an excerpt from “Chapter 8: Microservices in the Cloud and Java’s Concurrency” in the book, Java Concurrency and Parallelism, by Jay Wang, published in August 2024.
This case study delves into designing and implementing a data processing pipeline using amicroservices architecture:
pom.xml
file in each project’s root directory and add therelated dependencies.@Service
public class DataIngestionService {
private final AmazonSQS sqsClient;
public DataIngestionService(AmazonSQS sqsClient) {
this.sqsClient = sqsClient;
}
public void ingestData(Data dat{
// Validate the incoming data
if (isValid(data)) {
// Publish the data to Amazon SQS
SendMessageRequest sendMessageRequest = new SendMessageRequest()
.withQueueUrl("data-ingestion-queue-url")
.withMessageBody(data.toString());
sqsClient.sendMessage(sendMessageRequest);
}
}
private boolean isValid(Data dat{
boolean isValid = true;
// Implement data validation logic
// ...
return isValid;
}
The code represents the implementation of the data ingestion service, which is responsible for receiving incoming data, validating it, and publishing it to Amazon SQS forfurther processing.
TheDataIngestionService
class is annotated with@Service
, indicating that it is a Spring service component. It has a dependency on theAmazonSQS client
, which is injected throughthe constructor.
TheingestData()
method takes adata object
as input and performs data validation by calling theisValid()
method. If the data is valid, it creates aSendMessageRequest
object with the specified SQS queue URL and the data payload as the message body. The message is then sent to the SQS queue using thesqsClient.sendMessage()
method.
public class DataProcessingLambda implements RequestHandler<SQSEvent, Void> {
private final AmazonSQS sqsClient;
public DataProcessingLambda() {
this.sqsClient = AmazonSQSClientBuilder.defaultClient();
}
@Override
public Void handleRequest(SQSEvent event,
Context context) {
for (SQSEvent.SQSMessage message :
event.getRecords()) {
String data = message.getBody();
// Transform the data within the Lambda function
String transformedData= transformData(
data);
// Publish the transformed data to another Amazon SQS for persistence or further
// processing
sqsClient.sendMessage(
new SendMessageRequest()
.withQueueUrl(
"processed-data-queue-url")
.withMessageBody(transformedData));
}
return null;
}
/**
* Simulate data transformation.
* In a real scenario, this method would contain logic to transform data based
* on specific rules or operations.
*
* @param data the original data from the SQS message
* @return transformed data as a String
*/
private String transformData(String dat{
// Example transformation: append a timestamp or modify the string in some way
return "Transformed: " + data + " at " + System. currentTimeMillis();
}
}
This Lambda function,DataProcessingLambda
, processes data from an Amazon SQS queue by implementing theRequestHandler
interface to handleSQSEvent
events. It initializes an Amazon SQS client in the constructor and uses it to send transformed data to another SQS queue for further processingor storage.
ThehandleRequest()
method, serving as the function’s entry point, processes eachSQSMessage
from theSQSEvent
, extracting the data and transforming it directly within the function through thetransformData()
method. Here, the transformation appends a timestamp to the data as a simple example, but typically this would involve more complex operations tailored to specific dataprocessing requirements.
Following the data transformation, the function sends the processed data to a specified SQS queue by invoking thesendMessage()
method on theSQS client.
@Service
public class DataPersistenceService {
private final AmazonSNS snsClient;
private final DataRepository dataRepository;
public DataPersistenceService(DataRepository dataRepository) {
// Initialize the AmazonSNS client
this.snsClient = AmazonSNSClientBuilder.standard(). build();
this.dataRepository = dataRepository;
}
public void persistData(String data{
// Assume 'data' is the processed data received
// Store the processed data in a database
Data dataEntity = new Data();
dataEntity.setProcessedData(data);
dataRepository.save(dataEntity);
// Send notification via SNS after successful persistence
sendNotification("Data has been successfully persisted with the following content: " + data);
}
private void sendNotification(String message) {
// Define the ARN of the SNS topic to send notification to
String topicArn = "arn:aws:sns:region:account-id:your- topic-name";
// Create the publish request
PublishRequest publishRequest = new PublishRequest()
.withTopicArn(topicArn)
.withMessage(message);
// Publish the message to the SNS topic
snsClient.publish(publishRequest);
}
}
DataPersistenceService
is a Spring-managed bean responsible for handling data persistence and notifying other components or services via Amazon SNS....
Java Concurrency and Parallelismwas published in August 2024. Packt library subscribers can continue readingthe entire book for free or you can buy the bookhere!
That’s all for today.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere. Complete ProgrammingPro archives can be foundhere. Complete PythonPro archives arehere.
If your company is interested in reaching an audience of developers, software engineers, and tech decision makers, you may want toadvertise with us.