uniapp开发app框架在提升开发效率中的独特优势与应用探索
786
2022-09-23
jvsc文档
最近需要部署一个基于tomcat的应用时,因为应用和tomcat都是在测试环境配置好的,直接拷贝到生产环境后,发现无法启动,报一个和 jsvc相关的错误。当时没有来得及保留下错误日志,但是重新编译jsvc后,系统正常了。系统部署完成后,在apache看到jvsc的文档。
Introduction
Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily. Jsvc allows the application (e.g. Tomcat) to perform some privileged operations as root (e.g. bind to a port < 1024), and then switch identity to a non-privileged user. It can run on Win32 via the Cygwin emulation layer (see Cygwin for more information), however Win32 users may prefer to use procrun instead, which allows the application to run as a Windows Service.
The sources are located in the src/native/unix subdirectory.
In the future APR may be used to provide more portable platform support. Building from source
To build under a UNIX operating system you will need:
* GNU AutoConf (at least version 2.53) * An ANSI-C compliant compiler (GCC is good) * GNU Make * A Java Platform 2 compliant SDK
You need to build the "configure" program with:
sh support/buildconf.sh
(Note it is possible to replace sh by any compatible shell like bash, ksh). The result should be something like:
support/buildconf.sh support/buildconf.sh: configure script generated successfully
Once the configure script is generated, follow the next section. Building from a release tarball
To build the binary under a UNIX operating system you will need:
* An ANSI-C compliant compiler (GCC is good) * GNU Make * A Java Platform 2 compliant SDK
You have to specify the JAVA_HOME of the SDK either with the --with-java=
./configure --with-java=/usr/java
or
export JAVA_HOME ./configure
If your operating system is supported, configure will go through cleanly, otherwise it will report an error (please send us the details of your OS/JDK, or a patch against the sources). To build the binaries and libraries simply do:
make
This will generate the executable file jsvc. Starting jsvc
To check the allowed parameters for the jsvc binary simply do:
./jsvc -help Usage: jsvc [-options] class [args...]
Where options include:
-help | --help | -?
show this help page (implies -nodetach)
-jvm
Using jsvc
There two ways to use jsvc: via a Class that implements the Daemon interface or via calling a Class that has the required methods. For example Tomcat-4.1.x uses the Daemon interface whereas Tomcat-5.0.x provides a Class whose methods are called by jsvc directly. Via Daemon interface
Do the following:
* Write a Class that implements the Daemon interface (MyClass). * Put it in a jarfile (my.jar). * Call jsvc like:
./jsvc -cp commons-daemon.jar:my.jar MyClass
Directly
Write a Class (MyClass) that implements the following methods:
* void init(String[] arguments): Here open configuration files, create a trace file, create ServerSockets, Threads * void start(): Start the Thread, accept incoming connections * void stop(): Inform the Thread to terminate the run(), close the ServerSockets * void destroy(): Destroy any object created in init()
Store it in a jarfile and use as above:
./jsvc -cp my.jar MyClass
How jsvc works
Jsvc uses 3 processes: a launcher process, a controller process and a controlled process. The controlled process is also the main java thread, if the JVM crashes the controller will restart it in the next minute. Jsvc is a daemon process so it should be started as root and the -user parameter allows to downgrade to an unprivilegded user. When the -wait parameter is used, the launcher process waits until the controller says "I am ready", otherwise it returns after creating the controller process. Forks in commons-daemon
Launcher process:
main() { fork() parent: wait_child(), wait until JAVA service started when the child says "I am ready". child: controller process. }
Controller process:
while (fork()) { parent: wait_for_child. if exited and restart needed continue else exit. child: exit(child()). controlled process. }
Controlled process:
In child(): controlled process. init_JVM(). load_service(). start_service(). say "I am ready" wait for signal or poll for stop stop_service(). destroy_service(). destroy_JVM(). exit (with different codes so that parent knows if it has to restart us).
Note: The controller process uses signals to stop the controlled process. Downgrading user
On Linux setuid()/setgid() + capabilities are used. On other unix setgid/initgroups are used. We have something like:
/* as root */ init_JVM(). load_service. /* java_load() calls the load method */ downgrade user (set_caps() or set_user_group()) /* as the user $USER (from -user $USER parameter) */ umask() start_service. /* java_start() calls the start method */
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~