Archive for the ‘java’ category

Java socket connection timout

March 13th, 2014

There are 2 places to set the timeout option of a socket, at connection and at reading.

  • Socket.connect(SocketAddress endpoint, int timeout) connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs. Here the timeout setting only affects the action of connecting!
  • Socket.setSoTimeout(int timeout) enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Memo: tomcat configurations

December 7th, 2013

Here are some common used configurations of tomcat.

  • Add a new tomcat host. In server.xml, add these lines:
    
    		<Host name="youdomain.com" appBase="/var/www/domain_webapps_dir"
                    unpackWARs="true" autoDeploy="false">
                    <Context path="" docBase="/var/www/domain_webapps_dir/proect_name" reloadable="true" />
                    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                            prefix="xxx_access_log." suffix=".txt"
                            pattern="%h %l %u %t "%r" %s %b" />
                    </Host>
                    

Ant build script for java web project with tomcat on ubuntu

December 7th, 2013

Here is a sample ant build script for java web project with tomcat on ubuntu. It should be put under the root directory of the web project.



<?xml version="1.0" encoding="UTF-8"?>
<project name="ProjectName" default="war" basedir=".">
	<property environment="env" />
	<property name="webapp.name" value="ProjectName" />
	<property name="webapp.dir" value="/data/public/JavaWebapps" />
	<property name="tomcat.lib" value="/usr/share/tomcat7/lib" />
	<property name="ant.lib" value="/usr/share/ant/lib" />
	
	<property name="dist.dir" value="${basedir}/war" />
	<property name="webRoot.dir" value="${basedir}/WebContent" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	
	<!-- Init classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<fileset dir="${tomcat.lib}">
			<include name="*.jar" />
		</fileset>
		<!-- ant lib  -->
		<fileset dir="${ant.lib}">
			<include name="*.jar" />
		</fileset>
	</path>

	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
             property="echo.path.compile"
             refid="project.classpath">
	</pathconvert>
	
	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	</target>
	
	<target name="clear" description="Clear the project">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${webapp.name}/${webapp.name}.war" />
		<delete dir="${webapp.name}/${webapp.name}" />
	</target>

	<target name="init" depends="clear" description="Init the directories">
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />
	</target>

	<target name="compile" depends="init" description="Compile source files">
		<echo message="begin compile..." />
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			target="1.7" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			>
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		</javac>
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			</fileset>
			<fileset dir="${config.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			</fileset>
		</copy>
		<echo message="end compile..." />
	</target>

	<!--  
	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${webapp.name}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 
	            </fileset> 
	        </jar> 
	    </target> 
	-->

	<target name="war" depends="compile" description="Package the project as .war file.">
		<echo message="begin packing war..." />
		<war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" 
			webxml="${webRoot.dir}/WEB-INF/web.xml">
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
			</fileset>
		</war>
		<echo message="end war..." />
	</target>

	<target name="deploy" depends="war" description="Deploy the war to tomcat">
		<echo message="begin deploy..." />
		<copy file="${dist.dir}/${webapp.name}.war" todir="${webapp.dir}" />
		<echo message="end deploy..." />
	</target>

</project>

庖丁分词自定义词库

July 18th, 2013

注意分词格式

庖丁分词支持自定义词库,但是有一点要注意:
词库文件必须保存为UTF-8格式。在windows下用记事本创建的文件默认为ASCII编码,因此里面的词不会被识别。

自定义分词步骤

  1. 在paoding-dic-home.properties文件中配置自定义词库的位置。
    • 首先配置paoding.dic.home.config-fisrt。其有2个选项:system-env和this。
      • paoding.dic.home.config-fisrt=system-env 表示使用系统变量。如果使用这个,那个需要配置环境变量 PAODING_DIC_HOME 为字典所在目录。
      • paoding.dic.home.config-fisrt=this 表示使用本配置文件里面的配置。如果使用这个,需要在本配置文件中继续配置 paoding.dic.home。
    • 配置paoding.dic.home (如果在上面选择了paoding.dic.home.config-fisrt=this才需要),也有2种方法:使用相对路径和绝对路径
      • paoding.dic.home=classpath:dic 使用相对路径。表示使用本项目的classpath中列出的任意一个文件夹,然后把dic文件夹放置其中。一般在eclipse项目中,把dic文件夹放在源码文件夹(src)中。
      • paoding.dic.home=D:/somepath/dic 使用绝对路径。
  2. 配置好路径之后,可以在dic文件夹中新建任意以.dic为后缀的词库文件。每行一个词。还是注意:必须保存为UTF-8格式。

