Sunday, October 20, 2013

OCAJP7(1Z0-803) and OCPJP7(1Z0-804) Preparation

OCAJP7(1Z0-803) & OCPJP7(1Z0-804)

It was a looooong journey. I took OCAJP7 at the end of May and OCPJP at the middle of October. I was very suffering when I prepare for the exams, but everything is worth, it is guaranteed that the exams keep me growing. Now I can understand Java SE better and it get my Java skills increment.

Taking a glance of the exams, and experience the real exam level:
(1)
public static void main(String[]args){....}
public static void Main(String[]args){...}
public static void main(){...}

(2)
 try{
     throw new IOException();
}catch(Exception e){
     throw new FileNotFoundException();
}
which Exception will be thrown?
(3)
Files.copy(sourcePath, destinationPath);
Question: if the source file under sourcePath is hidden, what if we copy it to the destination path, is it Hidden?

What resources are required?
(1) The Java Tutorial
Programmer Level 1
Programmer Level 2
Those two links covers the topics of OCAJP7 and OCPJP7 respectively. Those links can only help you understand well which topics you should prepare, but if you only read those materials, it is far from passing the exams.

(2) SCJP6. This book is highly recommended. This books covers all the details of the topics it provided. Because of this book is for SCJP6 exam, some topics of OCAJP7/OCPJP7 are not covered by this book, for example: try-with-resource, string processing, File IO(NIO2), even though, it is still a good book for preparation.

(3)Oracle Certified Professional Java SE7. Java SE7 covers all the topics for OCPJP7, and it is as board/deep as the real exam. The topics not covered in SCJP6, they are involved by Java SE7. It has two mock tests, each has 90 questions, they can help you to estimate your level(The average score of those two mocks is very close to my OCPJP7 score :) ). Here's their official link: http://ocpjp7.blogspot.ca/, a tons of useful information are posted here.

My impression of the exams
1. Topics are not even distributed. Before I took the OCPJP7, I thought it would have a lot of questions are related to Generics and Threads, but when I sit in front of the real exam, I found only about 10 questions about Generics and Threads, and more than 15 questions are about File IO(NIO2) and String processing, and only 1 or 2 questions covered JDBC. Therefore, be careful when you prepare for your exams, you should understand all topics well.

2. Too many details
replaceAll(String regex, String replacement)
replace(char oldChar, char newChar)
Such kind of methods will be involved in your multiple choices questions, and you should know which is the best. For example, replace and replaceAll, they have the same functionality. The old characters will be replaced with the new one, replacing all the occurrence of old characters. When those two methods appear in the same question, you may think replace only replace the first occurrence of the oldChar, right?

Suggestions
1. Be patient when you prepare your exams. Provide enough time for your preparation. Don't register the exam first, you can prepare for it and when you feel you are ready, then go register.

2. Coding. Dont just read the books, it is not enough, you must practice each topic by yourself. If you do your practice, the compile time error and runtime error will be a piece of cake for you! You can use IDE or not, it is your choice, the thing is understanding why the codes is working or why it is not working.

3. The most important part is the topics are not even distributed. I thought the topics about Generics and Threads will have a lot of questions, but I was wrong, it had a lot of File IO questions and made me suffering.I believe when you read the third question about the Files.copy at the beginning of this post, you experienced it already

4. Don't buy any mock exams, it is not necessary. All the resources I suggested is enough for you get a good score. If you can understand them well, the exams are not  too challenging.

I hope this post helps. Best wishes for your exam:)

Friday, October 11, 2013

Java Generated Code - Protocol Buffers



1) Download protobuf from protobuf website.

2) Install gcc and gcc-c++
 > sudo yum install gcc gcc-c++

3) Install Protocol Buffer Compiler using C++ compiler.(You can read the README file of protobuf)
./configure
make 
make check
make install

4) Generate Java file
A. now we have test.proto file, it is:

message ProgrammerInfo
{
    required string name = 1;
    required bool likeCoding = 2;

    enum CodingLanguage{
        JAVA = 0;
        PERL = 1;
        JAVASCRIPT = 2;
    }

    required CodingLanguage programLang = 3 [default = JAVA];
}

B. command to generate java file:
protoc -I=/home/haifzhan/sourceFolder --java_out=/home/haifzhan/outputFolder /home/haifzhan/sourceFolder/test.proto

Now the file called Test.java is generated.

How to use it?

