Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, September 16, 2020

JDK 15


Java is finally getting interesting again since JDK 8 thats the first time I wish to be using a new JDK in production. I'm really looking forward to JDK 16 and to the future. JDK 15 has lots of interesting things such as Records, Pattern Matching for instanceof, EdDSA algo, Text Blocks, Local classes and interfaces, Sealed classes and interfaces, ZGC, FMA, and much more.  So Let's get started and take a look at a quick video I made to show Java 15 working with Maven and Idea. 



The Video

The Code

https://github.com/diegopacheco/java-pocs/tree/master/pocs/java-15-fun

Cheers,

Diego Pacheco

Monday, August 3, 2020

Java Mutation Testing with Pitest

Line by Line coverage can be very tricky. Many companies rely on line coverage as metric signals however you can easily have waste with that kind of metric like Getters/Setters, Database Calls, etc. The other issue is that kind of tools can be easily gamed.  The mutation test is not a new idea, it appears in the 70s. The main idea behind mutation testing is to test your Tests. Therefore harden your application solution and tests. The mutation test has the concept of mutators. Mutators are bugs and they should be KILLEd if they SURVIVE it means you have bugs in your code that your tests are not catching. Mutants are much harder to fool unlike line coverage tools. Today I want to share a hands-on Refactoring video I made using Pitest.  So Let's get started.
Video


Code


Cheers,
Diego Pacheco

Sunday, August 2, 2020

Java Object Creation Patterns

Java is an old language. 25 years now. Java has several limitations and issues however java is the robust and pretty well-stablished solution for backend, databases, Caches, Search Engineers,  IoT, and much more. Today I want to share some patterns about object creation. I highly recommend you read Effective Java book, which was a huge inspiration for this blog post. So I want to show you guys some useful patterns like Static Factories, Builder, Singletons and Immutable Objects.  So I made a quick video with code, let's get started. 



Video


Code


Cheers,
Diego Pacheco

Monday, July 27, 2020

Quarkus with Panache

Quarkus is a super interesting project.  IMHO Quarkus is JEE done right. It's super-fast, relies on Netty, uses GraalVM. Today I want to share a video I made about Quarkus with Panache using the Postgres database running on Docker. So Let's get started. 






Video


Code


Cheers,
Diego Pacheco

Spring-Boot-2 with Kotlin

Kotlin is a Jetbrains language that has lots of inspiration from Scala. Spring-boot-2 is a popular solution for microservices like Quarkus and Micronaut. Today I want to share a simple video I made so you can get started with Maven, Spring-Boot-2, and Kotlin. If you are new to Kotlin I also recommend seeing my previous post about the language. Okay, let's get started. 





Video


Code


Cheers,
Diego Pacheco

Kotlin 101

Kotlin is a JVM language created by JetBrains in 2011. Google declared the official preferred language for Android in 2019. Kotkin is heavily inspired in Scala. Kotlin claims to have the good parts of scala without having the bad parts. It's also possible to see some Python and Groovy influence as well. Kotlin makes java much less verbose and has awesome support in sense of IDE thanks to IntelliJ. I'm a Scala guy and prefer scala, however, kotling is an interesting choice especially if you are making android native apps. So today I want to share a video I made with a good walkthrough through several features of the language in a Maven project. So Let's get started!

Video


Code


Cheers,
Diego Pacheco

Friday, July 24, 2020

Property Based Testing with Jqwik

Property-Based Testing in a really nice functional programing way todo testing. Originally created at Haskell with QuickCheck. Today there are frameworks for pretty much all languages. Jqwik is a property-based testing framework for java.  Property-based Testing is cool because you focus on a predicate / boolean expression and forget about inputs. When I say forget about, thats happen because the property based testing takes care to generate all possible inputs for you and you can save lots of typing, creating uses cases. You still need to work in your property/assertions however you end up writing much less code and cover a much wider range of possible inputs.  Today I want to share a video I made about it, so let's get started.


Video


Code


Cheers,
Diego Pacheco

Immutables Builders

Fluent Interfaces(Internal DSL) are a nice and sweet way to code. However, make a builder in Java all the time can be a bit verbose and time-consuming. Immutables Builders is a very interesting library that can help us with the heavy lifting.  Immutables can generate builders which are Type-safe, null-safe, and thread-safe. They are super easy to use and serialization ready. Immutables make your code clean and fun. So today I want to share a java video made about Immutables Fluent DSL in Action. So Let's get started. 






Video


Code


Cheers,
Diego Pacheco

Swagger + Spring Boot 2

