Ninja features a standalone mode where you do not need a separate servlet container. The standalone mode enables you to package your application along with a fast, embedded HTTP server (Jetty in that case). There are several options for packaging your application using standalone mode.
This allows you to drop that self-executing file to your server run your application with zero external dependencies. Due to Ninja’s client-side sessions it becomes really simple to have one reverse proxy in front of many Ninja instances.
In order to do so you have to first of all add ninja-standalone as dependency to your pom.xml:
<dependency>
<groupId>org.ninjaframework</groupId>
<artifactId>ninja-standalone</artifactId>
<version>X.X.X</version>
</dependency>
Don’t forget to change packaging value from war to jar.
<packaging>jar</packaging>
Packaging the application is done via the maven shade plugin. Add the following snipplet to your pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ninja.standalone.NinjaJetty</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Whenever you build your application on the command line this will generate a fat jar inside your target directory. Usually that file is named roughly MY-APPLICATION-jar-with-dependencies.jar
You can generate the fat jar by calling
mvn clean compile package
Running the fat jar (and your app) is as simple as calling:
java -Dninja.port=9000 -jar MY-APPLICATION-jar-with-dependencies.jar
If you’d like to run your application as a proper daemon on Unix/Mac or as a service on Windows, another option is using the Stork Maven Plugin.
Stork is a collection of utilities for optimizing your “after-build” workflow by filling in the gap between Maven and eventual app execution. In just a couple quick steps, stork will package your Ninja app into a professional .tar.gz file ready for deployment.
Here is an article describing how Ninja apps can be packaged with Stork.
Here is a demo application that integrates with Stork.
Regardless of which option you choose, you can customize your application startup via the following command line parameters:
If you use option #1 and want to register your Ninja standalone application on your Linux box (Debian, Ubuntu) you can use and adapt the following script. The script should be copied at /etc/init.d/ninja and can be run via service ninja start.
#!/bin/bash
# chkconfig: 345 20 80
# description: Ninja start/shutdown script
# processname: java
#
# Installation:
# copy file to /etc/init.d
# chmod +x /etc/init.d/ninja
# chkconfig --add /etc/init.d/ninja
# chkconfig ninja on
# OR on a Debian system
# sudo update-rc.d ninja defaults
#
# Usage: (as root)
# service ninja start
# service ninja stop
# service ninja status
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Path to the application
APPLICATION_NAME=my-ninja-service
APPLICATION_PATH=/srv/ninja_applications/${APPLICATION_NAME}
APPLICATION_JAR=my-ninja-service-1.0-SNAPSHOT-jar-with-dependencies.jar
PID_FILE=/var/run/${APPLICATION_NAME}.pid
PORT=9013
# Path to the JVM
JAVA_BIN=/opt/java-oracle/jdk1.7.0/bin/java
PARAMS="-Dninja.port=${PORT} -jar ${APPLICATION_PATH}/${APPLICATION_JAR}"
# User running the Ninja process
USER=ninja
RETVAL=0
start() {
if [ -f ${PID_FILE} ]; then
echo "Ninja application ${APPLICATION_NAME} already running"
else
DAEMON_START_LINE="start-stop-daemon --chdir=${APPLICATION_PATH} --make-pidfile --pidfile ${PID_FILE} --chuid ${USER} --exec ${JAVA_BIN} --background --start -- ${PARAMS}"
${DAEMON_START_LINE}
RETVAL=$?
echo -n "Starting Ninja Application: ${APPLICATION_NAME}... "
if [ $RETVAL -eq 0 ]; then
echo " - Success"
else
echo " - Failure"
fi
echo
fi
echo
}
stop() {
kill -9 `cat ${PID_FILE}`
RETVAL=$?
rm -rf ${PID_FILE}
echo -n "Stopping Ninja application: ${APPLICATION_NAME}"
if [ $RETVAL -eq 0 ]; then
echo " - Success"
else
echo " - Failure"
fi
echo
}
status() {
if [ -f ${PID_FILE} ]; then
echo "Ninja application ${APPLICATION_NAME} running"
else
echo "Ninja application ${APPLICATION_NAME} not running"
fi
echo
}
clean() {
rm -f ${PID_FILE}
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
sleep 10
start
;;
status)
status
;;
clean)
clean
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac