Guide – Spring Boot (Part 02)

Tags

YAML

In the last post we used the application.properties config file, but Spring supports YAML for config too. So, the same config file from the last post (link in the end of the post) would be like this in a application.yml:

server:
    context-path: /
    port: 8081
spring:
    datasource:
        url: jdbc:h2:file:~/conscious_it
        username: sa
        password:
  • IMPORTANT: if there are both files, the priority is .properties

Configurations By Environment

  • Spring has the ability of being separated by environments. It means that I can have development and production configs separately and choose which to run dynamically. This feature is called Profile.
  • To choose which environment to execute, just pass the parameter -Dspring.profiles.active to JVM:
java -Dspring.profiles.active=dev -jar my_spring_boot_project.jar
  • This configuration would search the files:
application-dev.properties
application-dev.yml
  • The profile file pattern is:
application-{profile}.properties (ou yml)
  • In case of running in the IDE, just pass the parameter to the class that executes the SpringApplication.run
  • With Spring, it’s also possible set different implementations using the same parameter (spring.profiles.active) in the JVM. For this you need to use the Spring @Profile annotation.
    • Let’s suppose I have a UploadService interface and I want it to be AWS in production but for development I want it to be File. Using the annotation, it would look like this:

Continue reading

Guide – Spring Boot (Part 01)

Tags

  • Required knowledge of dependency injection. Preferably Spring, but can be CDI as well.
  • “Any time spent writing configuration is time spent not writing application logic” – (WALLS, Craig – Spring Boot In Action)

What it is?

It is a framework that eases the configuration of a Spring-based application. You can download the dependency of Spring Boot and its related projects as any other dependency of Maven / Gradle, but you can create a project configured using the site: start.spring.io/ (Note: For better visualization, click Switch to the Full version.)

Basic Concepts

  • The created project with Spring Boot can be in JAR or WAR format. If it is JAR and Web, Spring Boot starts an embedded Tomcat each time it is started.
  • Spring Boot needs a class with a method main that executes the method SpringApplication.run
  • By default, The @SpringBootApplication annotation is used in this class. Basically it is an annotation that adds other annotations:
    • @Configuration – It marks the class as a bean definition source.
    • @EnableAutoConfiguration – Add some beans based on Classpath and other settings.
    • @ComponentScan – Tells Spring where to look for other components, configurations, and services.
    • @EnableWebMvc – If spring-webmvc is on classpath, this annotation is also added, with all Spring MVC configuration.
  • If the class that runs SpringApplication.run (let’s call it MainApplication) is in the com.conscious.it package, it’s not necessary to create the beans with default constructor. Spring maps all packages and subpackages from MainApplication package.

Continue reading

Tracks – You might have lost 01

Tags

Professional Career

Good Practices

DevOps Related

Tests

Web

Improving the Knowledge

Tips

How to measure performance – Java

Tags

, ,

How to know how much the app is consuming from memory? Where is the application bottleneck? How are the application Threads? How many classes are being loaded? How many instances are being created?

You can answer these questions by monitoring the JVM and using Profiling (this is the process of monitoring parameters at the JVM level, such as Method Execution, Thread Execution, Object Creation, and Garbage Collection).

In the post about testing tools (Let’s try something different: Testing Tools 01) it was possible to measure if the application tolerates X accesses, the response time of the application and etc. Now let’s measure HOW the JVM is reacting to this.

For this example, we will use VisualVM because it is free, easy to use and has a good amount of information, and we will also use the java-sample-project project. The use of VisualVM is very simple: when downloaded, you unzip it and execute the visualvm file inside bin folder. Follow the guidelines in the Java Sample Project to run the project. When VisualVM and the project are started, VisualVM should look like this:

Continue reading

Early Access: JUnit 5

Tags

, , ,

I was testing one of the news that are about to be released: JUnit 5. The first impression is that it has a lot of cool functionalities, such as parameterized tests, improved Exception assert, grouping tests by Tags and etc.

I created a branch of my java-sample-project project using some JUnit 5 features with Spring Boot and I will summarize just some of the features:

