Friday, 14 June 2013

Push Notification

Push notifications to IOS devices

JavaPNS, so far the best library available in java. I have been using this in one of my projects and we have been so successful with that. I have a service that runs for every 20 min and reads around 400k records from mysql and iterates over and over and send pushes in batches.
Before you integrate this library, I suggest you to take a look at apple push notification docs. Its very important to understand the underlying behavior of notifications.


In particular, You should know what happen if you send a push notification to an erroneous push token(ie, push token that doesn't exist ) and how push token errors are handled in these libraries 



Example to send pushes using multithreads

numberOfThreads = Enable multithreading and allows us to send large number of push notifications concurrently


Apple doc says that its possible to send 9000 pushes/sec. JavaPNS  library has the ability to send large number of pushes at a time using thread mechanism. But to achieve somewhere closer to that count, It takes more than the brilliance of this library. Its up to the developer, If you would have read the above links, you can clearly get a picture of what I am taking about. Its about push token error handling logic.

There are two types of error handling in push notifications.
a) Handling erroneous push tokens(tokens that doesn't exist, ie, If user uninstall an app, token mapped to that app deosn't exist anymore, its invalid )
b) Retry and send rest of the notifications after a failure because of invalid push tokens.

Type(b) is already handled in javapns library, it has an exceptional way of handling errors. Now, the developer job is to handle type(a) properly.

To handle type(b), As a developer, when you are sending a bulk of push notifications, ignore all invalid push tokens and push everything in to a List
Next, Implement Feedback service. Its must, run feedback service on daily basis and remove invalid push tokens from your database to prevent sending pushes to invalid tokens.

Download JavaPNS library https://code.google.com/p/javapns/



Push notifications to Android devices

Server side implementation to send push notification to android devices

Add the below dependency in pom.xml



Spring annotation problem and fixes

Spring annotation problems and fixes

Multiple bean instances ( duplicate instances )

I have been dealing with spring and glassfish for almost 3 years. I have encountered many problems with bean xml declarations. Latest spring release supports annotations in java level and eliminated the need of xml declarations. But for some declarations like jpa repository, you still need the xml declarations. I feel that this blog will really help for people those who are struggling with duplicate bean instantiation problem because of improper tag definitions.

Spring offers <context:component-scan> tag to scan your source files and instantiate beans. 
We usually place this tag in your applicationContext.xml file.

    <context:component-scan base-package="com.xxxxxx" />

If you are using spring controller, placing this tag in applicationContext.xml is not just enough.
You have to put the same tag in your dispatcher-servlet.xml file to instantiate your spring controllers and map RequestMap annotations.

    <context:component-scan base-package="com.xxxxxx" /> 

Now, you have same tag definitions in applicationContext.xml and dispatcher-servlet.xml.
When spring scans these two files, it will instantiate the beans twice.

To avoid this problem. Use the below tag in applicationContext.xml

    <context:component-scan base-package="com.xxxxxx" >
        <context:exclude-filter expression="org.springframework.stereotype.Controller"
                                type="annotation"/>
    </context:component-scan>


In dispatcher-servlet.xml

   <context:component-scan base-package="com.xxxx" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" 
                                type="annotation"/>
    </context:component-scan>

  

Thursday, 13 June 2013

Glassfish Open Source Edition

Glassfish Open Source installation step by step procedure

Create User Account

  #Create new user     
sudo adduser --home /home/glassfish --system --shell /bin/bash glassfish

#Create new user group      
sudo groupadd glassfishadmin

#Assign glassfish user to glassfishadmin group    
sudo usermod -a -G glassfishadmin glassfish


Configure Java


#Remove openjdk if installed
sudo apt-get remove openjdk-6-jre openjdk-6-jdk

#Remove sun's jdk if installed

sudo apt-get remove sun-java6-jdk  sun-java6-jre

#Remove unwanted automatically installed packages
sudo apt-get autoremove

sudo apt-get autoclean

#Extract jdk 7 tar file
tar -xvf jdk-7u7-linux-x64.tar.gz