Simple Example of Java JSON Encoding in Servlet

May 17th, 2013

Here is a simple example showing how to output json in a servlet. I’ll use the “JSON-java” package which is recommended by http://json.org/java/ and can be downloaded at https://github.com/douglascrockford/JSON-java. To encode Java objects into a JSON string, we simply use org.json.JSONObject and org.json.JSONArray.java.

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * A simple example of showing how to output json in a Servlet.
 */
@WebServlet(description = "Test servlet showing how to output json in Java", urlPatterns = { "/services/testjson" })
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    
		response.setContentType("application/json");
		response.setHeader("Cache-Control", "nocache");
        	response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		JSONObject json = new JSONObject();

		// put some value pairs into the JSON object as into a Map.
		json.put("status", 200);
		json.put("msg", "OK");

		// put a "map" 
		JSONObject map = new JSONObject();
		map.put("key1", "val1");
		map.put("key2", "val2");
		json.put("map", map);
		
		// put an "array"
		JSONArray arr = new JSONArray();
		arr.put(5);
		arr.put(3);
		arr.put(1);
		json.put("arr", arr);

		// finally output the json string		
		out.print(json.toString());
	}
}

There is another package called json-simple which is also very handy and simple. More information about json-simple can be found at https://code.google.com/p/json-simple/.

Install Lucene Solar with Tomcat on Windows

May 7th, 2013

Installing the example solar web application is actually super easy. Here we suppose you already installed the Tomcat sevlet container on your computer TOMCAT_HOME (for example: D:\prog\apache-tomcat-7.0.35). If not, you can download it here http://tomcat.apache.org and install it (at TOMCAT_HOME).

The next step is to install Solar:

  1. download Solr at: http://lucene.apache.org/solr/
  2. extract the solar package into, for example, solr-4.3.0
  3. copy the sample “Solr Home” directory solr-4.3.0\example\solr\ (Note that it’s the folder ‘example\solr\’ itself, not the content under it!!!) into the home directory of tomcat: TOMCAT_HOME if you start tomcat with Tomcat Monitor or into TOMCAT_HOME\bin if you start tomcat with TOMCAT_HOME\bin\startup.bat. Note that the solr hoe directory must be put in the right directory. This is very important!!! Otherwise you will get such an exception: “HTTP Status 500 – {msg=SolrCore ‘collection1′ is not available due to init failure: Could not load config for solrconfig.xml …… ”
    In a word, the solr home directory should be put in the Java current working directory. You can also “Configure the servlet container such that a JNDI lookup of “java:comp/env/solr/home” by the Solr webapp will point to the Solr home”.
  4. copy the solr war file (solr-N.N.N.war) under solr-4.3.0\dist\ as solr.war into the tomcat webapps directory TOMCAT_HOME/webapps. Tomcat will automatically deploy it.
  5. After solr.war is extracted, copy the SLF4j logging jars from solr-4.3.0/example/lib/ext into TOMCAT_HOME/lib or TOMCAT_HOME/webapps/solr/WEB-INF/lib. If this step is ignored, this exception will be thrown out: org.apache.catalina.core.StandardContext filterStart
    SEVERE: Exception starting filter SolrRequestFilter
    org.apache.solr.common.SolrException: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see: http://wiki.apache.org/solr/SolrLogging

OK. That’s it! Now locate your web browser to http://localhost:8080/solr (Change the port if necessary) and you will see the admin page of solr.

If there are exceptions, check your tomcat logs (such as localhost.YYYY-MM-DD.log) under TOMCAT_HOME\logs\.

PS. actually to install solar on windows is not so much different from that on any other OS. :)

More details can be found on the Solr Wiki: http://wiki.apache.org/solr/SolrInstall

Java with UTF-8 encoding (especially with Chinese)

May 5th, 2013