Swagger is a great API documentation tool. Today I will show how we can generate Swagger Documentation based on a Spring-Boot-2 Application. We will be using SpringFox. Swagger is interesting because you have a very useful contract for services/microservices. It's possible to generate code from that spec and also invoke the services. Spring-Boot is a popular java framework for services/microservices the integration between to solution is more than ideal and SpringFox does that for us effortless. So Let's get started!








Video


Code


Cheers,
Diego Pacheco

AssertJ

Everybody write tests or we should at least. Assertions are used in lots of different kinds of tests such as Unit Test, API Test, Integration Testing, E2E Testing, Contract Testing, Stress/Load testing. Unit tests, Integration, and Contract tests can often be done with Junit. However, assertions sometimes can be a bit verbose and you might need to write some extra code in order to test what you are required to test. Today I want to share a video about a library for Java called AssertJ. AssertJ is Assertions fluent API.  AssertJ has integration with other java libraries like Guava, JodaTime, and others. Today I want to share a video I made about it. So Let's get started. 
Video


Code


Cheers,
Diego Pacheco

Hashing in Java

Hashing is an interesting technique we need to use in standard java solutions such as services and microservices. SHA-256 is a secure hash algorithm for data you want to hash. Often this technique can be used for Passwords or high entropy PII Data.  For this blog post, I want to share with you guys how we can create secure hashes in java using SALTS. We will be experimenting with 4 flavors of Hashing(SHA-256) being: Standard Java API, Guava, Apache Commons Codec, and Bouncy Castle. Today we will see a Java video I recorded going through the API. So Let's get started.





Video


Code


Cheers,
Diego Pacheco

Tuesday, July 21, 2020

Micronaut

Micronaut is a JVM(Java, Groovy and Kotlin) framework similar to Spring Boot or Quarkus. Micronaut is modular and blazing fast. It has a minimal memory footprint and is a bit cleaner API compared with Spring Boot for instance. Micronaut does Reflection based IOC(with Cache). Micronaut also has GraalVM support. Micronaut is a non-blocking IO and has a Reactive API based on Netty and Rectivex projects. Today I made a quick video going trought the Micronaut API DEMO I made. 


Video


Code


Cheers,
Diego Pacheco



Thursday, July 2, 2020

Serde Benchmark

Serialization is always an important aspect of any Microservice or solution. Often people associate Benchmarks as potential flame-war enablers, thats not my goal here. Benchmarks are sensible things, they really vary from config, hardware, use case, payload. I'm trying to convince you guys of anything. I'm Just sharing some simple comparison exercise I did so you guys have any idea about performance to Serialize and Deserialize and the resulting size. I highly recommend you do this benchmark for you use case. However, my pet project here might give you some ideas on how to get started. The benchmark I will show is coed in Java but you can take this ideas and do them in any language. I will use Junit as way to run the benchmarks. So let's get started. 

Friday, June 26, 2020

Debug Tricks

Debugging is an important tool. When it's possible to debug you have more chances to figure out the problem faster. Unfortunately not all issues are created equally and sometimes you cant debug. When you need to debug something is important to have your debug game sharp. Several engineers might consider the topic basic but as a consultant, I saw multiple times overtime people dont dominate the tools. Dominate and master your tools is important not only for productivity but also to improve your developer experience and make your life easier as an engineer.  I used Eclipse for a long time. For the last 3 months I decided to give IntelliJ a chance. A month ago I made a video about some killer stream debug trick and also how to do some configs. Today for this post I want to show some other IntelliJ Debug tricks which will make your life easier and will make debug much better and productive.


5 Java Debug Tricks Video


I hope you guys like it. 

Cheers,
Diego Pacheco 

Monday, May 18, 2020

Refactoring to Optional

When we right software we need to worry about so many things. For instance: Performance, Scalability, Security, and many other important concerns. There is also a need to worry about corner cases, error paths, and conde readability from other engineers. Which is not an easy task. So should we return null or not? Should we throw an exception or not? Null is considered to be a Billion of dollars mistake. Languages have some concepts to help us like Haskell has the Maybe Monad. Scala has Option. Rust has Result. Java has Optional. Since version 8, Java improved a lot and there is still a long head ahead but slowly getting there. Today I decided to do something different with you guys, let's do a simple refactoring together in order to improve our code and understanding. So there is a video showing from zero to a hero like code step-by-step. This is the first refactoring video I make, hopefully, it will be interesting for you guys. Without further due, let's get started! 


Video




Full code here on my github.

Cheers,
Diego Pacheco

Sunday, May 10, 2020

Feature Enrichment with Spring Data Redis and Lettuce

