Book Image

Learning Spring Boot

By : Greg L. Turnquist
Book Image

Learning Spring Boot

By: Greg L. Turnquist

Overview of this book

<p>This practical, accessible guide helps you get up and running fast with Spring Boot. This book starts by crafting a Spring MVC application using the Spring stack on top of Apache Tomcat, with little configuration on from your end. You will also learn how to write both JUnit and Spock test cases. Then, you'll pull back the curtain and see how Spring Boot works by using Spring Messaging (JMS and AMQP) as well as creating custom metrics, custom information, and custom CLI commands aimed at production environments. In the last two chapters, you'll see how Spring Boot supports everyday situations we all deal with. You will learn how to create multiple configurations inside your app that can interact with different data stores.</p> <p>By the end of the book, you'll have a good understanding of how Spring Boot works, how it manages low-level infrastructure, and how to start out production-grade apps with built-in support tools as well as custom ones.</p>
Table of Contents (13 chapters)
Learning Spring Boot
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating custom metrics to track the message traffic


We've created a custom health check and also added custom info about our app. Another useful strategy will be to create some custom metrics. In previous chapters, we saw how Spring Boot's Actuator comes with some out-of-the-box counters and gauges for web activity. For this section, let's count the flow of messages through the system.

Earlier in this chapter, we coded a simulator that would generate events periodically. The following fragment shows an updated version of that constructor to support metrics:

final private JmsTemplate jmsTemplate;
final private String destination;
final private CounterService counterService;

public NetworkEventSimulator(JmsTemplate jmsTemplate, String destination,
                            CounterService counterService) {
    this.jmsTemplate = jmsTemplate;
    this.destination = destination;
    this.counterService = counterService;
}

This constructor adds Spring Boot's org.springframework.boot.actuate.metrics...