Yichun Zhao JAVA Notes

My Java Online Notes

Saturday, 20 May 2017

Calculate distance when knowing longitude and latitude in java

Maven Dependency 

<dependency>
  <groupId>com.javadocmd</groupId>
  <artifactId>simplelatlng</artifactId>
  <version>RELEASE</version>
</dependency>

Getting Started
SimpleLatLng is written and compiled to Java 6. After you have included the library jar in your project or added it as a dependency in your build tool of choice, you can create a point in latitude and longitude as follows:

LatLng point = new LatLng(33.123123, -127.123123);
The important thing to note is that all latitudes and longitudes are handled in degrees using the convention that positive latitudes are in the northern hemisphere and positive longitudes are east of the Prime Meridian.

This library generally considers degree measurements to the millionth (0.000001) degree. So 33.0000010 is equal to 33.0000015, but not equal to 33.0000020. This yields a resolution of around 1 centimeter which is far more accurate than the distance calculations require.

When a LatLng point is constructed, its latitude and longitude will be normalized to fall within the ±90 degrees latitude, ±180 degree longitude range. Special case: if your LatLng is constructed at a pole (±90 degrees latitude), its longitude will be set to 0 because all longitudes intersect at the poles.
Distance between two points

To calculate the distance between two points, use the static methods in LatLngTool.
LatLng point1 = new LatLng(33.123123, -127.123123);
LatLng point2 = new LatLng(33.321321, -127.321321);
double distanceInMiles = LatLngTool.distance(point1, point2, LengthUnit.MILE);
All supported units of length are contained by the enum LengthUnit. We will add supported units if there is call for them.

Reference 

SimpleLatLng is a Java implementation of common latitude and longitude calculations.

Algorithm to find all Latitude Longitude locations within a certain distance from a given Lat Lng location

Spring Data Elasticsearch

index Latitude and Longitude and fast SQL query

at May 20, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Monday, 15 May 2017

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

Meeting the error msg. above,  when setting up Postgres database in the Spring boot.

This error is mostly due to the missing Postgres driver dependencies in the pom.


at May 15, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Thursday, 11 May 2017

Project Lombok


The project aims to reduce java boilerplate code. It may auto-generate getter, setters, to-string, hashcode, equals, and constructors etc.  To import it, add the following element as a child of dependecies in the pom.xml file:


<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.16</version>
</dependency>

Important: scope should be provided because we do not want Lombok jar file to be included in our build file.

Reference

Tutorial: using Lombok to reduce boilerplate code in Java
at May 11, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: LomBok

Wednesday, 10 May 2017

How to connect Postgres Database outside Heroku?

I tried to connect to the heroku Postgres database locally in the Netbeans, but I failed.

Open heroku "database credentials", copy the host, username, password etc into Netbeans as building a new database connection.

It needs to add the following at the end of JDBC URL, otherwise connection will fail.
?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory


at May 10, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: PaaS, Spring, SpringBoot

Using Profile Specific Property to Config. Dev. and Test Env.


Instead of loading different application property files, Spring Boot provides a simple way to setup different application properties according to build purposes.

Ref. to
profile specific properties 

Key points.
1) using the naming convention, i.e. application-{profile}.properties to define build specific property files.  for instance,  application-tst.properties

2) the profile specific property file can be triggered by adding the property spring.profile.active = tst  in application.properties file.

Please Note:
the property defined in the profile specific property file overrides the one defined in the application.properties. It doesn't mean the profile specific property file overrides the whole property definition defined in application.properties file.

I need to test the following way:
Spring Profile
Using Spring profile to make classes decorated by @Component or @Configuration to be alive.
Set @Profile("production") on @Component or @Configuration
spring.profiles.active=production

at May 10, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: SpringBoot

Sunday, 7 May 2017

How to Deploy Spring Boot to Heroku

Heroku is a popular PaaS platform.

For a window OS, installing Heroku CLI first; download it from Heroku homepage.
Starting Windows cmd, type in
$heroku login; 
input login email and pwd;
Switching to your working directory.

Create a new Git repository
Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a yardnbud

Deploy the App
(1) create an app on Heroku, which prepares Heroku to receive your source code. 
$heroku create   
or create a named app. 
$heroku apps:create myapp

(2) deploy your code in the heroku
$git push heroku master

ensure that at least one instance of the app is running
$heroku ps:scale web=1
meaning that, right now your app is running on a single web dyno. 

a handy shortcut to open the website as follows: 
$heroku open

View Logs 
$heroku logs --tail  
stop streaming the logs  by control +c

Define a Procfile
Creating a Procfile, a text file without any extension, in the root directory of your application, to tell heroku how to start your app. 

 You must configure your application to listen on the correct port.
web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot makes -D arguments available as properties accessible from a Spring Environment instance. The server.port configuration property is fed to the embedded Tomcat, Jetty or Undertow instance which then uses it when it starts up. The $Port environment variables is assigned to us by the Heroku Paas.  

web is important here. it declares that this process type will be attached to the HTTP routing stack of Heroku and receive web traffic when deployed

Declare app dependencies
Heroku recognizes an app as Java by the existence of a pom file in the root directory. It specifies the dependencies that need to be installed with you application. When an app is deployed, Heroku reads this pom file and install the dependencies.

Heroku by default use Java 8; so Maven or Gradle build need to use the same version(Maven can use the java.version property). If requiring JDK 7, creating a new file adjacent to pom.xml and Procfile, called system.properties, where adding the following:

java.runtime.version=1.7

The most common workflow for Heroku deployments is to git push the code to the production. 
$git push heroku master