Parameterized Tests

  • It gives you the ability to run the same test with different parameters, coming from different sources: Inline Values (String, Integer, etc.), Enum, CSV, Methods…
  • In my project, I used @MethodSource to exemplify:

  • With this, the test will search for a method called createUsers that returns a Stream<Arguments>:

  • The expected parameters in my test method are: a User and an Integer, and in that Stream I’m passing both. The test will run first with userOne () and 1, and then with userTwo () and 2.

Exceptions Assert

  • With JUnit 4 it was possible to test exceptions, both with @Test(expected=…) and Exception Rule (ExpectedException). This last one could still validate the exception message. But I believe nothing compares to this assertThrows:

Continue reading

Let’s try something different: Testing Tools 01

Tags

, , , ,

For testing tools and some other examples, I recommend one of these projects: python-sample-project ou java-sample-project, pretty simple projects for testing easily and quick.

Locust

  • A simple load testing tool, but powerful. You write Python code on tests, so the flexibility is a little bigger.
  • Prerequisites:
    • Python 2.7.4+ or 3.3+

Pros:

  • Some flexibility on tests
  • Easy to Install
  • Easy to Configure
  • Easy to Use
  • Multiplataform

Cons:

  • Although showing the main information, it could show more useful information and / or charts
  • The test never stops unless you click to
  • Tests HTTP calls but doesn’t test flows as a real user would do in a browser

Result Example:

locustio

SOURCE: http://locust.io/

Link: http://locust.io/

Httperf

  • It’s a simple but also powerful performance testing tool.

Pros:

  • Easy to Install
  • Easy to Use
  • Great amount of info
  • Great amount of customization of calls
  • Can limit the total of requests

Cons:

  • Linux Only
  • Tests HTTP calls but doesn’t test flows as a real user would do in a browser (but it is not what it proposes to do)

Result Example:

httperf

Source: http://www.labs.hpe.com/research/linux/httperf/httperf-man-0.9.pdf

Links: https://github.com/httperf/httperf and https://linux.die.net/man/1/httperf

In my list of tools, the next to be tested is Multi-Mechanize. If you have any suggestions, send me.

Why do you do what you do?

Tags

, ,

It seems like a simple question, but you need to think hard about why we work with what we work with. As a developer, I see a lot of people saying they program because of the money. Others say they program because of the number of jobs they have and so on.

Why am I asking you this? After all, I myself entered the programming area because I did not go through electrical engineering and Computer Science was one of the few courses where I was not very life-threatening (ironically, it is one of the most stressful ones to me, which apparently is not very good for health).

The first thing to be a good professional, regardless of the area, is to love what you do. When you love what you do, you dedicate yourself, you study, you want to improve, because you love what you do.

“Choose a job you love, and you will never have to work a day in your life.” (Confucius)

Another point I would like to highlight is: seek professional happiness. You may love your profession but work in a place that does not give you the satisfaction you need to perform well. Maybe, problems with the boss, problems with the company’s culture, maybe even salary. We know that there is no perfect company, but if you are not happy, have talked and not resolved, do not be afraid to seek your happiness.

The second last point is: do not be afraid to change, be afraid to stand still. It has happened that I am doing what I like, in a place I like and not evolving, and I do not mean job levels or positions in the company, I mean knowledge, professional growth, knowing more and more. Some people like routine and always doing the same thing, but the fact that you are not evolving has to bother you. You love what you do, you have to feel like learning more and more.

The last and not least point is: always do your best. Depending on the work area, the chance that you will work with someone who has worked with you is very large. Your professional image tells a lot about you. Do not be known as the person who “even worked, but …”. Rest assured that in all your work you have given your best and do not be afraid to be happy.

IDE – Visual Studio Code

Tags

Some people ask me which IDEs I use to develop. Usually I used to use an IDE for each language: Eclipse for Java, PyCharm for Python, and so on… I tried to see a generic “IDE” (like a code editor with plugins) as Sublime and Atom. I always read stories that said they are good but heavy.

