Reader small image

You're reading from  Practical Cloud-Native Java Development with MicroProfile

Product typeBook
Published inSep 2021
PublisherPackt
ISBN-139781801078801
Edition1st Edition
Concepts
Right arrow
Authors (5):
Emily Jiang
Emily Jiang
author image
Emily Jiang

Emily Jiang is a Java Champion, a cloud-native architect with practical experience of building cloud-native applications. She is a MicroProfile guru, leading a number of MicroProfile specifications as well as the implementations in Open Liberty. She is a well-known international conference speaker.
Read more about Emily Jiang

Andrew McCright
Andrew McCright
author image
Andrew McCright

Andy McCright is IBM's Web Services Architect with 20 years of experience building Enterprise Java runtimes. He leads the MicroProfile Rest Client & GraphQL projects and contributes to Open Liberty, Jakarta REST, CXF, RESTEasy, and more. He is also a blogger.
Read more about Andrew McCright

John Alcorn
John Alcorn
author image
John Alcorn

John Alcorn is an application modernization architect in the Cloud Engagement Hub, specializing in helping customers modernize their traditional Java EE applications to the cloud. He developed and maintains the Stock Trader application that shows how to build a composite application out of MicroProfile-based microservices in Java. You can connect with John via Twitter.
Read more about John Alcorn

David Chan
David Chan
author image
David Chan

David Chan is a software developer at IBM who works on the observability and serviceability components of the Open Liberty project. He is involved with the MicroProfile project with a specialization in the MicroProfile Metrics component.
Read more about David Chan

Alasdair Nottingham
Alasdair Nottingham
author image
Alasdair Nottingham

Alasdair Nottingham is a software developer and lead architect for Open Liberty, and WebSphere. He has been involved with the MicroProfile and Jakarta EE projects to a varying extent since their inception.
Read more about Alasdair Nottingham

View More author details
Right arrow

Chapter 10: Reactive Cloud-Native Applications

Up until now, we have mainly talked about traditional cloud-native applications that adopt imperative programming with clearly defined input and output. Imperative programming is the oldest programming paradigm. Applications using this paradigm are built using a clearly defined sequence of instructions making it easier to understand. Its architecture requires that the connection services are predefined.

However, sometimes, a cloud-native application does not know which services it should call. Its purpose might be just sending or receiving messages or events and staying responsive and reactive. Thus, imperative programming no longer applies to these kinds of applications. Under such circumstances, you will need to rely on reactive programing and use an event-driven architecture to achieve reactive, responsive, and message-driven applications. We will discuss reactive cloud-native applications in this chapter.

First, you will learn...

Differentiating between imperative and reactive applications

When developing imperative applications, the application developers define how to perform a task. You may design a synchronous application to start with. However, to deal with heavy loads and improve performance, you might think about switching from synchronous programming to asynchronous programming to speed up by performing multiple tasks in parallel. When using synchronous programming, on hitting block I/O, a thread has to wait, and no other tasks can be performed on that thread. However, in the case of asynchronous programming, multiple threads can be dispatched to perform other tasks if one thread is blocked.

Asynchronous programming dispatches multiple threads but it does not fix the blocking I/O issues. If there are blockages, eventually the application will consume all threads. Consequently, the application will run out of resources. One of the characteristics of imperative programming is that one application needs...

Using MicroProfile Context Propagation to manage context

MicroProfile Context Propagation (https://download.eclipse.org/microprofile/microprofile-context-propagation-1.2/) defines a mechanism for propagating context from the current thread to a new thread. The types of context include the following:

  • Application: This normally includes the thread context class loader as well as java:comp, java:module, and java:app.
  • CDI: The scope of the CDI context, such as SessionScoped and ConversationScoped, is still active in the new unit of work, such as a new CompeletionStage.
  • Security: This includes the credentials that are associated with the current thread.
  • Transaction: This is the active transaction scope that is associated with the current thread. This context is not normally expected to be propagated, but cleared instead.

Apart from the aforementioned context, an application can introduce custom context if needed.

To propagate the aforementioned context, this...

Using MicroProfile Reactive Messaging to build a reactive application

MicroProfile Reactive Messaging 2.0 (https://download.eclipse.org/microprofile/microprofile-reactive-messaging-2.0/) provides a mechanism for building event-driven cloud-native applications. It enables decoupling between the services via messaging. MicroProfile Reactive Messaging provides the @Outgoing annotation for publishing messages and @Incoming for consuming messages. The following figure illustrates how messages travel from the publisher (Method A) to the consumer (Method B). The message can be sent to a messaging store, such as Apache Kafka, MQ, and so on, and will then be delivered to a consumer such as Method B:

Figure 10.1 – Messaging flow

In Reactive Messaging, CDI beans are used to produce, process, and consume messages. These messages can be sent and received via remote brokers or various message transport layers such as Apache Kafka, MQ, and so on. Let's discuss...

Summary

In this chapter, we have learned the differences between imperative and reactive applications. We discussed briefly how to use MicroProfile Context Propagation to propagate contexts for asynchronous programming and then covered MicroProfile Reactive Messaging concepts to discuss how to use Reactive Messaging to create a reactive cloud-native application. Through this chapter, you will now be able to connect the built application with your chosen messaging technologies such as Apache Kafka. You also now understand that whenever you need to create a message-driven application, you should consider using MicroProfile Reactive Messaging.

In the next chapter, we will cover MicroProfile GraphQL to learn how to use GraphQL in your cloud-native applications to improve performance if you need to frequently execute queries.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Practical Cloud-Native Java Development with MicroProfile
Published in: Sep 2021Publisher: PacktISBN-13: 9781801078801
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (5)

author image
Emily Jiang

Emily Jiang is a Java Champion, a cloud-native architect with practical experience of building cloud-native applications. She is a MicroProfile guru, leading a number of MicroProfile specifications as well as the implementations in Open Liberty. She is a well-known international conference speaker.
Read more about Emily Jiang

author image
Andrew McCright

Andy McCright is IBM's Web Services Architect with 20 years of experience building Enterprise Java runtimes. He leads the MicroProfile Rest Client & GraphQL projects and contributes to Open Liberty, Jakarta REST, CXF, RESTEasy, and more. He is also a blogger.
Read more about Andrew McCright

author image
John Alcorn

John Alcorn is an application modernization architect in the Cloud Engagement Hub, specializing in helping customers modernize their traditional Java EE applications to the cloud. He developed and maintains the Stock Trader application that shows how to build a composite application out of MicroProfile-based microservices in Java. You can connect with John via Twitter.
Read more about John Alcorn

author image
David Chan

David Chan is a software developer at IBM who works on the observability and serviceability components of the Open Liberty project. He is involved with the MicroProfile project with a specialization in the MicroProfile Metrics component.
Read more about David Chan

author image
Alasdair Nottingham

Alasdair Nottingham is a software developer and lead architect for Open Liberty, and WebSphere. He has been involved with the MicroProfile and Jakarta EE projects to a varying extent since their inception.
Read more about Alasdair Nottingham