Wednesday, August 13, 2014

Android HTML 5 Parameter Passing Between Pages

Developing HTML 5 mobile application for Android is straightforward as we see before, one of the challenges in developing mobile applications using HTML 5 is passing parameters between pages, the main reason for that is some bugs that exist in some Android versions, let's see the possible ways to overcome this issue:

1) Using URL Parameters

In that case while you are routing your screen to another screen add the required parameters to the page URL.
For example:
self.location='itemDetails.html?id=115&type=Y';
In that example we have passed 2 parameters; "id" and "type" with the corresponding values.
This way fail due to Android bug and will mostly show "Page not found!" as it will search for the page that match the whole URL !

2) Using localStorage

In that way submitting the page will save the selected parameters into localStorage and the target page will reload these parameters in its onLoad() method.
For example:
< img src="images/index/84.png" onClick="goDetails(84)">

function goDetails(itemId){
localStorage.setItem("itemId",itemId);
self.location='itemDetails.html';
}

In the target page, load the stored value:

< body onload="init();">

function init(){
      itemId=localStorage.getItem("itemId");
      //do any required logic here ...
}

3) Using Android JavaScriptInterface

In this way we will use the Android code to store the selected parameters as instance variables so we can retrieve them from the target page.

For example:
function goDetails(itemId){
Android.setCurrentId(itemId);
self.location='itemDetails.html';
}
function init(){
  itemId=Android.getCurrentId();
}
And in your Activity add the following JavaScriptInterface methods:

public class MyJavascriptInterface {
    Context mContext;
    String id="114";
    MyJavascriptInterface(Context c) {
            mContext = c;
    }
    @android.webkit.JavascriptInterface
    public String getCurrentId() {
return id;
    }
    @android.webkit.JavascriptInterface
    public void setCurrentId(String current) {
id= current;
    }
....
}

We used the "String id" to store the value..

And add it in the WebView we add our JavaScriptInterface with the prefix as following:
     webView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");


Friday, July 18, 2014

JPA 2.1 Performance Tuning Tips

Here is some performance tuning tips while dealing with JPA 2.1.




To get more details about Java EE 7 performance tuning, check my book at the following URL: http://www.packtpub.com/java-ee-7-performance-tuning-and- optimization/book

Wednesday, July 2, 2014

Win "Java EE 7 Performance Tuning and Optimization" Book - CLOSED



Book Give-away:

Hold a chance to win free copy of the Java EE 7 Performance Tuning and Optimization, just by commenting about the book with the link - http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book!

For the contest we have 5 e-copies each of the book Java EE 7 Performance Tuning and Optimization, to be given away to 5 lucky winners.

How you can win:

To win your copy of this book, all you need to do is come up with a comment below:
1) Highlighting the reason "why you would like to win this book” 
2) Add the link of the book - http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book
3) Adding a book tweet link or social media post in the comment will increase the chance to win the book.

Note – To win, you must follow the previous 3 steps.

Duration of the contest & selection of winners:

The contest is valid for 1 week, and is open to everyone. Winners will be selected on the basis of their comment posted.
We will announce the winners in this post and ask them to provide their contact email addresses.




NOW the winners are listed below in the comments, please send me your emails, good luck for others.

Tuesday, July 1, 2014

Using JPA with Google App Engine Object Store

We can use JPA while dealing with Object Store in Google App Engine, this abstracts the interaction with object store.
Because both Object store and JPA are object-based there is good matching between both technologies.
Here is the simple steps we need to follow:

1) Create the JPA beans
- Including annotation
- Define primary key
- Setters/Getters for fields.
Here is an example of using Locate object as JPA bean:
@PersistenceCapable(identityType=IdentityType.DATASTORE)
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Long id;
@Persistent
public int entityId;
@Persistent
public double lat;
@Persistent
public double lon;
@Persistent
public double accuracy;
@Persistent
public long time;

public Location(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
... //getters and setters
}

We used auto-generation primary key strategy here.

2) Create session facade or abstraction layer or DAO object to interact with this bean

public class LocationDAO {
}

3) Define PersistenceManagerFactory object

private static final PersistenceManagerFactory persistenceFactory = JDOHelper
.getPersistenceManagerFactory("transactions-optional");


4) Get PersistenceManager object to use for JPA interactions

PersistenceManager pm = pmfInstance.getPersistenceManager();


5) Example for ADD/CREATE operation

public Location addLocation(int entityId, double lat, double lon, double accuracy,long time){
Location location = null;
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
location=new Location();
location.setEntityId(entityId);
location.setLat(lat);
location.setLon(lon);
location.setAccuracy(accuracy);
location.setTime(time);
pm.makePersistent(location);
} finally {
pm.close();
}
return location;
}