I tested Sublime, even liked it, but I thought that wouldn’t worth to buy or keep using. I downloaded Atom and the size already scared me: on that time was almost 130 MB or something close to that. I didn’t even install Atom when I heard a friend recommeding me Visual Studio Code. I got curious and I downloaded.

I had a surprise right away: at the time, 40 MB to download. By installing, I already liked the interface, the ease and the native support for Git. I used and enjoyed it, it did not take up much memory and I got carried away. I started to use other languages that I usually program and I was downloading plugins. I accidentally press a Hotkey I used in Eclipse and suddenly open a terminal inside the VS Code. That’s it. It was all I needed.

Continue reading

DevOps

Tags

, , , , , ,

What it is?

DevOps is an integration culture between Development and Operations, aiming that both participate together on the entire software lifecycle (Software Development Life Cycle – SDLC). Some of the main focuses of DevOps: better-quality software, collaboration between Dev and Ops and continuous improvement.

Directly Associated Practices

  • Cloud Computing (AWS, Azure, Google Cloud)
  • Infrastructure as a Code (Chef, Puppet, Ansible, SaltStack)
  • Automated Tests
  • Builds and Continuous Integration (Jenkins, Bamboo, CircleCI)
  • Continuous Delivery

Indirectly Associated Practices

Some practices that end up also being associated with DevOps:

  • Containerization (Docker, RKT)
  • Continuous Deployment

What DevOps is NOT?

  • A tool or just the practices.
  • Just automation.
  • Job Title.
  • Just about Dev and Ops.

Why to adopt DevOps?

  • Continuous and quick Feedback
  • Better quality
  • More reliable releases
  • Shorter TTM (Time-To-Market)
  • Better collaboration between teams
  • Continuous Learning
  • Continuous Improvement

More Knowledge

Free Course about DevOps: https://www.udacity.com/course/intro-to-devops–ud611
Podcast (BR): https://blog.lambda3.com.br/2016/10/podcast-12-devops/
Podcast (EN): https://www.arresteddevops.com/what-is-devops/
Reading: http://www.agileweboperations.com/what-devops-is-not
Reading: http://gutocarvalho.net/octopress/2013/03/16/o-que-e-um-devops-afinal/
Book: http://shop.oreilly.com/product/0636920039846.do

Programmer Utilities – Java Tools 01

Tags

,

Spring Loaded

Main Function: Refresh Java code without restarting server.

Similar: JRebel.

URL: https://github.com/spring-projects/spring-loaded

  • To use Spring Loaded, you have to download the jar and add the following parameters on JVM:
-Djavaagent:ABSOLUTE_PATH/springloaded-1.2.5.RELEASE.jar -noverify
  • On the servers below, the parameters ALSO must be included:
    • JBOSS
-Djboss.modules.system.pkgs=org.jboss.byteman,org.jboss.logmanager,org.springsource
-Djava.util.logging.manager=org.jboss.logmanager.LogManager 
-Xbootclasspath/p:${JBOSS_HOME}/modules/org/jboss/logmanager/main/jboss-logmanager-1.2.0.GA.jar 
-Xbootclasspath/p:${JBOSS_HOME}/modules/org/jboss/logmanager/log4j/main/jboss-logmanager-log4j-1.0.0.GA.jar 
-Xbootclasspath/p:${JBOSS_HOME}/modules/org/apache/log4j/main/log4j-1.2.16.jar
  • Wildfly
-Djboss.modules.system.pkgs=org.jboss.byteman,org.springsource

After that, you only need to start the server in Debug Mode and any change on Java Code that is not on method definition or new method/class, you won’t need to restart. It’s recommended to disable the server’s Hot Deploy (Never publish automatically).

PODAM

Main Function: Fill a class (and its dependencies) with random data.

URL: http://devopsfolks.github.io/podam/

Main Functionalities: In addition to being able to fill a class, you can define strategies to generate your data, it supports Bean Validation, generating data for example: Numbers between 3 and 5 (@Min and @Max), and more. Continue reading