Skip to content

This project showcases how to implement OAuth 2 security in a RESTful API using Spring Security. With inspiration from the "Securing REST" chapter in "Spring in Action", this project demonstrates how to create an authorization server.

yacineXP/messaging-with-spring

Repository files navigation


spring
Messaging with Spring [Learning]

Project Description | Tech Stack and Libraries | How it Works | Code Examples | Acknowledgements

🚀 Project Description

This project is based on the "Sending messages asynchronously" chapter of "Spring in Action" book, where I learned how to use Spring to implement asynchronous messaging for the Taco Cloud application. I explored three options that Spring offers for asynchronous messaging: the Java Message Service (JMS), RabbitMQ and Advanced Message Queueing Protocol (AMQP), and Apache Kafka. I also learned about Spring's support for message-driven POJOs, which resemble Enterprise JavaBeans' message-driven beans (MDBs).

🛠️ Tech Stack and Libraries

  • Java Spring
  • Spring JMS
  • Spring RabbitMQ
  • Spring Kafka
  • ActiveMQ
  • RabbitMQ
  • Apache Kafka

⚙️ How it Works

This project leverages the Spring framework to implement asynchronous messaging in the Taco Cloud application. Asynchronous messaging allows for communication between the website and the kitchens to occur independently of one another, improving the responsiveness and scalability of the system.

To enable this functionality, the application uses the Java Messaging Service (JMS), RabbitMQ with the Advanced Message Queuing Protocol (AMQP), or Kafka messaging technologies, all of which are supported by the Spring framework.

💻 Code Examples

1. An exemple of JMS messaging implementation:

@Service
public class JmsOrderMessagingService implements OrderMessagingService {

  private JmsTemplate jms;

  @Autowired
  public JmsOrderMessagingService(JmsTemplate jms) {
    this.jms = jms;
  }

  @Override
  public void sendOrder(TacoOrder order) {
    jms.convertAndSend("tacocloud.order.queue", order,
        this::addOrderSource);
  }
  
  private Message addOrderSource(Message message) throws JMSException {
    message.setStringProperty("X_ORDER_SOURCE", "WEB");
    return message;
  }

}

This code snippet shows how to implement an order messaging service using the Java Messaging Service (JMS) in a Spring application. The JmsOrderMessagingService class implements the OrderMessagingService interface and provides a sendOrder method to send TacoOrder objects to the "tacocloud.order.queue" destination using the convertAndSend method of the jms object.

2. An exemple of Kafka messaging implementation:

@Service
public class KafkaOrderMessagingService
                                  implements OrderMessagingService {
  
  private KafkaTemplate<String, TacoOrder> kafkaTemplate;
  
  @Autowired
  public KafkaOrderMessagingService(
          KafkaTemplate<String, TacoOrder> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
  }
  
  @Override
  public void sendOrder(TacoOrder order) {
    kafkaTemplate.send("tacocloud.orders.topic", order);
  }
  
}

This code snippet shows how to implement an order messaging service using Apache Kafka in a Spring application. The KafkaOrderMessagingService class implements the OrderMessagingService interface and provides a sendOrder method to send TacoOrder objects to the "tacocloud.orders.topic" topic using the send method of the kafkaTemplate object.

📚 Acknowledgements

This project was created with the help of the book "Spring in Action" by Craig Walls and Ryan Breidenbach. Many of the concepts and techniques used in this project were learned from this valuable resource.

About

This project showcases how to implement OAuth 2 security in a RESTful API using Spring Security. With inspiration from the "Securing REST" chapter in "Spring in Action", this project demonstrates how to create an authorization server.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published