Wednesday, October 3, 2012

How to build Maven Repository


  1. Download nexus war file from http://www.sonatype.org/downloads/nexus-2.1.2.war
  2. Rename nexus-2.1.2.war to nexus.war. 
  3. Put nexus.war into your tomcat webapp folder. 
  4. Start tomcat server 
  5. Login your nexus server:  http://127.0.0.1:8080/nexus (the default user/password: admin/admin123)
  6. Use the web UI to upload your jar file, as follows: 
  • Select the "3rd party" repository
  • Choose the "Artifact Upload" tab. 
  • Follow the steps in the screen to choose your jar file to upload. 

  • Choose the "Browse Storage" tab to check your uploaded jar file
  • Double click your uploaded jar file (i.e, Test-1.jar) will show the dependency content for pom.xml

In you pom.xml, you need to add the following content to connect your maven repository: 
  • <repositories>
  • <repository>
  • <id>VPDC</id>
  • <name>VPDC Repository</name>
  • <url>http://172.16.3.139:8080/nexus/content/groups/public/</url>
  • </repository>
  • </repositories>
To get your uploaded jar file (i.e., Test.jar), you also need to add the following content in your pom.xml: 
  • <dependency>
  • <groupId>org.springframework</groupId>
  • <artifactId>spring-hibernate3</artifactId>
  • <version>2.0.8</version>
  • </dependency>

Sunday, September 9, 2012

Build Maven Web Project from Eclipse

  •  Open your eclpse, New->Others, choose "Maven Project" and pres Next.
 
  •  Check "Create a simple project (skip archetype selection)" and press Next.
  •  Fill "Group Id", "Artifact Id" fields, and choose "war" in Packaging drop list. 
  • Press Finish.
  •  Since Maven didn't generate the web.xml, you have to build this file in path (src/main/java/webapp) manually.





Tomcat Installation

For Windows
  1. Download Tomcat package from http://tomcat.apache.org/download-60.cgi
  2. Unzip this file to generate corresponding folder (e.g., Tomcat)
  3. In Tomcat/bin, you can find the startup.bat file. 
  4. Double click statup.bat and use http://localhost:8080 for testing, as follows: 

Install MySQL

For windows: 
  1. Download Appserv (e.g., appserv-win32-2.5.10.exe) form http://www.appservnetwork.com/
  2. Double click appferv-win32-2.5.10.exe to install. 
  3. press next, next, then finish.
  4. Finally, you can use the http://localhost to check. 
    •  In the above page, you can click "phpMyAdmin Database Manger" to enter mysql server (admin type root, and password as your setting), as follows:









Saturday, September 8, 2012

Hibernate Code Generation

Hibernate Code Generation
  • Before code generation, you have a database server includes database with some tables. Here, we have installed MySQL and built a database (i.e. chtrh) with some tables (i.e., user, stock). 
  • There are two required files and one console configuration (i.e., Hibernate Configuration File, Hibernate Reverse Engineering File, and Hibernate Console Configuration) for code generation.
    • Create Hibernate Configuration file and name as hibernate.cfg.xml, then press next.

    •  Set your database url, username and password, then press finish. 

    • Create Hibernate Console Configuration (maybe you can't find this file in eclipse). 
      • Here, you have to choose the location of hibernate.cfg.xml. 
      • Select the "Annotations (jdk 1.5+)" radio button, then press finish. 

    • Create Hibernate Reverse Engineering file and name as hibernate.reveng.xml then press next. 
    • In the "Type Mappings" tab, you can press "Refresh" botton, and you can get the database and its tables, in here, you can free choose table which to be generated code.
    • In the "Table Filters" tab, you can press "Refresh" button, and you can get the database and its tables, in here, you can also free choose table which to be generated code.
    •  In the "Table and Columns" tab, you can press "Add" button, and you can get the database and its tables, in here, you can also free choose table or columns which to be generated code.


    • After generating hibernate.cfg.xml and hibernate.reveng.xml files and hibernate console configuration. Switch to Hibernate perspective view, and choose Hibernate Code Generation Configuration function.

    •  In the "Console configuration" drop list, you can choose console configuration.
    • In the Exporters tab, you have to choose output type (usually select Domain code, Hibernate XML Mappings, DAO code). 
    • If you prefer use hibernate annotation, you have to check the "Use Java 5 syntax" and "Generated EJB3 annotations"
    • Lastly, after pressing run button, the hibernate will generate threes (i.e.,Domain code, Hibernate XML Mappings, DAO code ) file for each table. 

Hibernate Eclipse Plugin

Hibernate Eclipse Plugin
  • Open Eclipse Marketplace and input "Hibernate", you can see the following screen. 

  •  Install Hibernate Tools depend on you eclipse version. In this article, we choose eclipse Indigo. 
  • After installation, you can see the Hibernate button in your perspective view.
   
  • The next article will introduce how to automatically generate code. 

Maven Installation

For command line:
  • Set the Maven/bin in your path. 
  • Type "mvn --version" for testing, the output are showed in below: 
  •  C:\Users\Stanley_PC>mvn --version
    Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)
    Java version: 1.6.0_24
    Java home: C:\Program Files\Java\jdk1.6.0_24\jre
    Default locale: zh_TW, platform encoding: MS950
    OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows

 For Eclipse Plug-in
  • Use the Eclipse Marketplace menu and input "Maven", you can get the following screen. 

  • Install the "m2eclipse-wtp: Maven Integration for Eclipse WTP" and  "Maven Integration for Eclipse", where WTP package for web project. 
  • Now, you can new a Maven project.
  