#Remove previous jdk 7 installations
rm -rf /usr/lib/jvm/jdk1.7

#If jvm folder doesn't exist, create a new one
sudo mkdir /usr/lib/jvm

#Move extracted files
sudo mv ./jdk1.7 /usr/lib/jvm/jdk1.7
sudo chgrp -R root /usr/lib/jvm/jdk1.7

sudo chown -R root /usr/lib/jvm/jdk1.7

#Update alternatives
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7/bin/javaws" 1

sudo update-alternatives --config java

#Check jdk installation
cd /etc/alternatives

ls -lrt java*

#Setting environment variables JAVA_HOMEfor all users(only for bash)
sudo nano /etc/bash.bashrc
#append the following lines:
export GLASSFISH_HOME=/home/glassfish
export JAVA_HOME=/usr/lib/jvm/jdk1.7

export PATH=$PATH:$JAVA_HOME/bin:$GLASSFISH_HOME/bin

#Setting JAVA_HOME and AS_JAVA for everyone
sudo nano /etc/environment
#append the following lines:
JAVA_HOME=/usr/lib/jvm/jdk1.7

AS_JAVA=/usr/lib/jvm/jdk1.7

#Finally check the installation in /etc/alternatives directoy

cd /etc/alternatives

ls -lrt java*

Glassfish installation


#switch over to glassfish user account
sudo su glassfish

#Change to home directory of glassfish
cd /home/glassfish/

#Create downloads directory 
mkdir downloads

#Go to downloads directory
cd /home/glassfish/downloads/

#Download and install glassfish 3.1.2
unzip glassfish-3.1.2.zip

#Move extracted contents to glassfish home directory
mv /home/glassfish/downloads/glassfish3.1.2/* /home/glassfish/

#Exit from glassfish user
exit


#change group of glassfish home directory to glassfishadmin

sudo chgrp -R glassfishadmin /home/glassfish


#change owner of glassfish home directory to glassfish

sudo chown -R glassfish /home/glassfish
#make sure the files are executable/modifyable/readable for owner and group

sudo chmod -R ug+rwx /home/glassfish/bin/
sudo chmod -R ug+rwx /home/glassfish/glassfish/bin/
#others are not allowed to execute/modify/read them
sudo chmod -R o-rwx /home/glassfish/bin/
sudo chmod -R o-rwx /home/glassfish/glassfish/bin/

#Now you can start and stop glassfish
#Switch over to glassfish user
sudo su glassfish

#Start glassfish
/home/glassfish/bin/asadmin start-domain domain1

#Stop glassfish
/home/glassfish/bin/asadmin stop-domain domain1

#Exit
exit

Configure glassfish init script


#Now you need an init script to automatically start glassfish on system reboot.

#Create init script
sudo nano /etc/init.d/glassfish


#(paste the lines below into the file and save it)


#! /bin/sh


export AS_JAVA=/usr/lib/jvm/jdk1.7
GLASSFISHPATH=/home/glassfish/bin
case "$1" in
start)
echo "starting glassfish from $GLASSFISHPATH"
sudo -u glassfish $GLASSFISHPATH/asadmin start-domain domain1
;;
restart)
$0 stop
$0 start
;;
stop)
echo "stopping glassfish from $GLASSFISHPATH"
sudo -u glassfish $GLASSFISHPATH/asadmin stop-domain domain1
;;
*)
echo $"usage: $0 {start|stop|restart}"
exit 3
;;
esac
:

#To start this init script automatically issue the following commands
#Make the init script executable
sudo chmod a+x /etc/init.d/glassfish

#configure Glassfish for autostart on linux boot
sudo update-rc.d glassfish defaults

#if apache2 is installed, remove that from startup
#stopping apache2
sudo /etc/init.d/apache2 stop
#removing apache2 from autostart
update-rc.d -f apache2 remove

#Now we can start glassfish easily be the following commands
#start
/etc/init.d/glassfish start
#stop
/etc/init.d/glassfish stop
#restart
/etc/init.d/glassfish restart