月度存档: 10月 2013

64k大赛作品

请参考以下两个网址:

http://breakpoint.untergrund.net

http://www.scene.org

maven中导出所依赖的第三方包

mvn dependency:copy-dependencies -DoutputDirectory=lib   -DincludeScope=compile

将所有的使用的第三方包导出于lib目录下。

 

游戏业务逻辑拆分,mvn war包引用war包解决方案

游戏服务器端打为了一个war包。

这次做项目时,希望将游戏中的通用模块及通用业务模块拆分至一个公共项目中,然后每个游戏可以分别建立一个项目来引用公共项目,可以只需要写业务逻辑,而不再需要关心其它的方面。

2013.11.06 记:

其实前几天已经解决掉了最后一个遇到的问题:这里还要感谢run jetty插件的作者。

现在开始动手进行项目改造:

  1. 这里核心工程定义为xcore,定义依赖工程文件xcore-dep,提供用来进行编译所需要的类。
  2. 定义另一个工程xcore-game,该工程为实际发布的游戏包。这里放在这里是为了调试xcore工程。以后开发新游戏时,直接复制xcore-game包即可。
  3. 定义第三个工程xcore-moon,测试工程,用来测试更为复杂的功能。(这个等前两个工程完工后再完成).
  4. 先在xcore工程下尝试运行看看能否运行。mvn、jetty:run及jetty:run-war。全部运行通过。
  5. 在xcore中的pom.xml中添加以下代码,用来生成xcore中的包含类的jar包
    			<plugin>
    				<artifactId>maven-war-plugin</artifactId>
    				<version>2.4</version>
    				<configuration>
    					<attachClasses>true</attachClasses>
    				</configuration>
    			</plugin
  6. 在xcore-game工程中修改pom.xml
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-war-plugin</artifactId>
    				<version>2.4</version>
    				<configuration>
    					<useCache>false</useCache>
    					<workDirectory>target/overlay-war-folder</workDirectory>
    					<overlays>
    						<overlay>
    						</overlay>
    						<overlay>
    							<groupId>cn.XXXXX</groupId>
    							<artifactId>xcore</artifactId>
    						</overlay>
    					</overlays>
    				</configuration>
    			</plugin>
  7. 运行xcore-game工程,遇到以下错误
    [ERROR] Nested in org.springframework.beans.factory.BeanCreationException: Error
     creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' d
    efined in file [D:\workspace\xcore-game\target\tmp\webapp\WEB-INF\classes\applic
    ationContext-mybatis.xml]: Initialization of bean failed; nested exception is ja
    va.lang.NoSuchFieldError: NULL:
    java.lang.NoSuchFieldError: NULL
            at org.springframework.expression.TypedValue.<clinit>(TypedValue.java:31
    )

    打开生成的war包,发现里面spring-expression引入了两个不同的版本,修改pom.xml将旧版本排除掉。OK,顺利解决问题。

  8. 奇怪,前几天明明ren jetty插件可以正常运行,现在竟然跑不起来了。有空再查吧,先用jetty:run-war凑活。
  9. run jetty插件失效的原因找到了,在原工程里面mvn可以成功编译工程,现在新的工程中,依赖eclipse编译源码,mvn无法成功编译源码,无法编译的原因后续再查。(原因查找到了,原来我是copy过来的pom.xml,里面的包名没有修改导致的)

maven中直接添加jar包

<dependency>
<groupId>cn.kingsoft</groupId>
<artifactId>game-server</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/game-server-1.0.jar</systemPath>
</dependency>

maven中添加war包依赖

maven中添加war包依赖

<project>
  ...
  <build>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.appfuse.plugins</groupId>
                    <artifactId>maven-warpath-plugin</artifactId>
                    <version>2.2.2-SNAPSHOT</version>
                </plugin>
            </plugins>
        </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
      <plugin>
           <groupId>org.appfuse</groupId>
                <artifactId>maven-warpath-plugin</artifactId>
                <version>2.0.2</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <goals>
                            <goal>add-classes</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