Lettuce by far is the best Redis client out there for Java. In the java world, Annotations and Mappings are pretty popular. I build services using these concepts before however they were always under the service boundaries, never as shared libraries in order to avoid coupling. So today I record a video showing how we can use this feature and also extend the functionality to add custom enriched behavior.  We will be using Spring Boot 2, Spring Data Redis, and Lettuce as redis Client. We also will need to have redis running either as standalone / cluster or running on docker. We will create custom annotations in order to describe the behavior we want. I need to say that some people love and other people hate annotations, I have mixed feelings. Often for those that don't like it - check this out. Besides that, let's take a look. So Let's get started.


Video



Code

Model

Our model is pretty simple. Simple Person POJO with Getters/Setters, toString, equals/hashCode all generated by Idea. You can see there is a custom annotation called UpperCase. The idea is to add custom features enrichment and make sure some fields are stored in the Upper case.



Annotation

Now let's look at the annotation.



Custom Repository

Here is where the magic happens. I wrapped all CurdRepository operations and I'm delegating to the proper CrudRepository implementation at the end of the day. However I', also I have a reflection code that adds the feature enrichment I want based on the existence of the UpperCase annotation at the Entity fields.



The complete code is here.

Cheers,
Diego Pacheco

Monday, May 4, 2020

HMAC in Java

In 2018 AWS CTO(Vogels) said: "Security is everyone's job". For the last 2 years, there was so many famous and big data leaks and breaks that made that statement be very true more than ever. Security often could mean worst performance and worst user experience so in order to get it right you really need to think about the designs before jumping into to code and consider performance and user experience has main requirements.  I was thinking about writing about how to do HMAC in Java for a while and recently Redis 6.0.0 come out and to my surprise, there was a refactoring on the password part in order to use HMAC.  Security easily could scare engineers often because it is not something well spread yet but I believe this will change soon. It does not matter if you have to deal with PII Data or not, security is super relevant because everybody is running their workloads at the cloud or with IoT and Edge devices which means more distribution, more code, more points of failures, and therefore more attacking vectors.  HMAC security depends on the security of the underlying has a function but HMAC is more secure, it will be hard to lunch successful collision attacks on it because of the secret key. So before I talk more about how HMAC works and show the java code let's understand why we would use HMAC.
Why use HMAC

As you might need to deal with PII(Personal Identifiable) Data like SSN(Social Security Number), Address, Email, Name, Last Name, login credentials, and Passwords. For security reasons, you might need to store data at Rest(Databases) and Data at Transit(TLS / mTLS). So why should I consider HMAC instead of Symmetric or Assimetric keys? Well, there is a couple of reasons, for instance, let's consider passwords, why we should know the plaintext for a password? It does not sound like a good idea for security reasons. Another use case could be cookies. Using HMAC is interesting because as you might need to do key rotations since HMAC is a HASH(there is no way we are getting the original value back) would not make sense to rotation it or to re-generate the hash. HMAC is used in scenarios where you want to check for "Integrity" and "Authenticity". HMAC is also used on IPSec(VPNs), TLS, and Json Web Tokens and Bitcoin(Hashcash).

How HMAC works?

So basically HMAC works this way. There is a Cryptographic Hash Function and Cryptographic Hash Key. The HMAC is associated with a Hash function like SHA-256 or SHA-3 so the HMAC carries the name of the function used like HMAC-SHA-256 or HMAC-SHA-3.  

HMAC is a 2 phase computation. First, the secret key is used to derived 2 secret keys(inner and outer keys). 

In the first phase, the algorithms produce an internal hash derived from the Message and inner key.

The second phases produce the final HMAC code derived from the inner hash and the outer key. This algorithm provides immunity against Length Extension Attacks.  So talking is cheap let's see how we do that in Java. 

Java Code


We are using only java standard library here. Basically, there is just one function because once we produce the HMAC there is no turning back.  So we need to create a MAC instance saying what algorithm we will be using for our case will be: HMAC-SHA-256. Then we need to instantiate a Secret Key Spec, for educational reasons the key is hardcoded "secreT1_" on line 34, that secret should be a cipher and safely stored in places else like Vault or Secured S3.  So after that, we initialize the MAC and we can do the HASH by calling mac.DoFinal. That's pretty much it, as you guys can see is pretty simple. 

The full code is available here.

Cheers,
Diego Pacheco

Saturday, May 2, 2020

Idea: Configs and Stream Debug

I work with Java for a long time. I always used eclipse. I like eclipse IDE. However I need to admit eclipse is pretty much dead, specially scala IDE wish I used a lot but unfortunate is long dead. So IntelliJ Idea is alive and has some interesting features like being alive first of all and also easily I could say performance is pretty good and you dont want need to install lots of plugins. So I made a video showing some configs that will make your life easier if you came from eclipse like me and also share some killer features for Java Streams Debug.
I always like eclipse but I dont have issues using Idea, I believe 2 things. First of all changing editors, IDE is always good for experience and open your mind and secondly, you should not be attache to any language, IDE, editor or tool, these things are tools and there is the best too for the jobs, this tools dont define you and you are bigger than this tools. So the tools should serve you and not the other way around.  So Let's get started!