ProgrammerInfo info = ProgrammerInfo.newBuilder().setName("haifzhan")
                            .setLikeCoding(true)
                            .setProgramLang(ProgrammerInfo.CodingLanguage.JAVA)
                            .build();


Wednesday, October 9, 2013

Guake dual monitor setup


Guake dual monitor setup

Guake is a drop-down terminal, I replace default terminal with guake. It worked properly when I only have one monitor, but it has a bug when use dual monitors. It always pops out on my laptop monitor which is not what I want.

Here I will (1) show you how to install Guake on Fedora/CentOs, (2) How to make Guake display on your external monitor.

(1) How to install Guake?
> sudo yum install guake.

(2) How to make guake available for your external monitor?(Guake version 0.4.4-9.fc18)
> sudo vim /usr/bin/guake

If you are using vim viewing your /usr/bin/guake file, you can search the keyword get_final_window_rect(self): using "/get_final_window_rect(self)and replace the existed method with  the following method:

 def get_final_window_rect(self):
        """Gets the final size of the main window of guake. The height
        is the window_height property, width is window_width and the
        horizontal alignment is given by window_alignment.
        """
        screen = self.window.get_screen()
        height = self.client.get_int(KEY('/general/window_height'))
        width = 80
        halignment = self.client.get_int(KEY('/general/window_halignment'))

        # future we might create a field to select which monitor you
        # wanna use
        #monitor = 0 # use the left most monitor
        monitor = screen.get_n_monitors() - 1 # use the right most monitor

        monitor_rect = screen.get_monitor_geometry(monitor)
        window_rect = monitor_rect.copy()
        window_rect.height = window_rect.height * height / 100
        window_rect.width = window_rect.width * width / 100

        if width < monitor_rect.width:
            if halignment == ALIGN_CENTER:
                window_rect.x = monitor_rect.x + (monitor_rect.width - window_rect.width) / 2
            elif halignment == ALIGN_LEFT:
                window_rect.x = monitor_rect.x
            elif halignment == ALIGN_RIGHT:
                window_rect.x = monitor_rect.x + monitor_rect.width - window_rect.width

        window_rect.y = monitor_rect.y
        return window_rect



Now restart your Guake, It should work!

Monday, May 13, 2013

How to install Dropbox on Linux ?


1./etc/yum.repos.d

2. sudo vim dropbox.repo

3. past the following:

[Dropbox]
name=Dropbox Repository
baseurl=http://linux.dropbox.com/fedora/$releasever/
gpgkey=https://linux.dropbox.com/fedora/rpm-public-key.asc

4. sudo yum install nautilus-dropbox

Friday, May 10, 2013

[SOLVED] Error while loading shared libraries: libgtk-x11-2.0.so.0 fedora 18


How to solve the above problem?

this problem occured when I try to install Wine QQ on my Linux(Fedora 18)

The first thing to try is simply: `yum update`, then `yum install gtk2.i686`

Wednesday, April 24, 2013

show git branch on bash prompt

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

# User specific aliases and functions
# show git branch
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\[\033[00m\]\u@\h\[\033[01;33m\] \w \[\033[31m\]\$(parse_git_branch)\[\033[00m\]$\[\033[00m\] "

Monday, April 1, 2013

Setup Hadoop Cluster



Hadoop Cluster Setup in CentOS6

Requirement:
1.    Have java 1.6.x installed.
2.    Have ssh installed.

Installation & Configuratio[MUST be a root user]

1.    Download hadoop rpm file from apache hadoop official website.

2.    Install hadoop:
rpm –i hadoop_version.rpm

3.    Edit the file /etc/hosts on the servers:
192.168.1.40   master
192.168.1.41   slave1
192.168.1.42   slave2

4.    We must configure password less login from name node(master) to all data nodes (slave1 and slave2), on all servers do the following:
Ø  Command :ssh-keygen –tdsa
Ø  Keep press ENTER button until the id_dsa.pub file is generated.
We have 3 .pub files; one is on master, and others on the two slaves.

Copy the contents of those three .pub files to the authorized_keys file.
All servers authorized_keys file should have the same content.

5.    Open the file /etc/hadoop/hadoop-env.sh, and set the $JAVA_HOME: 
export JAVA_HOME=/usr/java/jdk1.6.0_38.

6.    Open the file /etc/hadoop/core-site.xml, add the following properties. This file is to configure the name node store information:




7.    Open the file /etc/hadoop/hdfs-site.xml and add the following properties:

8.    Open the file /etc/hadoop/mapred-site.xml, add the following properties. This file is to configure the host and port of the MapReduce jobtracker in the name node of the hadoop setup:




9.    Open the file /etc/hadoop/masters, add the namenode name: [NAMENODE SERVER ONLY]
master

10. Open the file /etc/hadoop/slaves, add all the datanodes names:[NAMENODE SERVER ONLY]
/* in case you want the namenode to also store data(i.e namenode also behave like a datanode) this can be mentioned in the salves file.*/
master
slave1
slave2

11. Modify files permissions.
Once Hadoop is installed, start-all.sh, stop-all.sh and several other files would be generated under /usr/sbin/, we must change all of those files permission:
           # sudo chmod a+x  file_name


Notice: Step 9 ,10 and 11 only for master server, the slaves should do nothing about those steps.

Start and Stop Hadoop Cluster (doing on hippo server)
1.    Formatting the namenode:
           # hadoop namenode –format
2.    Starting the Hadoop Cluster
           # start-all.sh

Run JPS command on master server:
          # jps 
            922 JobTracker
815 SecondaryNameNode
1062 TaskTracker
521 NameNode
1136 Jps

Run JPS command on slaves:
          # jps  
7407 DataNode
7521 TaskTracker
7583 Jps

3.    Checking the status of Hadoop Cluster:
(1)  Type the command :
hadoop dfsadmin –report
            (2) Browse the web interface for the NameNode (master server) and the JobTracker:
·      NameNode – http://192.168.1.40:50070/
·      JobTracker – http://192.168.1.40:50030/

4.    Process a sample to test Hadoop Cluster (wordcount example):
(1)  Create a directory in master server
mkdir input
          
(2) Create two test files under the ‘input’ directory and add the following text into the files
               echo "Hello haifzhan" >> text1.txt
               echo "Hello hadoop" >> text2.txt
               echo "Hello hadoop again" >> text3.txt

(3) Copy the two test files from master server to Hadoop’s HDFS
                  Under the ‘input’ directory:
                 # hadoop dfs -put ./   input
            (4)  Now you can check the files on Hadoop’s HDFS
                 # hadoop dfs -ls input/*
-rw-r--r--   2 root supergroup         15 2013-04-01 15:03 /user/root/input/text1.txt
-rw-r--r--   2 root supergroup         13 2013-04-01 15:03 /user/root/input/text2.txt
-rw-r--r--   2 root supergroup         19 2013-04-01 15:03 /user/root/input/text3.txt   

(5)  Run the MapReduce job
                 # hadoop jar /usr/share/hadoop/hadoop-example-1.0.3.jar wordcount input output
            (6)  Check the result
                 # hadoop dfs -cat output/part-r-00000
                        Hello  3
again  1
hadoop            2
haifzhan         1       
5.  Stopping the Hadoop Cluster
               # stop-all.sh

Other useful resources:
1. The logfiles locate in: /var/log/hadoop/root
2. Useful websites:
Error Solving:
1. Datanode: No route to host (start but then shut down automatically for a while)
           close the firewalls on both master and slaves machines
           # service iptables stop
2. Namenode: How to exit the safemode
           # hadoop dfsadmin -safemode leave
3. How to start datanode or tasktracker independently
            # hadoop-daemon.sh start datanode/tasktracker
4. How to check the current java version and the path of your local machine
            # echo $JAVA_HOME

5.  proccess information unavailable
remove all files under /tmp , reformate namenode and restart all servers.


Wednesday, March 20, 2013

Perf4j for TPS(transactions per second)

How to use Perf4j with Slf4j ?


I used to use the following code for counting TPS

long start = System.currentTimeMillis();

long timeTook = System.currentTimeMillis() - start;
System.out.println("Time took(milliseconds):  " + timeTook);
System.out.println("Successful output #: " + counter);
System.out.println("Average TPS: " + counter * 1000 / timeTook);

Actually it is not a good way to record TPS.

Now I will introduce a new way to counting TPS, it is Perf4j, it can be used both for log4j and slf4j,
log4j is the way which the perf4 offical website introduced.Now I will introduce how to use Perf4j with slf4j.
Please take a glance at my github repo for perf4j.

It is very easy to use, convenient to use, hope it helps.

Install GuestAddition for Fedora 18/19/20

Install guest additions for Fedora

I encountered this problem:

Unable to mount the CD/DVD image /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso on the machine fedora18. Would you like to force mounting of this medium?
Could not mount the media/drive '/Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso' (VERR_PDM_MEDIA_LOCKED).



How did I solve this problem?

Host: Mac 10.7.5
VM: Fedora 18


1.
change to the root user using "su"
2. 
yum install kernel* gcc -y
3.
cd /run/media/haifzhan/VBOXADDITIONS_xxx
4.
./VBoxLinuxAdditions.sh

Done!

Wednesday, February 27, 2013

Jenkins Installation w/ Git Support in CentOS 6


Jenkins Installation w/ Git Support in CentOS 6

Installation
First, we need to set up the Jenkins repository for CentOS.
# sudo wget –O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
# sudo yum install –y jenkins
Make sure git is installed and check the path:
# which git
Output: /usr/bin/git
If no output, do a:
# sudo yum install git

Start/Stop Service
Start Jenkins service by issuing:
# sudo service jenkins start
Enable Jenkins to start at boot by:
# sudo chkconfig jenkins on

Logging in to Jenkins Web UI and Install Git and Github Plugins
Access jenkins web interface by pointing your browser to:
Where “localmachine” is the ip address or hostname of the machine you installed jenkins on.
Next, click on “Manage Jenkins” on the left hand column, and on the next menu, “Manage Plugins”.
Click the “Available” tab and check the boxes next to “Git Plugin” and “Github Plugin”, then click “Download now and install after restart” at the bottom of the page.
Once the plugins are installed, check the box that says “Restart Jenkins when installation is complete and no jobs are running”.  Jenkins will now restart and the plugins will be installed.

Configure Security
Click on “Manage Jenkins” and on the next menu, “Configure Global Security”.
Check “Enable security”, and under “Security Realm”, click the radio button next to “Jenkins’s own user database”.  Under “Authorization”, click on “Matrix-based security”.
Add a user named “administrator” (or whatever you like) and once the user appears, check *all* the boxes under *all* the categories.
Click “Save” in the bottom left, and you will be taken to a sign up page.  Enter “administrator” under the username field, and fill in the rest of the fields.  You will then be logged in as administrator and have full control over Jenkins.

Giving Jenkins user Access to Github
First, we need to temporarily give the Jenkins user shell access so we can generate SSH keys in order to access github.
# sudo vim /etc/passwd
Change the following line from:
jenkins:x:496:492:Jenkins Continuous Build server:/var/lib/jenkins:/bin/false
To:
jenkins:x:496:492:Jenkins Continuous Build server:/var/lib/jenkins:/bin/bash
Save and close the file.
Then switch to jenkins user:
#sudo su - jenkins
At the bash shell, issue:
# ssh-keygen
Hit enter for all the options.
Once the keys are generated and you’re back at the bash prompt, type “exit”.
Next, we have to rename the key files by doing the following:
# sudo cp /var/lib/jenkins/.ssh/id_rsa.pub /var/lib/jenkins/.ssh/authorized_keys
Now we need to copy the key.
# sudo cat /var/lib/jenkins/.ssh/authorized_keys
Copy the entire output and add to the SSH Keys section of your github user profile, giving it a name such as “Jenkins ssh key”.

Lastly, we have to connect to the github repo that we’re going to use in order to accept github into Jenkins’ known_hosts file.  Do this by issuing:
# sudo su – jenkins
# /usr/bin/git ls-remote -h git@github.com:starscriber/MessageBroker.git HEAD
When a message comes up asking you to continue, type “yes”.
If it is successful, you should go back to the bash prompt after a few seconds.  You can now type “exit”.
Now edit the /etc/passwd file again, and change the jenkins user’s shell back to /bin/false.

Adding Github repo to Jenkins Web UI
Click “Manage Jenkins” and then “Configure System”.
Click on “Git installations” and change “Path to Git executable” to full git path from step 1: /usr/bin/git
Make sure “Manually manage hook URLs” at the bottom of the page is checked.
Click on “Save”.
Next, select “New Job”.
Put something under “Job name” ie: MyCompany_MyApplication, and select “Build a free-style software project”, and click OK.
On the next screen, enter the “GitHub project” URL, ie: https://github.com/mycompany/MyApplication/
Under “Source Code Management”, select “Git” and enter the SSH repository URL, ie: git@github.com:mycompany/MyApplication.git
Under “Build Triggers”, check the box next to “Build when a change is pushed to GitHub”.
Save.

Test Everything
Go back to Jenkins main page, and you should see a list of your Jobs.  If you hover your mouse on the Job Name, ie: Starscriber MessageBroker, you should get a dropdown list.  Click on “Build Now” and if successful, everything is working properly.