Monday, August 20, 2012

Peral Installation

For Linux:
1. Download Perl Distributions (the latest version is perl-5.16.1.tar.gz) from here.

2. Decompress the perl-5.16.1.tar.gz into your local disk
     tar -xvf perl-5.16.1.tar.gz

3. Install the Perl package in your computer:
    cd perl-5.16.1
    ./Configure -des -Dprefix=/usr/local (installed perl as /usr/local/bin)
    make
    make install

4. Using perl --version for testing perl installation, 

5.Build a perl program named as hello.pl and input the following code:
   print "Hello/n"; 
   print "World/n"; 
   print "This is my first perl program/n"; 

6. Perform perl hello.pl and the out as follows:
    Hello
    World
    This is my first perl program

Ref.: http://www.perl.org/get.html
        http://learnperl.scratchcomputing.com/
 

Tuesday, January 10, 2012

Memcached Java Client Simple Usage

1. Download library from here.
2. Import java_memcached-release_2.6.3.jar into your project.
3. Simple code for testing:

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheClient {
static{
String[] serverList = {"localhost:11211"};
Integer[] weight = {3};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverList); 
pool.setWeights(weight);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000*60*60*6);
pool.initialize();
}
public static void main (String args[]){
MemCachedClient mcc = new MemCachedClient();
mcc.set("testA", "AA");
mcc.set("testB", "BB");
System.out.println(mcc.get("testA"));
System.out.println(mcc.get("testB"));
mcc.delete("testA");
System.out.println(mcc.get("testA"));
}
}


Sunday, January 8, 2012

Memcached Server Installation

For Windows
1. download memcached for windows (memcached-1.2.6-win32-bin.zip) from here
2. unzip memcached-1.2.6-win32-bin.zip in any folder (e.g., D:\memcached-1.2.6-win32\).
3. install: D:\memcached-1.2.6-win32\memcached.exe -d install.
4. start: memcached.exe -d start.



Yum install
1. using yum to install memcached
# yum install memcached
2. 開機啟動 memcached 設定
# chkconfig memcached on
3. 啟動 memcached server 
# service memcached start
4. 簡單測試 memcached service
# echo stats | nc localhost 11211
會產生以下內容: 
STAT pid 13576
STAT uptime 1114
STAT time 1326179848
STAT version 1.4.10
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.008998
STAT rusage_system 0.013997
STAT curr_connections 13
STAT total_connections 16
STAT connection_structures 14
STAT reserved_fds 20
STAT cmd_get 113
STAT cmd_set 226
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 113
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 31426
STAT bytes_written 3403
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 40793
STAT curr_items 226
STAT total_items 226
STAT evictions 0
STAT reclaimed 0
END
5. 參數設定
# vi /etc/sysconfig/memcached
以下為預設參數值: 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
6. 防火牆設定
# vi/etc/sysconfig/iptables
增加以下內容
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
7. 重新啟動 memcached server
# service memcached restart


