Java socket connection timout

March 13th, 2014 by xrigher No comments »

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.

常用命令

March 4th, 2014 by xrigher No comments »

Linux

  • find /home -iname “*.log” -exec rm {} \;

    Delete file recursively by extension.

Windows

Memo: tomcat configurations

December 7th, 2013 by xrigher No comments »

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 by xrigher No comments »

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>

memo: install nginx on ubuntu and use sock

October 9th, 2013 by xrigher No comments »

$ sudo apt-get install nginx
$ sudo apt-get install php5 php5-fpm ...

edit /etc/php5/fpm/pool.d/www.conf, and chenge
listen = 127.0.0.1:9000
to
listen = /var/run/php-fpm.sock

庖丁分词自定义词库

July 18th, 2013 by xrigher No comments »

注意分词格式

庖丁分词支持自定义词库,但是有一点要注意:
词库文件必须保存为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格式。

Configure Tomcat 7 & Eclipse on Ubuntu

June 28th, 2013 by xrigher 3 comments »

Install Eclispe

Either via sudo apt-get install eclipse and then install Web Tools Platform for JavaEE, or download Eclipse for JavaEE.

Install tomcat

We can also install tomcat in 2 ways:
1. sudo apt-get tomcat7
2. download tomcat7 and extract it into folder, such as ~/Prog/tomcat

Integrate Eclipse and Tomcat

When I run the project with “Run On Server” and follow the instruction of “Define a new server” with tomcat v7, I always get this error message: “Cannot create a server using the selected type”. Well, here is the solution: http://oramirez.wordpress.com/2012/02/03/eclipse-with-tomcat-v7-0-in-ubuntu-11-10/.

And then somehow I encounter this error: Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete., and http://www.jusuchyne.com/codingchyne/2010/10/installing-tomcat-7-on-ubuntu/ tells that it’s because eclipse failed to copy the conf files into eclipse tomcat conf. so:
1. first sudo chmod -R 777 ~/Prog/tomcat7/conf;
2. and delete the server in Eclipse Perferences -> Server -> Runtime Environment, re-add it;
3. and then re-define the server at “Run on Server”.

Simple Example of Java JSON Encoding in Servlet

May 17th, 2013 by xrigher 1 comment »

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 by xrigher 9 comments »

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 by xrigher 2 comments »

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.