Here are just some memos on how to deal with non-unicode characters (like Chinese) in java.

  • Decode characters from URL in a servlet:
    String s = request.getParameter("mykeywords");
    s = new String(s.getBytes("ISO-8859-1"), "UTF-8");

    But if the uri encoding is specified in server.xml as:
    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>,
    there is no need to decode it. Simply use:
    String s = request.getParameter("mykeywords"); and s will be utf-8 encoded.

  • Send response with UTF-8:
    response.setCharacterEncoding("UTF-8");
  • Connect mysql with UTF-8:
    private static final String DB_URL = "jdbc:mysql://DB_HOST:3306/DB_SCHEMA?useUnicode=true&characterEncoding=utf8";
  • To encode JSP files with UTF-8, put this at the beginning of a JSP file:
    <%@ page contentType="text/html;charset=UTF-8" %>
  • With SpringMVC, for GET requests, set <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>,.
    And for POST requests, set this as the first filter in web.xml:
    <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
  • And finally, make sure every file itself is stored as UTF-8.

java client with multi entry

August 9th, 2011

The command to execute different jar entry:
java -cp project.jar p1.AnotherEntry

javaps – list java processes on linux

July 28th, 2011

here is a script that you can use to list java processes on linux.

http://www.cs.columbia.edu/~akonstan/javaps/javaps

here is code:

#!/bin/bash
#
# Copyright (c) 2000 Alexander V. Konstantinou 
#
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation. Alexander V. Konstantinou makes no
# representations about the suitability of this software for any
# purpose. It is provided "as is" without express or implied warranty.
#
# Description: prints out the java processes running on a Linux host.
# Optional "-noflags" argument controls verbosity
#
# Requirements: Linux host with a /proc filesystem
#
# Updates: http://www.cs.columbia.edu/~akonstan/javaps
#
# $Id: javaps,v 1.3 2002/01/21 02:32:12 akonstan Exp $

PATH=/bin:/usr/bin

SYSNAME=`uname -s`

if [ "$SYSNAME" != "Linux" ]; then
echo "$0: must be executed on Linux system (found $SYSNAME)" >&2
exit 1
fi

if [ ! -d "/proc" ]; then
echo "$0: Linux host does not appear to have a /proc filesystem" >&2
exit 1
fi

NOFLAGS=0
if [ -n "$1" ]; then
if [ "$1" = "-noflags" ]; then
NOFLAGS=1
else
echo "Usage: $0 { -noflags }" >&2
exit 1
fi
fi

echo "PID Process"

# For every file in the /proc file system

FILES=`/bin/ls -1 /proc`

for FILE in $FILES; do
PROCESS_ID=$FILE
STATUS_FILE=/proc/$FILE/status
CMDLINE_FILE=/proc/$FILE/cmdline

# Check if it is a process directory and that we have read access
if [ -d "/proc/$FILE" -a -r $STATUS_FILE -a -r $CMDLINE_FILE -a "$FILE" != "/proc/self" ]; then

PROCESS_NAME=`grep 'Name:' $STATUS_FILE | awk '{print $2}'`

# Only interested in java processes
if [ "$PROCESS_NAME" = "java" ]; then
PARENT_PID=`grep 'PPid' $STATUS_FILE | awk '{print $2}'`

# Figure out if process has a parent that is a java process
ISROOT=0
if [ $PARENT_PID -eq 1 ]; then
ISROOT=1
else
PARENT_NAME=`grep 'Name:' /proc/$PARENT_PID/status | awk '{print $2}'`
if [ "$PARENT_NAME" != "java" ]; then
ISROOT=1
fi
fi

# If root java process print out
if [ $ISROOT -ne 0 ]; then
PROCESS_CMDLINE="`cat $CMDLINE_FILE | tr '00' ' '`"

IGNORE_ARG=0
DESCR=
for ARG in $PROCESS_CMDLINE; do
if [ $NOFLAGS -eq 0 ]; then
DESCR="$DESCR $ARG"
else
if [ $IGNORE_ARG -eq 0 ]; then
if [ $ARG = "-classpath" -o $ARG = "-cp" ]; then
IGNORE_ARG=1
elif [ -z "`echo $ARG | grep '^-D'`" -a -z "`echo $ARG | grep '^-X'`" ]; then
DESCR="$DESCR $ARG"
fi
else
IGNORE_ARG=0
fi
fi
done
echo "$PROCESS_ID $DESCR"
fi
fi
fi
done


setting java environment under ubuntu

May 20th, 2010

you can set all the environment vars in /etc/environment.

for example:

PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games”
JAVA_HOME=”/usr/lib/jvm/java-6-sun/jre”
CLASSPATH=”.:$JAVA_HOME/lib:/home/xxxxxx/other-packages/lucene-3.0.1″

UPDATE:

use bash> source /etc/environment to refresh the system variables.