Thursday, September 8, 2016

Git: How To



How to create a new local branch?
git checkout -b <branch>
How to push a new local branch to remote?
git push origin <branch>
How to push a new local branch to remote and track it?
git push -u origin <branch>
How to remove remote?
git remote remove origin    
git remote rm origin
How to add a remote?
git add origin <url to remote>
How to remove remote and add a new one?
git remote set-url origin git://new.url.here   
How to see diff on uncommitted files with their previous version?
git diff <file name>
How to see git diff on a added file with its previous version?
git diff --cached <file name>
How to see diff on a committed file with its previous version?
git diff HEAD^ <file name>
How to add a tag on a branch?
git tag <tag name>
How to delete a tag?
git tag -d <tag_name>
How to list all tags?
git tag
How to push tag to remote?
git push --tags
How to see commit history?
git log
How to see commit history with files committed?
git log --stat
How to check out a perticular commit from commit log?
git checkout <commit id>

This is all for the moment will add more as and when I come across :)



Tuesday, September 6, 2016

Spring JPA, Entity Manager and Custom Query

When it comes to Spring JPA, usually until you have a really complex relationship in your tables its quite simple and straight forward to work on it.

I am going to explain a scenario where you want to get a result set which directly does not map to the entity you have defined.

Suppose you are a laundry service provider and you have a table which contains the details about the delivery offices which can do home delivery.

------------------------------ TABLE: laundry_office ----------------------------

CREATE TABLE `laundry_office` ( `id` bigint(20) NOT NULL, `address_1` varchar(255) DEFAULT NULL, `address_2` varchar(255) DEFAULT NULL, `address_3` varchar(255) DEFAULT NULL, `address_4` varchar(255) DEFAULT NULL, `latitude` double DEFAULT NULL, `longitude` double DEFAULT NULL,
  `home_delivery_indicator` bit NOT NULL DEFAULT 0,
`name` varchar(255) DEFAULT NULL, `postcode` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

------------Corresponding Entity-----------------------------


Here we have used SqlResultSetMapping as we are trying to get extra data  from the query which can't be held by the Entity object of LaundryOffice . (distance_in_miles in this case)

Pay attention to : columns={@ColumnResult(name="distance_in_miles")}

@Entity@Table(name = "laundry_office",
         indexes = {@Index(name = "geo_index",  columnList="latitude,longitude")})
@SqlResultSetMapping(
        name = "mySqlResultSetMapping",
        entities = @EntityResult(
                entityClass = LaundryOffice.class,
                fields = {
                        @FieldResult(column = "id", name = "id"),
                        @FieldResult(column = "name", name = "name"),
                        @FieldResult(column = "address_1", name = "address1"),
                        @FieldResult(column = "address_2", name = "address2"),
                        @FieldResult(column = "address_3", name = "address3"),
                        @FieldResult(column = "address_4", name = "address4"),
                        @FieldResult(column = "postcode", name = "postcode"),
                        @FieldResult(column = "longitude", name = "longitude"),
                        @FieldResult(column = "latitude", name = "latitude"),
                        @FieldResult(column = "home_delivery_indicator", name = "homeDeliveryIndicator")}),
                        columns={@ColumnResult(name="distance_in_miles")}
)
public class LaundryOffice {

    @Id    @Column(name = "id")
    private long id;

    @Column(name = "name")
    private String name;

    @Column(name = "address_1")
    private String address1;

    @Column(name = "address_2")
    private String address2;

    @Column(name = "address_3")
    private String address3;

    @Column(name = "address_4")
    private String address4;

    @Column(name = "postcode")
    private String postcode;

    @Column(name = "longitude")
    private Double longitude;

    @Column(name = "latitude")
    private Double latitude;
   
   @Column(name = "home_delivery_indicator", columnDefinition="bit NOT NULL DEFAULT 0")
   private boolean homeDeliveryIndicator;
}

----------- Java code -----------------------

Firing simple query is straight forward, but for complex queries, we may have to use Entity Manager. 
Refer SpringJPA for simple  queries.
@PersistenceContext
private EntityManager entityManager;

// Creating a parameterized custom query.
private static final String FIND_ALL_LAUNDRY_OFFICE_BY_LAT_LONG_AND_RADIUS =
        //To return km instead of miles, swap the ‘3959’ below statement with ‘6371’)        "select (\n" +
                "     3959 * acos (\n" +
                "       cos ( radians(:latitude) )\n" +
                "       * cos( radians( latitude ) )\n" +
                "       * cos( radians( longitude ) - radians(:longitude) )\n" +
                "       + sin ( radians(:latitude) )\n" +
                "       * sin( radians( latitude ) )\n" +
                "     )\n" +
                "   ) AS distance_in_miles, laundry_office.*\n" +
                " FROM laundry_office\n" +
                " HAVING distance_in_miles < :radius\n" +
" ORDER BY distance_in_miles\n";

Note, how we are getting all the laundry_office data and distance_in_miles in above query.


// Pass parameters to the above queries. 
List<Object[]> results = entityManager.createNativeQuery(FIND_ENQUIRY_OFFICE_BY_LOCAL_COLLECT, "mySqlResultSetMapping")
.setParameter("latitude", latitude)
.setParameter("longitude", longitude)
.setParameter("radius", radius)
.setMaxResults(10) // act as limit in sql query
.getResultList();

//Extract both Entity and extra parameter out of the result sets returned. 
results.stream().forEach((record) -> {
    LaundryOffice lo = (LaundryOffice) record[0];
    double distance_in_miles = (double) record[1];  
   System.out.println("LaundryOffice: " + lo.getId() );
   System.out.println("distance_in_miles: " + distance_in_miles );
});

Hope this example helps you to when creating complex custom queries

Enjoy!! :)

Thursday, October 22, 2015

Hystrix demistified

Hystrix is simple in itself and very easy to understand, still some get confused about how it works.
So let me give a simple example of how it works.
I have taken below example from : Hystrix site Getting-Started
public class CommandHelloWorld extends HystrixCommand<String> {

    private final String name;

    public CommandHelloWorld(String name) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected String run() {
        // a real example would do work like a network call here
        return "Hello " + name + "!";
    }
}

String s = new CommandHelloWorld("Bob").execute();
Let us demystify this code:

First:

You need to extend the HystrixCommand to use Hystrix circuit breaker.
HystrixCommand<String>  // This statement tells what your Hystrix command is going to return when your execute it. (Return value of run() method)

HystrixCommand<AnyObject>

HystrixCommand<Anything> 

Second:

Constructor behave as the entry point of your Hystrix Command. You should define it in such a way that values passed can be used to:
1) defining Hystrix Command name, group name , Cache key etc.
2)  help execute the run() method to do the actual task by providing needed element.
    public CommandHelloWorld(String name) {
        }

Example 1:

In above example this parameter is used in run method.
    public CommandHelloWorld(String name) {
    super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
    }

Example 2:

The parameter here are used to define Hystrix Command and are used in run method to make a http call.
   public CommandHelloWorld(String groupKeyName,String commandName, HttpClient httpClient, HttpContext httpContext, HttpRequestBase httpRequest) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKeyName)).andCommandKey(HystrixCommandKey.Factory.asKey(commandName)));
    }
    

Example 3:

The parameter here are used to define Hystrix Command and are used in run jdbc call.
public CommandHelloWorld(String groupKeyName,String commandName, JDBCTemplate jdbcTemplate) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKeyName)).andCommandKey(HystrixCommandKey.Factory.asKey(commandName)));
    }

Third:

This call is to configure the HystrixCommand.
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));  // this define the group name of your Hystrix command.

Hystrix define two constructror for configuration:

protected HystrixCommand(HystrixCommandGroupKey group) { // Example 1 
        this(new HystrixCommand.Setter(group, null));
    }

protected HystrixCommand(HystrixCommand.Setter setter) { // Example 2 or 3
        this(setter.groupKey, setter.commandKey, setter.threadPoolKey, (HystrixCircuitBreaker)null, (HystrixThreadPool)null, setter.commandPropertiesDefaults, setter.threadPoolPropertiesDefaults, (HystrixCommandMetrics)null, (HystrixCommand.TryableSemaphore)null, (HystrixCommand.TryableSemaphore)null, (HystrixPropertiesStrategy)null, (HystrixCommandExecutionHook)null);
    }

Fourth:

To implement the HystrixComman override the run() method. This is where you will do all the task as per your requirement.
@Override
    protected String run() {
        // a real example would do work like a network call here
        return "Hello " + name + "!";
    }

Fifth:

The above created command be used like this :String s = new CommandHelloWorld("Bob").execute(); // this will internally call the run() method

Monday, October 5, 2015

Spring Core -- Interesting Observations



  1. Spring Framework -> It's programing model. 
  2. When doing Unit Test of Spring Application Try not to use Spring. Dependencies can easily be stubbed out for unit Testing.
  3. Used Java based configuration  as its more intuitive and easy to comprehend by Java programmer.
  4. If a Class want to have Fields which are mandatory for its creation/working make sure your use constructor for it. Try not to Auto-wire the private fields, It will be hard to test it.
  5. It is good practice to have a Integration Test for configuration to validate that your Spring Context only has bean which you intend to have.
  6. Try not to have schema version number in name space of your xml files, as correct version is automatically picked by Spring. Also it made easy for upgrade .  But if you add it  it does not add any value, but make your code hard to  upgrade.
  7. Try to have multiple configuration files one for each different aspect of your application.
  8. e.g application bean configuration. infrastructure configuration, rest configuration, soap configuration etc.
  9. User Spring Profile for managing your application based on different env.   Component which does not have any Profile annotation on it is always available along with the currently active profile components.
  10. Never rely on the Spring generated Names when using @Component can lead to run time issues.
  11. Java Configuration is arguably better than using @Component.


Transaction

Transaction will not work if you break the thread boundary. Transaction is Spring are stored in thread-local which are not  passed between threads.

So if a thread begins a transaction and in between your code you create a new thread then most probably your transaction wont work. as the newly created thread wont have the transaction element.


Constructor vs Setter Dependency when to use which?

Constructor :

  1. Mandatory fields
  2. Immutable dependencies
  3. Concise (pass several parameter at once).


Setter Dependency :

  1. Option /Changeable dependency
  2. Circular dependency
  3. Inherited automatically.












Sunday, September 20, 2015

Exposing your service as SOAP Enpoint using Apache Camel

Today I will be telling you about how to expose your service as SOAP endpoint using Apache Camel.


The four steps for it.

1) creating of  cxf-server.xml -- which contain detail about your soap endpoint.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:soap="http://cxf.apache.org/bindings/soap"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                           http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd">


    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

   
    <!-- Create SOAP endpoint  -->
    <cxf:cxfEndpoint id="mySoapEndPoint"
                     address="http://localhost:{{port}}/test/SOAP"
                     serviceClass="com.test.Broker">
        <cxf:binding>
            <soap:soapBinding version="1.2"/>  <!-- this allow you accept soap 1.2 request, as  by default camel accept only soap 1.1 request-->
        </cxf:binding>
        <cxf:properties>
            <entry key="dataFormat" value="PAYLOAD"/>
        </cxf:properties>
    </cxf:cxfEndpoint>
   
 
   
</beans>


2) Creation of your camel-server.xml -- defining your route.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.11.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <import resource="classpath:META-INF/cxf/cxf-config.xml" />


<!-- Define your route here -->
    <camel:camelContext id="camel-server">

        <!-- Externally Exposed Routes -->
        <camel:routeBuilder ref="soapHandlerRoute"/>
     
    </camel:camelContext>

<bean id="soapHandlerRoute"  class="com.test.SoapRoute "/>




3)  Define your route -- the actual implementation.

package com.test;

public class SoapRoute extends RouteBuilder {

@Override
    public void configure() throws Exception {

     from("cxf:bean:mySoapEndPoint")
                .routeId("soapRouteId")
                .convertBodyTo(String.class)
                .end(); //  add your logic here to handle the input soap request.

    }


}


4) Define your dummy endpoint broker class


package com.test;

@WebServiceProvider()
@ServiceMode(Mode.PAYLOAD)
public class Broker implements Provider<SOAPMessage>{

    public SOAPMessage invoke(SOAPMessage message) {
        // Requests should not come here as the Camel route will
        // intercept the call before this is invoked.
        throw new UnsupportedOperationException("Placeholder method");
    }
 
}


}


That is it your have exposed your service as SOAP Request and Response.


Refer : cxf-example and cxf.html for more detail







Wednesday, June 10, 2015

Mongodb and scripting


To do house keeping job on Mongo DB we can use scripting for it

Steps involved will be

1) Write a java script file using the details given in http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/
2) To run a script use comand : mongo .js
3) If you want to get the shell also when executing like this : mongo testConnction.js --shell

testJsScript.js

` db = connect("localhost:27017/myDatabase"); // db contain the reference of the database printjson(db.getCollectionNames()); // mongodb provided print command print(db.getCollectionNames()); //js print command cursor = db.collection.find(); // this will return a cursor, this cursor can be used to iterate over all the collection object.
you can even use this cursor for updating items, but the catch is you have to fire a query which actually return only the json object which can be directly used in the query.