Monday, August 22, 2016

Install a Local Instance of AWS DynamoDB



STEP 1:  Press Command+Space and type Terminal and press enter/return key.
STEP 2:  Install Brew(If you don't already have it installed.)  Run this command in the Terminal app.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
and press enter/return key.
Wait for the command to finish.
STEP 3:  Use Brew to install and download dynamoDB so that you can run it locally.
brew install dynamodb-local

Launch your DynamoDB local instance like this:
To have start dynamodb-local now and restart at login as a background service using brew services:
  brew services start dynamodb-local
Or, if you don't want/need a background service you can just run:
  /usr/local/bin/dynamodb-local

You can then access a JS Shell in a browser such as Chrome:
URL to hit the Javascript shell for your locally running DynamoDB instance:
http://localhost:8000/shell/

This is what you will see what you load up your DynamoDB Shell.  If you see this, then you are successfully running a local instance of DynamoDB!

AWS Local DynamoDB Javascript Shell in Browser
AWS Local DynamoDB Javascript Shell in Browser


Common DynamoDB Concepts
  • Table:  A collection of data
  • Items: An item is a group of attributes that is uniquely identifiable among all of the other items.
  • Attributes: An attribute is a fundamental data element, something that does not need to be broken down any further.
  • Primary Key
    • Partition Key:  Composed of one attribute known as the partition key
    • Partition Key and Sort Key: Composite primary key, composed of two attributes.

Datatypes
Scalar
  • Number:  String
    • Example:  "N": "123.45"
  • String:  String
    • Example:  "S": "Hello"
  • Binary:  Base64-encoded binary data
    • Example:  "B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"
  • Boolean:  Boolean
    • Example:  "BOOL": true
  • Null:  Boolean
    • Example:  "NULL": true
Multi-value
  • String Set:  Array of Strings
    • Example:  "SS": ["Giraffe", "Hippo" ,"Zebra"]
  • Number Set:  Array of Strings
    • Example:  "NS": ["42.2", "-19", "7.5", "3.14"]
  • Binary Set:  Array of Base64-encoded binary data
    • Example:  "BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]

Document
  • List: array of AttributeValue objects
    • Example:  "L": ["Cookies", "Coffee", 3.14159]
  • Map:  String to AttributeValue object map
    • Example:  "M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}

Tuesday, August 2, 2016

Suppress FindBugs Warnings in a Java and Spring Boot Web Application using Gradle

How to Suppress FindBugs Warnings using Annotations in a +Spring Boot and +Java Application



If your build is breaking because of a FindBugs issue and it is a false-positive or you are unable to resolve the issue because of other considerations, you can add an Annotation to ignore the Findbugs warning.


Update your Gradle Dependencies


You will want to add the following compile time dependency to your build.gradle file.

compile group: 'findbugs', name: 'findbugs', version: '1.0.0'

dependencies {

    compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}


Get the Findbugs Issue ID

You will need a specific ALL_CAPS identifier so that FindBugs knows what bug to ignore.


Locate the FindBugs Report


In your build message, you will see a link to the findbugs report:



You can also find the report in your build artifact.  Right-click and open in your preferred browser to view file.
find bugs html report in build artifact
Open the findbugs html report from the build directory


Open the FindBugs Report


Once you have it, open the Findbugs HTML report in a browser.  It should look like the following.

findbugs html report for java issues
Find Bugs HTML Report

Get the FindBugs identifier


Copy and paste the FindBugs identifier.  It should be in ALL_CAPS.

You will use the annotation like this:
@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")

Add the Annotation to Your Code


Be sure that the correct findbugs SuppressWarnings annotation is being used.

import edu.umd.cs.findbugs.annotations.SuppressWarnings;


Add Suppress warnings annotation


Add this Suppress warnings annotation above the offending line of code.  There are multiple versions of this annotation.

Be sure to use the specific FindBugs issue ID as a parameter in the annotation.


Be sure to use the annotation specific to findbugs in the package edu.umd.cs.findbugs.annotations.

Suppress warnings annotation
Use the edu.umd.cs.findbugs.annotation version of @SuppressWarnings


This is the annotation you will want to add above the offending line of code.

@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")



Re-run the Build to Verify that the Warning is being Suppressed


You want to ensure that the warning is being ignored correctly.  You may want to add a TODO as well if you intend to fix the issue at a later point as well or at least track the issues you are suppressing in some way.

Your build should run successfully without issuesmoving forward now.



Further info:




Saturday, December 5, 2015

Whirlwind Tour of JavaOne and Oracle Openworld 2015

JavaOne 2015 Roundups and Summary Articles



I recently had the wonderful opportunity of attending JavaOne and Oracle Openworld 2015 and have extensively blogged about the social and technical aspects of the conference!

Get a taste of the many events and exhibits on display and an insider's view on the conference.  I will also be presenting the most important takeaways regarding the future direction of Java including an in-depth discussion of changes coming in Java 9 and how to best prepare for those upcoming changes.

If you are local to the Richmond,VA then check out our meetup on the topic!




JavaOne 2015 Day 1 Roundup




JavaOne 2015 Day 2 Roundup




JavaOne 2015 Day 3/4 Roundup





More Pictures of JavaOne 2015



You can find more pictures of the event in our JavaOne 2015 Album here: JavaOne 2015

We had a great time celebrating 20 years of #java at the JavaOne Conference and Oracle OpenWorldBig thanks to Atlassian for providing refreshments at Duke's Cafe and Buzzhouse#JavaOne #JavaOne2015 #oow15

Posted by Ippon Technologies USA on Tuesday, November 3, 2015

Whirlwind tour of Javaone Meetup





Slides





Link to slides: Slides for RJUG JavaOne meetup

Friday, September 11, 2015

Running a new jHipster Application using Compass and the main CSS file is not loading

How to fix a Newly Created jHipster Application not Loading the Main CSS File 


If you see an error similar to the following upon first startup of a jHipster application you have just created:

Failed to load resource: the server responded with a status of 404 (Not Found) http://127.0.0.1:8080/assets/styles/main.css


Issue loading main.css on jHipster application
jHipster with CSS not loaded


Then you will need to run the grunt task, grunt compass.


Install Compass

You will need to first install Compass, an open-source CSS Authoring Framework before you can run the Grunt task to create the main.css file which is missing.

Using node:

npm install compass

Reference the following link for more info about installing compass using npm


Run Grunt Task


After you install Compass, you will then need to run the grunt task.  In in IntelliJ you can click the grunt task runner at the bottom of your screen or go to Tools -> Open Grunt Console.

Use grunt task runner console to create Main CSS file
Run grunt compass task to create main.css file

Restart your Application


Try refreshing and restarting your application and the styling should now display correctly.

mvn spring-boot:run

Saturday, August 15, 2015

Spring Scheduler to Create Background Process to make Async Calls to Update Cache in Spring Boot


Using Spring Scheduler to Create a Scheduled Task that hits your Endpoints to Update a Spring Cache for a Spring Boot Application



I used Spring Scheduler to execute a process on a regular basis to make separate async calls to several endpoints to keep the Spring cache updated and also as a health monitor.

This serves two purposes for us, we monitor all the endpoints to ensure correct data is coming back and it keeps the caches fresh and up to date even when there hasn't been traffic on that endpoint recently.

Guides on how to setup Spring caching:


Guide on Spring Scheduler:  http://spring.io/guides/gs/scheduling-tasks/


View all the code used in this article on Github here:


Spring Scheduler



You will want to add 3 annotations to your Application.java class.  You will need to add annotations for caching, async method calls and scheduling.

Add annotations to your Application.Java class for caching, async and scheduling
Add annotations to your Application.Java class

Create Scheduled Task



I created a scheduled task here and set the interval that it runs on using Spring Scheduler and the @Scheduled annotation.

The task calls a method which is async and cached which allowed me to call multiple endpoints concurrently.  The results of the method call were cached and the success was logged.

I also injected the service to call the async method we are using to get the status of various endpoints.



Create Service for Scheduled Task to use



Get endpoint status is the method called directly from the scheduled task.  You can pass in a constant or enum to indicate which endpoint you want to check.  Each endpoint will be cached separately because the parameters in the method call are different.

You will need to setup caching in a separate config file.  You can view this article on how to setup Spring caching:  http://codedevstuff.blogspot.com/2015/07/add-guava-cache-to-spring-boot-to-cache.html

Get endpoint status will then call the correct getStatus() method to check the results of the endpoint.  These results will be cached.



This scheduled task can be used with an API or service to regularly check the status and response of various endpoints for health checking or as a regular running integration test of sorts.

View full code below



View the code on Github here:

Code snippets below:


Wednesday, July 29, 2015

Add Guava Cache to Spring Boot to Cache Method Calls


Add Google Guava Cache to Spring Boot to Cache the Results of Method Calls using Java Config and Spring Annotations



If you have a time-intensive method that is slowing down your application and the results of that method don't change very often, it is a good candidate for caching.  This guide will show you how to implement caching in your Spring Boot application using Google Guava cache.  

Guava cache is a simple, lightweight in-memory cache that has more configuration options then the default In-Memory Spring Cache.  Guava cache is not for clustered systems, for that you will want to use Hazelcast http://hazelcast.com/

So if you just need a simple cache but you want more config options such as cache expiration time, then this approach may fit your use case.  Alternatively, you could use EhCache.

Full source code references can be found at the bottom of the article.

More info on Guava Cache:
https://code.google.com/p/guava-libraries/wiki/CachesExplained

More info on Spring Cache:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html



Add dependencies to your Maven pom.xml file



Add the two following dependencies to your maven pom file and clean and package your dependencies.  You will need to add Guava and a Spring library that contains helper methods and classes for configuring the cache.

Add dependencies to your pom.xml for google guava cache and helper spring methods
Add dependencies to your pom.xml for guava cache

Create CacheConfig file



You need to create a CacheConfig file to configure the cache using Java config.  Create this config file as a separate class implementing CachingConfigurer and place it in your config folder with your other Java config classes.

Add Cache Manager and setup Guava Cache
Add Cache Manager and setup Guava Cache



Add Cacheable annotation to method to be cached



You will need to add the @Cacheable annotation and pass in the name of the cache which will be used for that method.

If your method has a passed in parameter, results for each different parameter will be cached.  So, each time a new parameter is passed in, this will execute the method and place the results in the cache.  Future calls using that parameter will pull results from the cache and skip execution of the method.

Add Cacheable annotation to the method to be cached
Add Cacheable annotation to the method to be cached





Add Endpoint to Clear Cache on Demand



You will also probably want to add an endpoint to easily refresh the cache.  You can do this by adding an endpoint to a controller with the @CacheEvict annotation.

This method will completely clear the cache.  All future requests will execute the method until the cache is populated again.

You may want to protect this endpoint using basic security implemented through Spring Security


Add an endpoint to clear the cache
Add an endpoint to clear the cache

Other considerations and Source Code



You may want to initially warm the cache and call the method with all possible parameters such that even initial users access the cached version.


You can view the complete source code here: https://gist.github.com/anataliocs/1074e7004b2c99320b7a




Friday, May 15, 2015

Spring Boot Internationalization with Default Locale for Message Strings

How to add Spring Boot Internationalization with Default Locale for Storing Message Strings such as Validation Messages in a message.properties file 


 This article will show you how to use a properties file to define static text in your application such as for validation message.

All the code is appended at the bottom of the article. 


Add Default locale and Message Source beans to your Application class 



 Add these classes to set your default locale and configure the location of your message properties file

 
Add beans to your Spring Boot Application class
Add locale and MessageSource beans to your Application Class


Add a Service and Interface to retrieve message text 


 This service will pull the default locale from the session and then get the message text from your properties file using the messageSource.

Create Service to get messages by locale
Add a service to get the msgs in your props file


In your controller, use the Message service to get the message text 


 You will inject the message service in the controller and then pass in the id to get the value from the props file


Inject svc and get message from i18n props file
Inject service into your controller and get the message



Create the message.properties file in the locale folder 


 In resources, create the locale folder and then create a file called messages_en_US.properties

Create localized message props file
Create message properties file



Review the code here


You can view all the code here: Link to Github Gist here