Posts

Spring Cloud Contract

Image
Spring Cloud Contract  is a project that, simply put, helps us write Consumer-Driven Contracts (CDC). This ensures the contract between a Producer and a Consumer, in a distributed system – for both HTTP-based and message-based interactions. Producer – Server Side For our producer side, we'll need the spring-cloud-starter-contract-verifier dependency. Producer Side Setup Test stubs are in /src/test/resources/contracts/ package. When the build is run, the plugin automatically generates a test class named ContractVerifierTest that extends our BaseTestClass and puts it in /target/generated-test-sources/contracts/. The names of the test methods are derived from the prefix “validate_” concatenated with the names of our Groovy test stubs. For the above Groovy file, the generated method name will be “validate_find_provider_by_id()”. Consumer – Client Side The consumer side of our CDC consume stubs generated by the producer side through HTTP interaction to maintain the contract...

Jenkins docker image with docker inside

Image
  Jenkins  is an open source, Java-based automation server that offers an easy way to set up a continuous integration and continuous delivery (CI/CD) pipeline. Continuous Integration (CI) is the process of automating the build and testing of code every time a team member commits changes to version control. Continuous Delivery (CD) is the process to build, test, configure and deploy from a build to a production environment.    Jenkins docker image with docker inside Step 1/ Create Dockerfile extending the official Alpine image. Then get jenkins.war and docker from official sites. Step 2/ Jenkins admin user setup to set a default admin user and password, by creating a Groovy script file. Step 3/ Adding Plugins to auto install some selected plugins. Step 4/ Build Your Custom Jenkins Image Here  is a fully functional Jenkins server.

How can I make reflection work on JDK 16 and later?

Image
  Reflection is another form of meta-programming, and just as valid, as the type-based parameters that you see in most languages these days. Reflection is powerful and generic, and reflective programs are a high order of maintainability (when used correctly, of course) and more so than purely object-orientated or procedural programs.  Reflection is fantastic for building tools for developers. With the advent of Java 9  the approach  to encapsulate has changed and the scary-sounding message appeared: WARNING: An illegal reflective access operation has occurred   WARNING: Illegal reflective access by ReflBytecodeName (file:/Users/ben/projects/books/resources/) to method sun.invoke.util.BytecodeName.parseBytecodeName(java.lang.String) WARNING: Please consider reporting this to the maintainers of ReflBytecodeName   WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations   WARNING: All illegal access operation...

Java Bean IBAN Validation - Creating custom constraint annotation

Image
  Although Bean Validation has plenty of handy built-in constraints, such as to make sure if a field is following a specific regex or even if a date is in the past or future, it is certain that soon or later you will need a specific constraint that is not covered by the standard annotations.  Definition of ConstraintValidator Definition of Constraint Custom constraints are at the heart of JSR 303 Bean Validation flexibility.

Triggering Bean Validation programmatically

Image
Validation in Spring Boot applications can be done in many different ways. Why validation? Validating incoming request data is good to reject nonsense as early as possible. Persistence layer validation should only be used as additional layer of safety. I prefere validation programmatically even if triggering Bean Validation programmatically takes a bit more effort. I think it is usually the most flexible way. Validation is done by using the Bean Validation API. The reference implementation for the Bean Validation API is Hibernate Validator. Triggering Bean Validation programmatically Validating extends SelfValidating class

Consider a builder when faced with many constructor

Image
Static factories and constructors share a limitation: they do not scale well to large numbers of optional parameters. Traditionally, programmers have used the telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameter, a third with two optional parameters, and so on, culminating in a constructor with all the optional parameter. We can use alternative when you’re faced with many optional parameters in a onstructor is the JavaBeans pattern, in which you call a parameterless constructor to create the object and then call setter methods to set each required parameter and each optional parameter of interest. This pattern has none of the disadvantages of the telescoping constructor pattern. Unfortunately, the JavaBeans pattern has serious disadvantages of its own. Because construction is split across multiple calls, a JavaBean may be in an inconsistent state partway through its construction. Luckily, there i...

Monitoring Spring Boot Application with Prometheus and Grafana

Image
 Every application that is deployed on production needs some kind of monitoring to see how the application is performing. This will give you some insights on whether the application is performing as aspected or if you would need to take some action in order to obtain the desired level of performance. In the modern world, this data is called Application Performance Metrics (APM).   Let’s try to set up a basic Springboot App monitoring with a Grafana Dashboard and Prometheus.