For Linux
1. 下载memcached-1.2.1.tar.gz和libevent-1.3b.tar.gz
2. 安装libevent
# tar xvfz  libevent-1.3.tar.gz
# cd libevent-1.3
# ./configure --prefix=/usr
# make & make install
# ls -al /usr/lib | grep libevent
3. 安装 memcached
# tar xvfz  memcached-1.2.5.tar.gz
#  cd  memcached-1.2.5
# ./configure --with-libevent=/usr
#  make & make install
#  ls -al /usr/local/bin/mem*
4. 將 libevent 安裝的/usr/local/lib 輸出到涵式庫路徑
# vi ~/.bash_profile
add the following sentence
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
5.  啟動 memcached: /usr/local/bin/memcached
# memcached  -d  -m 128 -l 192.168.152.143 -p 11211 -u root

Memcached Session Manager Installation

Environments: One lighttpd web server (192.168.56.1, installation), one memcaced server (192.168.56.2, installation), and two tomcat servers (192.168.152.140 & 192.168.152.143, here) with memcached session manager packages.

Tomcat server with memcached session manager package:
1. put the following eight jar files into your tomcat library folder (e.g., /usr/share/apache-tomcat-7.0.**/lib)
  • http://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.4.1.jar
  • http://spymemcached.googlecode.com/files/spymemcached-2.7.jar
  • http://memcached-session-manager.googlecode.com/files/kryo-1.03.jar
  • http://memcached-session-manager.googlecode.com/files/minlog-1.2.jar
  • http://memcached-session-manager.googlecode.com/files/reflectasm-0.9.jar
  • http://mirrors.ibiblio.org/pub/mirrors/maven2/asm/asm/3.2/asm-3.2.jar
  • http://github.com/downloads/magro/kryo-serializers/kryo-serializers-0.8.jar
  • http://memcached-session-manager.googlecode.com/files/msm-kryo-serializer-1.4.0.jar
2. add the following paragraph into context.xml (e.g., /usr/share/apache-tomcat-7.0.**/context.xml)
        <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
         memcachedNodes="n1: 192.168.56.2:11211"
         transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
* sometimes if you meet "com.esotericsoftware.kryo.SerializationException: Buffer limit exceeded reading object of type: java.util.concurrent.ConcurrentHashMap" problem, my solution is to change transcoderFactory from "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" to "de.javakaffee.web.msm.JavaSerializationTranscoderFactory"
3. before restart your tomcat servers, the memcached server and web server have to been started.

Test jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%><%@ page import="java.net.InetAddress;" %>
<%
    String server_ip = "";
    InetAddress inetAddress = InetAddress.getLocalHost();
    server_ip = inetAddress.getHostAddress();
    String abc = (String) session.getAttribute("abc");
    String in_session = "false";
    if (abc == null)  {
        session.setAttribute("abc", "value  in session");
        abc  = "value not in session";
        in_session = "false";
    } else {
        in_session = "true";
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world! This is <%=server_ip%><br>
session id : <%=session.getId()%> <br>
abc value in session ? <%=in_session%><br>
abc value = <%=abc%>
</body>
</html>


Ref:
http://napmas.blogspot.com/2011/06/tomcat-and-memcached-and-session.html
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration



Friday, January 6, 2012

Java Installation

For Linux
1. Download jdk-7u2-linux-64.rpm from here.
2. # rpm -ivh jdk-7u2-linux-64.rpm
3. Perform java and javac instruction.

For Windows
1. Download jdk-7u2-windows-64.exe from here
2. Double click jdk-7u2-windows-64.exe to install.
3. Set the environment variable (i.e., path & JAVA_HOME)
4. Perform java and javac in your command to test.

Thursday, January 5, 2012

Nginx as a Load Balancer

Environments: 
There are three servers. One has been installed nginx (192.168.152.142, here). The other two servers (192.168.152.140, 192.168.152.143) have been install tomcat server (here).
Scenario:
The http request be submitted to nginx server, then we take nginx server as a load balancer to assign this request to one of tomcat servers equally.
Configuration Nginx (Windows)
1. Add the following paragraph content into nginx.conf (C:\nginx\conf\nginx.conf)

http {
upstream examples{
ip_hash;
server 192.168.152.140:8080;
server 192.168.152.143:8080;
}
       server {
         listen       80;
         server_name  localhost;
         location / { proxy_pass http://examples;
                       }
       }
}
2. restart your nginx. 

Nginx Installation

For Windows
1. Download nginx-1.0.11-win64-setup.exe from here.
2. Double click nginx-1.0.11-win64-setup.exe to install.
3. Double click Start-nginx in C:\nginx\Start-nginx.

Lighttp as a Load Balancer

Environments: 
There are three servers. One has been installed lighttp (192.168.152.142, here). The other two servers (192.168.152.140, 192.168.152.143) have been install tomcat server (here).

Scenario:
The http request be submitted to lighttp server, then we take lighttpd server as a load balancer to assign this request to one of tomcat servers equally.

Configuration Lighttp (Linux)
1. Add the sentence (include "conf.d/proxy.conf")  into lighttp.conf (/etc/lighttpd/lighttpd.conf).
2. Add the sentence ("mod_proxy") into module.conf (/etc/lighttpd/modules.conf) server.modules section, as follows:
server.modules = (
  "mod_access",
  "mod_proxy",
#  "mod_alias",
#  "mod_auth",
#  "mod_evasive",
#  "mod_redirect",
#  "mod_rewrite",
#  "mod_setenv",
#  "mod_usertrack",
)

3. Add the following paragraph into proxy.conf (/etc/lighttpd/conf.d/proxy.conf)
  • $HTTP["url"] =~ "^/examples/" {
  • proxy.balance = "hash"
  •     proxy.server = (
  •         "" => (
  •                   ("host" => "192.168.152.140", "port" => 8080),
  •                   ("host" => "192.168.152.143", "port" => 8080)
  •               )
  •     )
  • }
where "examples" is your project name, you can even use ^ symbol to represent all projects.
4. restart your lighttpd
# service lighttpd restart 

Configuration Lighttp (Windows) 

1. uncomment "mod_proxy" in lighttpd-inc.conf (C:\Program Files (x86)\LightTPD\conf\lighttpd-inc.conf) at server.modules section, as follows:

server.modules              = (
                                "mod_access",
                                "mod_accesslog",
                                "mod_alias",
#                               "mod_auth",
#                               "mod_cgi",
#                               "mod_cml",
#                               "mod_compress",
#                               "mod_dirlisting",
#                               "mod_evasive",
#                               "mod_evhost",
#                               "mod_expire",
#                               "mod_extforward",
#                               "mod_fastcgi",
#                               "mod_flv_streaming",
#                               "mod_indexfile",
#                               "mod_magnet",
#                               "mod_mysql_vhost",
        "mod_proxy",
}

2. modify the content about your proxy location in lighttpd-inc.conf (C:\Program Files (x86)\LightTPD\conf\lighttpd-inc.conf), as follows:

proxy.server               = ( ".jsp" =>
                               ( "localhost" =>
                                 ("host" => "192.168.152.140","port" => 8080),
("host" => "192.168.152.143","port" => 8080)
                               )
                             )


3. restart your lighttd server. 



Lighttp Installation

For Linux
RPMForge x86_64 or 64bit install
rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
rpm -Uvh http://apt.sw.be/redhat/el5/en/x86_64/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
Install lighttpd
# yum install lighttpd
# chkconfig lighttpd on
Check that Apache is not running
# service httpd status
httpd is stopped
Start lighttpd server
# service lighttpd start
Lighttpd Default Paths
Directory Root (where to put your website files): /srv/www/lighttpd/
Lighttpd Config File: /etc/lighttpd/lighttpd.conf
Lighttpd log files: /var/log/lighttpd/


For Windows
Download LightTPD-1.4.29-1-Win32-SSL.exe from here
Double click LightTPD-1.4.29-1-Win32-SSL.exe to install
Check that lighttp is running
http://localhost
If lighttpd is not running
startup the service in [console > service]