Show me the code

Here we have a simple stream code in Java 8, however, this stream codes can get pretty messy and hard to debug.

https://gist.github.com/diegopacheco/8d5c3ea0e2e82acdcc5d3fb3ab2d7e23

The complete code is here on my github.

Video


IntelliJ Idea Java: Configs and Stream Debug from Diego Pacheco on Vimeo.

Cheers,
Diego Pacheco

Thursday, March 26, 2020

Cool features you missed from Java 8 to 14

Java is In a different cycle releasing features from very short cycles which are considered for some folks not ideal. IMHO it's great because it faster innovation and make the language alive. There are some folks saying that java is just copying features from other languages but I dont see issues in that, actually this is good.  Java is a very old language 1995, however, it still used a lot at the enterprise and used to run most critical programs like databases, search engines,  distributed log, and message brokers, HTTP servers and several other key components that we use day by day.  Today for this blog post I want to share some features that you might miss from java 9 to java 14. There are some interesting features which could help you every single day.

All the Source code you will see is this blog post was generated using JDK 14 and JShell which will be covered later on this very post. JShell is an amazing feature IMHO the best feature in Java Stream support in Java 8. Lately, I'm doing lots of work around Rust and using Rust in production, however, my background was made by using Java in Production, and still, I have system with Java in production.

Var

Var feature comes from JDK 11 and supports you to create a local variable with less ceremony. What is cool is that you dont need to worry about types, it makes similar like javascript.



Objects.nonNull

Added in Java 8, few folks know it. It's an easy way to check for null.



Although the feature is cool it's it does not cover blank String, so you might still need to use Apache Commons.

Jshell 

JShell was added to Java 9.  IMHO is the best feature from java 8 to 14. It's also known as a REPL(Read Evaluate Print and Loop) which is famous in functional programming languages like Clojure, Scala, Haskell, and now Java. So it's useful to play with the language and also experiment code with less formality and speed.



Switch Expressions

Switch expressions are like Pattern Matcher that exist in functional programming languages like  Scala, Haskell, Rust. In java, it's possible to assign the result of switch expression to a variable.



Text Blocks

Text Blocks are multi-line strings. This makes easier to avoid scaping with strings and automatically formats the string in a predictable way.



Helpful null pointer exception 

NPE was always complicated in Java. Null Pointer is considered a million dollars mistake. Languages like Rust avoid null and is much better. Now when an NPE happens there is a much better exception message letting you know what happens and how to fix the NPE. It would be better to remove NPE but the impact would be much bigger at this point.

a.x.y.z = 99;---Exception in thread "main" java.lang.NullPointerException: Cannot read field "z" because "x.y" is null

Convenience Methods for Collections

Instantiate and populate collections in java was always verbose and a bit wanting. But this gov fixed in Java 9. Now it's super easy to and convenient to instantiate ad populate collections.



I Love this feature, Scala had it for a Long time. It makes the developer experience much much better.

Records

One of the best features that Java stole from Scala. Records are like case classes in Scala.



Records are cool because they generate access methods, toString(), equals() and hashCode().

Why Records? Just for syntactical slugger? No. Records are great for Immutable classes.

So far this are small changes but slowly Java is getting back on track. I wish the changing flow continues and Java keeps adding features, making the language live and hopefully new frameworks and libraries will appear.

Cheers,
Diego Pacheco

Wednesday, March 11, 2020

Running a rich Microservice constellation in Kubernetes

Languages have trade-offs like any solution in tech. Microservices are great for many reasons, one of them allows the best tool for the job. Kubernetes allows us to have the same operational standards and procedures even using different languages and solutions. For this blog post, I want to show a simple project I build which is a constellation of services. There are 5 microservices, written in several languages like Scala, Java, Go, Python and Rust. The services do a pretty basic math operation like (+, - , / , *) and the Scala one does the aggregation and orchestrate the other services using a polish notation algorithm and REST calls.

The Video



Calc Services (5 Microservices Scala, Go, Rust, Python, and Java) running on K8s from Diego Pacheco on Vimeo.

The Source Code

The complete source code is here on my github.

Cheers,
Diego Pacheco

Chuyên mục văn hoá giải trí của VnExpress

.

© 2017 www.blogthuthuatwin10.com

Tầng 5, Tòa nhà FPT Cầu Giấy, phố Duy Tân, Phường Dịch Vọng Hậu, Quận Cầu Giấy, Hà Nội
Email: nguyenanhtuan2401@gmail.com
Điện thoại: 0908 562 750 ext 4548; Liên hệ quảng cáo: 4567.