6) Example for DELETE operation

public void removeLocation(Location location) {
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
pm.currentTransaction().begin();
location = pm.getObjectById(Location.class, location.getId());
pm.deletePersistent(location);
pm.currentTransaction().commit();
} catch (Exception ex) {
pm.currentTransaction().rollback();
throw new RuntimeException(ex);
} finally {
pm.close();
}
}

7) Example of UPDATE operation

public void updateLocation(Location location,int entityId, double lat, double lon, double accuracy,long time) {
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
pm.currentTransaction().begin();
location = pm.getObjectById(Location.class, location.getId());
location.setEntityId(entityId);
location.setLat(lat);
location.setLon(lon);
location.setAccuracy(accuracy);
location.setTime(time);
pm.makePersistent(location);
pm.currentTransaction().commit();
} catch (Exception ex) {
pm.currentTransaction().rollback();
throw new RuntimeException(ex);
} finally {
pm.close();
}
}

8) Example of SELECT operation

public List getAllLocationsForEntity(int entityId){
PersistenceManager pm = persistenceFactory.getPersistenceManager();
String queryStr = "select from " + Location.class.getName();
Query selectQuery=pm.newQuery(queryStr);
selectQuery.setFilter("entityId=="+entityId);
return (List) selectQuery.execute();
}

These are the simple basic CRUD operations to perform on Object data store in Google App Engine using JPA.

Friday, May 16, 2014

Java EE 7 Performance Tuning and Optimization Book


The book covers performance tuning and optimization in Java EE 7, it takes months from me to complete this book :) and i did my best to cover most of the important topics in this area.

What this book covers
Chapter 1, Getting Started with Performance Tuning, takes you through the art of performance tuning with its different components and shows you how to think when we face any performance issue. It focuses on preparing you to deal with the world of performance tuning and defining the handling tactics.
Chapter 2, Understanding Java Fundamentals, lays the foundation of required knowledge of the new features in Java Enterprise Edition 7 and different important Java concepts, including the JVM memory structure and Java concurrency. It also focuses on the different Java Enterprise Edition concurrency capabilities.
Chapter 3, Getting Familiar with Performance Testing, discusses performance testing with its different components, defines useful terminologies that you need to be aware of, and then gives hands-on information about using Apache JMeter to create your performance test plans for different components and get the results.
Chapter 4, Monitoring Java Applications, dissects the different monitoring tools that will be used in performance tuning, starting from the operating system tools, different IDE tools, JDK tools, and standalone tools. It covers JProfiler as an advanced profiling tool with its offline profiling capabilities.
Chapter 5, Recognizing Common Performance Issues, discusses the most common performance issues, classifies them, describes the symptoms, and analyzes the possible root causes.
Chapter 6, CPU Time Profiling, focuses on the details of getting the CPU and time profiling results, ways to interpret the results, and ways to handle such issues. It discusses the application logic performance and ways to evaluate different application logics. It provides the initial performance fixing strategy.
Chapter 7, Thread Profiling, discusses thread profiling with details on how to read and interpret thread profiling results and how to handle threading issues. It also highlights the ways to get, use, and read the thread dumps.
Chapter 8, Memory Profiling, discusses how to perform memory profiling, how to read and interpret the results, and how to identify and handle possible issues. It also shows how to read and query memory heap dumps and analyze the different out of memory root causes. The chapter finishes your draft performance fixing strategy.
Chapter 9, Tuning an Application's Environment, focuses on tuning the application environment, starting from the JVM and passing through other elements such as the application servers, web servers, and OS. We will focus on selected examples for each layer and discuss the best practices for tuning them.
Chapter 10, Designing High-performance Enterprise Applications, discusses design and architecture decisions and the performance impact. This includes SOA, REST, cloud, and data caching. It also discusses the performance anti-patterns.
Chapter 11, Performance Tuning Tips, highlights the performance considerations when using the Agile or Test-driven Development methodologies. This chapter also discusses some performance tuning tips that are essential during the designing and development stages of the Java EE applications, including database interaction, logging, exception handling, dealing with Java collections, and others. The chapter also discusses the javap tool that will help you to understand the compiled code in a better way.
Chapter 12, Tuning a Sample Application, includes hands-on, step-by-step tuning of a sample application that has some performance issues. We will measure the application performance and tune the application issues, and re-evaluate the application performance.

The book is published by Packt Publishing:
You can find the covered topics in Table of content in the Packt Publishing web site:
http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book

Packt Publishing offers free shipping to UK, US, Europe and selected countries in Asia.


I hope everyone found this book useful and get the maximum value from the book topics.






Thursday, April 3, 2014

BRB

Apologize for being busy in the past period, will try to give more time to my blog in the near future once I finished all my existing commitments :)