1 下载wrapper-windows-x86-32-3.2.2.zip并解压到{Wrapper Home}
2 在应用程序目录{JavaService}下创建以下文件夹
logs、lib、conf、bin
并拷贝文件:
{Wrapper Home}\lib\wrapper.dll、wrapper.jar
到
{JavaService}\lib\wrapper.dll、wrapper.jar
{Wrapper Home}\src\conf\wrapper.conf.in
到
{JavaService}\conf\wrapper.conf
{Wrapper Home}\bin\wrapper.exe
{Wrapper Home}\src\bin\*bat.in
到
{JavaService}\bin\wrapper.exe、*.bat
在{JavaService}\logs下面新建wrapper.log
3 {JavaService}下面创建文件
import java.io.*;
import java.net.*;
public class MainSvcThread {
public static void main(String[] args) {
try {
Socket server;
ServerSocket li = new ServerSocket(2022);
System.out.println("Echo service starting...");
while (true) {
server = li.accept();
new EchoThread(server).start();
}
} catch (Exception e) {
}
}
}
4 {JavaService}下面创建文件
import java.io.*;
import java.net.*;
/*实现每一个客户连接到此NT服务时的服务器端的线程单元逻辑*/
public class EchoThread extends Thread // 实现线程接口
{
Socket so = null;// 套接字
public EchoThread(Socket s) {
so = s;
}
public void run() {
String line;
DataInputStream in; // 套接字上的输入流
PrintWriter out; // 套接字上的输出流,带缓冲
try {
in = new DataInputStream(so.getInputStream());// 获取套接字的输入流
out = new PrintWriter(new DataOutputStream(so.getOutputStream()));
out.println("You have connected to EchoSvc!"); // 发送问候
out.flush(); // 必须刷新缓冲区内的内容
while ((line = in.readLine()) != null) {
line = line.trim();
if (!line.equalsIgnoreCase("quit")) {
out.println("ECHO: " + line);
out.flush();
} else
break;
}
in.close();
out.close();
} catch (IOException ioe) {
}
}
}
5 修改{JavaService}\conf\wrapper.conf:
#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=C:\Program Files\Java\jdk1.5.0_06\bin\java
# Java Main class. This class must implement the WrapperListener interface
# or guarantee that the WrapperManager class is initialized. Helper
# classes are provided to do this for you. See the Integration section
# of the documentation for details.
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
# Java Classpath (include wrapper.jar) Add class path elements as
# needed starting from 1
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/EchoService.jar
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
# Java Additional Parameters
#wrapper.java.additional.1==-Dprogram.name=run.bat
# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3
# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64
# Application parameters. Add parameters as needed starting from 1
wrapper.app.parameter.1=MainSvcThread
#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Format of output for the console. (See docs for formats)
wrapper.console.format=PM
# Log Level for console output. (See docs for log levels)
wrapper.console.loglevel=INFO
# Log file to use for wrapper output logging.
wrapper.logfile=../logs/wrapper.log
# Format of output for the log file. (See docs for formats)
wrapper.logfile.format=LPTM
# Log Level for log file output. (See docs for log levels)
wrapper.logfile.loglevel=INFO
# Maximum size that the log file will be allowed to grow to before
# the log is rolled. Size is specified in bytes. The default value
# of 0, disables log rolling. May abbreviate with the 'k' (kb) or
# 'm' (mb) suffix. For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=0
# Maximum number of rolled log files which will be allowed before old
# files are deleted. The default value of 0 implies no limit.
wrapper.logfile.maxfiles=0
# Log Level for sys/event log output. (See docs for log levels)
wrapper.syslog.loglevel=NONE
#********************************************************************
# Wrapper Windows Properties
#********************************************************************
# Title to use when running as a console
wrapper.console.title=Service For EchoThread
#********************************************************************
# Wrapper Windows NT/2000/XP Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
# using this configuration file has been installed as a service.
# Please uninstall the service before modifying this section. The
# service can then be reinstalled.
# Name of the service
wrapper.ntservice.name=MyEchoService
# Display name of the service
wrapper.ntservice.displayname=我的Echo服务程序
# Description of the service
wrapper.ntservice.description=我的第一个利用Wrapper包装的Java Nt Service程序
# Service dependencies. Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=
# Mode in which the service is installed. AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=true
6 编译EchoThread.java及MainSvcThread.java,并将生成的class打包为EchoService.jar
javac -d . *.java
jar cvf lib/EchoService.jar EchoThread.class MainSvcThread.class
7 在Bin目录下执行App.bat可在控制台运行程序,InstallApp-NT.bat安装service,UninstallApp-NT.bat卸载service
在任意一台联网的机器上Telnet service_ip 2022 即可测试service程序,
service的log写在logs目录下