Skip to main content

Posts

Showing posts from October, 2015

Hazelcast : Setup Hazelcast Server in Spring Boot Application

Following are the steps to setup to Hazelcast server Put data in hazelcast Retrieve data from hazelcast Step 1 : Add the Hazelcast dependency compile("com.hazelcast:hazelcast:3.3.5") compile("com.hazelcast:hazelcast-client:3.3.5") Step 2 : Initialize the Hazelcast Instance (Hazelcast Server) Config config = new XmlConfigBuilder().build(); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); The above code will start the Hazelcast server with default settings. For more settings you can refer this blog . Step 3 : Connect to Hazelcast server using a Hazelcast Client Now to need to get a Hazelcast client to get and post data to the hazelcast server. Following is an example of how to get the hazelcast client. ClientConfig clientConfig = new ClientConfig(); clientConfig.getNetworkConfig().addAddress(address); clientConfig.getNetworkConfig().setConnectionAttemptLimit(10); clientConfig.getNetworkConfig().setConnectionAttemptPeriod(24 * 60);

Spring Boot : How to create a Bootstrap class

Points To Remember In order to create a class that acts like a bootstrap for the application, that class needs to implement the InitializingBean of the package org.springframework.beans.factory.InitializingBean How to create a Bootstrap class in spring boot application Following is an example of a Bootstrap class. This class will be executed first when the application is coming up and is ready to server requests. Any class that implements InitializingBean will be executed before the application is up and its afterPropertiesSet method will be called. public class Bootstrap implements InitializingBean

Spring Boot : How to register a Filter in the Application

Steps : How to register a Filter in the Application Make a class. Make it a spring bean by using  @Component annotation. Implement Filter interface to register it as a Filter in the Filter chain. Use  @Order  annotation to define the order of the filter in the filter chain. Following is the example of how you can implement the Filter using the above mentioned steps

Spring : How to define Filter order in Filter Chain in Spring Boot

Points To Remember From Spring boot version you can define the order of your filters by defining order for your filters using @Order annotation. You can use @Order annotation as follows to make a filter to be first in the filter chain. @Order(1) How to define Filter order in Filter Chain in Spring Boot Following is the example where we have registered two filters Security Filter - To block unauthorized requests Tracking Filter - To log each request coming to the server ###### security filter ###### tracking filter

Java : How to check if the field of a class is STATIC by Reflection

Points To Remember You can use Reflection  to check if the field of a class is static or not. You need to get the modifiers of the field to check if the field is static. How to check if the field of a class is static by Reflection The following is the only way to check if the field is static or not field.getModifiers()& Modifier.STATIC) == Modifier.STATIC Following program shows how to get the static fields from a class. package com.ekiras.demo; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Test { public static void main(String args[]){ Field[] fields = Person.class.getDeclaredFields(); for(Field field : fields){ if((field.getModifiers()& Modifier.STATIC) == Modifier.STATIC ){ System.out.println("final field :: " + field.getName()); } } } } class Person{ public static final int someConstant = 2; private String name; private String email; public String getName() { return name; } public void setName(String

Java : How to check if the field of a class is FINAL by Reflection

Points To Remember You can use Reflection  to check if the field of a class is final or not. You need to get the modifiers of the field to check if the field is final How to check if the field of a class is final by Reflection The following is the only way to check if the field is final or not field.getModifiers()& Modifier.FINAL) == Modifier.FINAL Following program shows how to get the final fields from a class. package com.ekiras.demo; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Test { public static void main(String args[]){ Field[] fields = Person.class.getDeclaredFields(); for(Field field : fields){ if((field.getModifiers()& Modifier.FINAL) == Modifier.FINAL ){ System.out.println("final field :: " + field.getName()); } } } } class Person{ public static final int someConstant = 2; private String name; private String email; public String getName() { return name; } public void setName(String name) { th

Java : Exception in thread "main" java.util.ConcurrentModificationException

When Exception in thread "main" java.util.ConcurrentModificationException occurs. package com.ekiras.demo; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String args[]){ // create a new list List<String> list = new ArrayList<String>(); // add 50 items to the list for(int itr=0;itr<50;itr++) list.add("user-"+(itr+1)); // try to remove item from list while iterating the list for(String str : list){ if(str.equals("user-15")){ list.remove(str); } } } } The above code will give the following error. Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859) at java.util.ArrayList$Itr.next(ArrayList.java:831) at testing.Test.main(Test.java:18) How to avoid ConcurrentModificationException Create the list of type CopyOnWriteArrayList , this will create a new copy of list for

Android: Pagination in Custom List View

Points To Remember You can refer to blog - Create list view with custom list view adapter . Now we will implement pagination in this list view. Pagination in Custom List View Download Source from GitHub We will add 20 items each time to the list view. After 18th element is displayed on the screen we will call a method to load new items to the list view, so that user does not wait for new items to be loaded.(In real cases you might be loading from a server and this might take much time). We will restrict our pagination after 400 elements have been displayed.( It is a replication of the scenario when the server has no more items to be displayed). We will be setting a onScrollListener() on the listView to check when to load new items and add them to the list. listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onSc

SpringBoot : No 'Access-Control-Allow-Origin' Access-Control-Allow-Origin

Points To Remember This error occurs when Server does not allow cross domain headers. Application does not allow cross domain headers. Custom headers provided by request is not accepted by the application. How to Solve : No 'Access-Control-Allow-Origin' Access-Control-Allow-Origin error In order to allow cross domain ajax calls to your Server you need to allow the Cross Domian Headers in your application. You can allow the cross domain requests from your application in Spring boot by adding a CORS Filter as shown below. response.setHeader( "Access-Control-Allow-Origin" , "*" ); response.setHeader( "Access-Control-Allow-Methods" , "POST, GET, OPTIONS, DELETE" ); response.setHeader( "Access-Control-Max-Age" , "3600" ); response.setHeader( "Access-Control-Allow-Headers" , "Content-Type, x-requested-with, X-Custom-Header" ); Here you need to specify the following Headers your application accepts as a co

Android : Create a List View with custom List View Adapter

Points To Remember You need to create a custom List Adapter to show a model class in the list view. You need to create a xml file to display, how each item of the list will look on the UI. You need to set the Tag in the view, so that your data does not refresh due to scroll. Download from GitHub Create a List View with custom List View Adapter You need to make Person Model class - show data on UI from this class Person Adapter - create the list view and inflate the view Main Activity - to contain the list view  The List View will look like as shown in image below Download from GitHub

Android : Difference between gravity and layout_gravity

Points To Remember There are two attributes for a View in android to align its content gravity layout_gravity The basic difference between the two is that android:gravity is used for child elements of the view. android:gravity_layout  is used for this element with respect to parent view . So, if you have a layout like <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="EkiRas" android:textSize="22dp" android:gravity="center" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_h

Android : How to hide and show a view

Points To Remember There are three things yo can do to set the visibility of a VIEW in android VISIBLE - it makes the VIEW visible . INVISIBLE - it makes the VIEW invisible but it still takes the space occupied by the View element on the screen. GONE - it makes the VIEW invisible and also hides the space occupied by the view on the screen. How to make a TextView Visible The following code makes the TextView invisible to the user and also hides the space occupied by the TextView on screen <TextView android:id="@+id/text_view_name" android:visibility="gone" android:layout_height="30dp" android:textColor="@android:color/holo_blue_dark" android:paddingLeft="15dp" android:textStyle="bold" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:gravity="center_vertical" android:layout_width="match_parent" android:b

RestTemplate : How to get response from a server by HTTP GET call

Points To Remember You need to add the following dependency to your project to use Spring Rest Template. compile('org.springframework:spring-web:3.0.2.RELEASE') How to get response from a server by HTTP GET call Following are the methods that can be used to get the response from a url using HTTP GET method. getForObject() This method takes a URL , Response class  (it tells that the response should be bound to this class). RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject("http://localhost:8080/someUrl",String.class); Following method will make a Http GET call with params. Map<String,String> params = new LinkedHashMap<String, String>(); params.put("userId","1"); RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject("http://localhost:8080/someUrl",String.class,params); This method does not give the following Http status code for the GET cal

Android : How to change the Android Application Label in Action Bar

Points To Remember Android saves the Application level Settings like App Icon, App Label etc in the AndroidManifest.xml file. How to change the Android Application Icon  Go to the Application > manifests > AndroidManifests.xml  file and look for the following lines. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ekiras.activity" > <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > // Your activities are declared here <activity></activity> </application> </manifest> You can set the Application icon in strings.xml by name app_name  in the values folder. <resources> <string name="app_name">Ekiras</string> <

Android : How to create a Basic List View

Points To Remember You need to create a ListView in your activity. Then create a view for Item of the ListView. Create an Adapter and a list of objects to display. Pass the List of objects to the ListView. How to create a Basic List View  Your Activity may look like the following activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="Planet List" android:textAlignment="center" android:textSize="24dp" /> <ListView android:id="@+id/list_view_drawer"

Android : How to change ActionBar/ ToolBar color

Points To Remember You can just override the primary_material_dark  in the colorx.xml file in values folder. How to change ActionBar/ ToolBar color We will create a new file colors.xml if it does not exist. Override the Primary color of the theme  Theme.AppCompat.Light.DarkActionBar See the results <?xml version="1.0" encoding="utf-8"?> <resources> <color name="primary_material_dark">#1B5891</color> </resources> Your styles.xml file may look like the following <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> </resources> and the AndroidManifest.xml may look like following <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.

Android : Add Action Icon / Action button on Action Bar

Points To Remember You need to create a file in res > menu > menu.xml You can use showAsAction property to decide if to show the action icon on action bar or not.  Adding Action Icon Action button on Action Bar You can choose either of the following app:showAsAction="never" This will show the action in the list view when menu icon is pressed. app:showAsAction="ifRoom" This will show the action in the action bar if the amount of space required to show the action is available on the Action bar. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".HomeActivity"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="#Settings" app:showAsAction="never" /> <item