通常会再添加以下配置将war包中的lib包过滤掉:

                        <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<dependentWarExcludes>WEB-INF/lib/*</dependentWarExcludes>
				</configuration>
			</plugin>

添加war包依赖

       <dependency>
            <groupId>cn.kingsoft</groupId>
            <artifactId>game-web</artifactId>
            <version>1.0</version>
            <type>warpath</type>
        </dependency>

cmd命令中显示utf字符

自从项目中所有文件变为utf8字符集后,cmd中就再也没有看到过正确的中文显示。因为cmd默认使用gbk字符集。

以下操作将cmd改为utf8字符集。

1、打开CMD.exe命令行窗口
2、通过 chcp命令改变代码页,UTF-8的代码页为65001(gbk 936)
chcp 65001
执行该操作后,代码页就被变成UTF-8了。但是,在窗口中仍旧不能正确显示UTF-8字符。
3、修改窗口属性,改变字体
在命令行标题栏上点击右键,选择”属性”->”字体”,将字体修改为True Type字体”Lucida Console”,然后点击确定将属性应用到当前窗口。

把java安装为服务

将java安装为服务,强烈推荐common daemon:

http://commons.apache.org/proper/commons-daemon/procrun.html

apache上的多个java项目都使用这个安装为windows服务。

用法很简单:

java类中的main函数中使用start和stop来做为启动和停止指令:

public static void main(String[] args) {
		System.out.println("start Pdf2Swf Service main with args:" + args);

		String mode = "start";
		if (args != null && args.length > 0) {
			mode = args[0];
		}
		if ("start".equals(mode)) {
			// 启动主线程
			Pdf2SwfService pdf2SwfService = new Pdf2SwfService();
			pdf2SwfService.run();
			Pdf2SwfService.staticInstance = pdf2SwfService;
		} else if ("stop".equals(mode)) {
			if (Pdf2SwfService.staticInstance != null) {
				try {
					Pdf2SwfService.stop = true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

下面编译安装服务的脚本:

@echo off
cd /d %~dp0
set PR_PATH=%CD%
SET PR_SERVICE_NAME=Pdf2Swf
SET PR_JAR=pdf2swf.jar
SET START_CLASS=org.haifi.Pdf2SwfService
SET START_METHOD=main
SET STOP_CLASS=java.lang.System
SET STOP_METHOD=exit
rem ; separated values
SET STOP_PARAMS=0
rem ; separated values
SET JVM_OPTIONS=-Dapp.home=%PR_PATH% -out %CD%\stdout.log -err %CD%\stderr.log -current %CD%

echo %PR_PATH%

prunsrv.exe //IS//%PR_SERVICE_NAME% --DisplayName="%PR_SERVICE_NAME%" --Install=%PR_PATH%\prunsrv.exe --LogPath=%PR_PATH%\logs --LogLevel=Debug --StdOutput=auto --StdError=auto --StartMode=Java --StopMode=Java --Jvm=auto --StartMode=jvm --StartClass=%START_CLASS% ++StartParams=start --StopMode=jvm --StopClass=%STOP_CLASS% ++StopParams=stop --Classpath="%PR_PATH%\%PR_JAR%"  ++JvmOptions=%JVM_OPTIONS%
pause


上面的类换为自己写的主类名。

如果启动不起来时,注意一下如果jdk为32位版本时,需要使用32位的prunsrv。

如果使用64位的prunsrv,则必须使用64位的jdk版本。

不然会直接提示无法启动,但又不会有任何有用的提示信息。

获得bat文件所在的目录

cd /d %~dp0

编写bat必备内容。

voltdb安装及使用

1、下载voltdb后直接解压:

$ tar -zxvf LINUX-voltdb-ent-3.6.tar.gz

2、两点注意事项,切记切记:

1.1.

Disable Swapping

Swapping is an operating system feature that optimizes memory usage when running multiple processes. However, memory is a critical component of the VoltDB server process. Any contention for memory, including swapping, will have a very negative impact on performance and functionality.

We recommend using dedicated servers and disabling swapping when running the VoltDB database server process. Use the swapoff command to disable swapping on Linux systems. If swapping cannot be disabled for any reason, you can reduce the likelihood of VoltDB being swapped out by setting the kernel parameter vm.swappiness to zero.

voltdb是内存数据库,常驻内存非常重要,因此需要将交换分区(虚拟内存)完全关闭。可以使用swapoff命令关闭,如果无法使用swapoff命令,则将内核参数vm.swappiness修改为0。

1.2.

Turn off TCP segmentation offload and generic receive offload if cluster stability is a problem.

There is an issue where, under certain conditions, the use of TCP segmentation offload (TSO) and generic receive offload (GRO) can cause nodes to randomly drop out of a cluster. The symptoms of this problem are that nodes timeout — that is, the rest of the cluster thinks they have failed — although the node is still running and no other network issues (such as a network partition) are the cause.

Disabling TSO and GRO is recommended for any VoltDB clusters that experience such instability. The commands to disable offloading are the following, where N is replaced by the number of the ethernet card:

ethtool -K ethN tso off
ethtool -K ethN gro off

Note that these commands disable offloading temporarily. You must issue these commands every time the node reboots.

这个是使用集群时需要注意的事项。

3、如果是旧版本升级需要按以下顺序执行:

The process for upgrading VoltDB for a running database is as follows:

    Place the database in admin mode using the @Pause system procedure (or VoltDB Enterprise Manager).

    Perform a manual snapshot of the database (using @SnapShotSave).

    Shutdown the database (using @Shutdown).

    Upgrade VoltDB.

    Recompile the application catalog using the new version of VoltDB.

    Restart the database using the create option, the new catalog, and starting in admin mode (specified in the deployment file).

    Restore the snapshot created in Step #2 (using voltadmin restore).

    Return the database to normal operations (using voltadmin resume).

4、将voltdb添加至环境变量中.

JAVA_HOME=/opt/jdk1.7.0_40
VOLTDB_HOME=/opt/voltdb-3.5.0.1
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$VOLTDB_HOME/bin

export JAVA_HOME VOLTDB_HOME PATH

5、以下开始学习使用简单的voltdb命令。

先创建一个指定目录:helloworld

定义表结构:

CREATE TABLE HELLOWORLD (
   HELLO VARCHAR(15),
   WORLD VARCHAR(15),
   DIALECT VARCHAR(15) NOT NULL,
   PRIMARY KEY (DIALECT)
);

Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048