Showing posts with label Jackson. Show all posts
Showing posts with label Jackson. Show all posts

Friday, 8 November 2024

Amazon SQS Java: Send and Receive Product Objects | Amazon SQS: Sending and Receiving Custom Objects

🚀 Master AWS Development with Java!

Subscribe to Ram N Java for more hands-on tutorials on AWS SDK and Java integration.

SUBSCRIBE TO THE CHANNEL

Introduction

Amazon Simple Queue Service (SQS) is a fundamental tool for building decoupled, distributed systems. While SQS natively handles strings, most real-world applications need to exchange complex data. In this tutorial, we demonstrate how to use the AWS SDK for Java and Jackson to send and receive custom Product objects by serializing them into JSON.

Step 1: Maven Dependencies

To follow along, ensure your pom.xml includes the AWS SDK for SQS and the Jackson library for JSON processing:

<!-- AWS SDK for SQS -->
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sqs</artifactId>
    <version>2.x.x</version>
</dependency>

<!-- Jackson for JSON -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

Step 2: Defining the Product Class

Create a simple POJO to represent your data. This class should have a default constructor and standard getters/setters for Jackson to work correctly.

public class Product {
    private int id;
    private String name;
    private double price;
    // Getters and Setters
}

Step 3: The Producer (Sending Objects)

The producer converts the Java object to a JSON string using ObjectMapper and sends it to the SQS queue using the SendMessageRequest.

ObjectMapper mapper = new ObjectMapper();
String productJson = mapper.writeValueAsString(new Product(1, "iPhone 16 Pro Max", 125000));

SendMessageRequest sendMsgRequest = SendMessageRequest.builder()
    .queueUrl(queueUrl)
    .messageBody(productJson)
    .build();
sqsClient.sendMessage(sendMsgRequest);

Step 4: The Consumer (Receiving Objects)

The consumer receives the message, extracts the JSON body, and deserializes it back into a Product object. Don't forget to delete the message after successful processing!

ReceiveMessageResponse response = sqsClient.receiveMessage(receiveRequest);
for (Message message : response.messages()) {
    Product product = mapper.readValue(message.body(), Product.class);
    System.out.println("Processing: " + product.getName());
    
    // Delete message from queue
    DeleteMessageRequest deleteRequest = DeleteMessageRequest.builder()
        .queueUrl(queueUrl)
        .receiptHandle(message.receiptHandle())
        .build();
    sqsClient.deleteMessage(deleteRequest);
}

Conclusion

By combining the AWS SDK with Jackson, you can easily pass complex data structures through Amazon SQS. This pattern is essential for microservices architectures where different components need to exchange typed data asynchronously. Happy coding!

Saturday, 4 February 2023

Java Program to Send Custom Object into Kafka Topic & Consume Custom Object From Kafka Topic

🚀 Master Object Serialization!

Subscribe to Ram N Java for simplified tutorials on Apache Kafka, Custom Serializers, and Advanced Java Backend!

SUBSCRIBE TO OUR CHANNEL

Kafka Java: Sending & Consuming Custom POJOs

While strings are simple, real-world enterprise applications deal with complex data. In this tutorial, we "simplify" the process of Sending and Consuming Custom Objects (POJOs) using Apache Kafka and Java.

Handling Complex Data Types

We walk through the complete implementation for moving structured data through your Kafka cluster:

  • Custom Serializer: Implementing the Serializer interface to convert Java objects into byte arrays using Jackson.
  • Custom Deserializer: Implementing the Deserializer interface to reconstruct your objects on the consumer side.
  • Producer Configuration: Setting up the KafkaProducer to use your custom class for value serialization.
  • Consumer Implementation: Building the polling loop that receives and casts data back to your custom Java class.

Essential for Microservices

For Java Developers, the ability to send domain objects like User, Order, or Product directly through Kafka is a fundamental skill. We show you how to leverage the Jackson ObjectMapper to handle JSON conversion seamlessly, ensuring your Event-Driven Architecture is robust and type-safe. This guide provides the practical code you need for real-world development.

Practical Code-First Learning

Don't get stuck with just text messages. This tutorial provides the technical clarity to handle any data structure in Apache Kafka. By walking through a concrete example with a "Student" object, we make complex serialization accessible. Join us at Ram N Java and elevate your backend programming skills.

📥 Start Building!

Watch the full video to see the step-by-step Java implementation. Subscribe to Ram N Java for more high-quality tech guides and simplified backend tutorials!

Friday, 24 April 2020

How to send/receive a product object to/from the queue (Spring boot+JMS+ RabbitMQ Example)?

🚀 Master Messaging Architecture!

Subscribe to Ram N Java for simplified Java, RabbitMQ, and Spring Boot tutorials!

SUBSCRIBE TO OUR CHANNEL

Sending Java Objects with RabbitMQ

In distributed systems, you often need to move complex data, not just simple strings. In this tutorial, we "simplify" the process of sending and receiving Java Objects as messages using Spring Boot and RabbitMQ.

Advanced Message Conversion

Moving from plain text to rich objects requires the right configuration. We walk you through the essential components to make this transition seamless:

  • JSON Message Converter: Setting up the Jackson2JsonMessageConverter to automatically serialize your Java beans into JSON for the wire.
  • RabbitTemplate Configuration: Customizing your template to use the converter so every convertAndSend call handles objects naturally.
  • POJO Mapping: Ensuring your producer and consumer share a compatible data structure for perfect deserialization.

The Full Message Lifecycle

We demonstrate a practical example where a producer application sends a custom Employee object. You'll see how the message travels through the RabbitMQ exchange and is eventually picked up by a consumer, which automatically reconstructs the JSON back into a Java object. This "magic" is what allows your microservices to share complex state effortlessly.

Why This is a Game Changer

Mastering Object Messaging in Spring Boot is a fundamental skill for building modern, decoupled applications. It simplifies your code by removing manual parsing logic and lets you focus on your business logic. For any Java Developer looking to build professional-grade systems, this is a must-know technique.

📥 Get the Source Code!

The complete Java source code and PowerPoint presentation for this object-based messaging tutorial are available! Check the download links in the YouTube video description above to get started.

Tutorials