Scale the app
A dyno is considered as a lightweight container that runs the command specified in the Procfile
check how many dynos running using the command
$heroku ps

scale number of dyno up
$heroku ps:scale web=1 

Run app locally
$heroku local web 

heroku local examines the Procfile to determine what command to run

Using Database

The HikariCP(ref. to A) database connection pool(ref. to B) is initialized with the configuration application.properties
spring.datasource.url: ${JDBC_DATABASE_URL:}
This sets spring.datasource.url to the value in the JDBC_DATABASE_URL environment variable, set by the database add-on, and establishes a pool of connections to the database.
Listing database provisioned for your app using the following command
$heroku addons 

Listing config vars for your app that will display the URL to the database. 
$heroku config 

Listing a lot more
$heroku pg


Deploy local changes

$git add .
$git commit -m "demo"
$git push heroku master
$heroku open 




A: What is HikariCP?
HikariCP is a “zero-overhead” production-quality connection pool.

B: What is Connection Pool?
Establishing JDBC connections is resource-expensive, especially when the JDBC API is used in a middle-tier server environment, such as when DataDirect Connect for JDBC or DataDirect SequeLink for JDBC is running on a Java-enabled web server. In this type of environment, performance can be improved significantly when connection pooling is used. Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.

C: Using SPRING_DATASOURCE_URL in Spring Boot APP
Heroku buildpacks for Java and Gradle will attempt to create SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD environment variables when a dyno starts up. The values of the variables will be identical to the values in the corresponding JDBC variables.
As long as your application has the proper JDBC driver defined as a dependency, these environment variables should allow your Spring Boot application to connect to the database without any other configuration.
If you need to override the predefined SPRING_DATASOURCE_* environment variables you can set them yourself with the heroku config:set command or in the dashboard. Alternatively, you can add the -Dspring.datasource.url property to your Procfile, which will take precedence over the OS-level environment variables.


D: Where define environment variables in Spring Boot APP?
environment variables(key = value) are defined in the .env text file located at the root directory. It may be configured from CLI, for instance as the following;
$heroku config:set ENERGY = "20 Gev."
In runtime, environment variables are exposed to application and then can be grabbed like the following;
String energy = System.getEnv().get("ENERGY"); 


at May 07, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: Heroku

Sunday, 30 April 2017

Spring Boot PostGre DataBase Setup

I tested to use a cloud Postgres database provided by Heroku from my Netbeans in the following database connection setup. However, it led to a connection failure.

I think the fault should be at the JDBC URL, which needs extra string to break down the https. Please ref. to how-to-connect-postgres-database


# DATA SOURCE
spring.datasource.url=jdbc:postgres://ixfctradvatvsx:s5d2njfvukfHWlzdmVDcETNUgx@ec2-54-83-56-177.compute-1.amazonaws.com:5432/dlbp7q8itvcdf
spring.datasource.username = ixfctradvatvsx
spring.datasource.password = s5d2njfvukfHWlzdmVDcETNUgx
spring.datasource.driver-class-name = org.postgresql.Driver

# JPA/HIBERNATE
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create-drop

spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect


at April 30, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: SpringBoot
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...

  • keyword strictfp
    The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
  • Configuring Spring Boot Test
    In construction. Spring test annotations:  @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
  • Anonymous Class
    As the name implies, an anonymous inner class isn’t defined using an explicit name. An anonymous inner class is created when you combine ins...

About Me

Yichun Zhao
View my complete profile

Blog Archive

  • September (2)
  • July (1)
  • June (1)
  • April (4)
  • March (2)
  • February (1)
  • January (1)
  • November (1)
  • September (5)
  • August (3)
  • July (1)
  • June (1)
  • July (3)
  • June (8)
  • May (3)
  • April (2)
  • March (1)
  • February (3)
  • January (9)
  • December (1)
  • October (1)
  • August (2)
  • July (4)
  • May (1)
  • April (2)
  • December (1)
  • October (8)
  • September (7)
  • August (4)
  • June (1)
  • May (2)
  • April (2)
  • February (1)
  • December (5)
  • September (2)
  • August (1)
  • July (2)
  • June (1)
  • May (5)
  • April (1)
  • March (5)
  • February (2)
  • January (2)
  • November (1)
  • October (1)
  • September (1)
  • August (4)
  • July (1)
  • June (2)
  • May (7)
  • April (6)
  • March (1)
  • November (1)
  • September (4)
  • August (9)
  • June (13)
  • May (20)
  • April (2)
  • March (2)
  • June (1)
  • September (3)
  • July (1)
  • March (1)
  • October (1)
  • September (1)
  • August (3)
  • April (1)
  • November (1)
  • September (5)
  • August (3)
  • July (4)
  • June (4)
  • May (1)

Labels

  • Algorithm
  • BigNumber
  • Database
  • Docker
  • Email
  • Exception
  • File IO
  • Generics and Collections
  • Git
  • GMap API
  • Heroku
  • IDE
  • Java 8
  • JavaCharts
  • JDBC
  • JPA-Hibernate
  • K8s
  • Keyword
  • Linux
  • Log
  • LomBok
  • MacOS
  • Maven
  • microservice
  • OCPJP
  • OOAD
  • Overriding
  • PaaS
  • Patterns
  • Performance
  • Persist LOB using JPA in J2EE/J2SE
  • PostgreSQL
  • Regx
  • Spring
  • SpringBoot
  • SpringBoot Test
  • SQL
  • Test
  • Threads and Concurrency
  • Time Complexity
  • URI

Report Abuse

  • Home

Search This Blog

Simple theme. Theme images by luoman. Powered by Blogger.