How to run a JAX-RS application on an embedded Tomcat? - java

I want to run a JAX-RS 2.0 (Jersey) application on an embedded Tomcat (7). But I have not found an example or documentation about it.
How can I set up Tomcat 7 programatically and add a Servlet wrapping a JAX-RS application to it?

If you want to start/stop Tomcat from Java environment, look at its startup scripts to see which classes are invoked. It's actually not that complex; I do this from my IntelliJ all the time. My IntelliJ startup config looks like this:
Main Class: org.apache.catalina.startup.Bootstrap
VM parameters: -ea -cp $CLASSPATH:/path/to/tomcat/bin/bootstrap.jar -Dcatalina.base="/path/to/tomcat" -Dcatalina.home="/path/to/tomcat" -Djava.io.tmpdir="/path/to/tomcat/temp" -noverify -Xmx400M -XX:MaxPermSize=400M
Program parameters: start
Working directory: /path/to/tomcat
If you want to stop Tomcat gracefully, the only different thing is program parameters, which is stop.

Related

Logs not printing after migrating from log4j-1.2.17 to Log4j2 2.17.1

I am trying to migrate from log4j-1.2.17 to log4j-api-2.17.1 but after all the changes, the logs are not getting printed. I am using 'Option 2' as suggested by the https://logging.apache.org/log4j/2.x/manual/migration.html .
I have added lo4j-api-2.17.1.jar and log4j-core-2.17.1.jar in my libs folder and properly configured log4j2.xml in the src/resources/... folder.
There are no build errors nor any errors or warnings in my console when I run my Weblogic 12c server. It is a jdk 1.6 and struts 2 java web application.
The logs print fine when I use below VM args:
-Dlog4j2.configurationFile=%LOG_PATH% -Dlogpath=%LOGS%
set LOG_PATH=C:\bea\user_projects\domains\app_domain\conf\log4j2.xml
set LOGS=C:\bea\user_projects\domains\app_domain\logs
But this method is not acceptable for Prod env due to business requirements. Is there any alternative to perform the tasks of VM args through java code without using the VM args?
The alternative way is to use System properties. Then you programmatically add properties:
System.setProperty("log4j2.configurationFile", "/path/to/log4j2.xml");
You can add this code to the dispatcher listener. See how the dispatcher listener should be used.
Referencies:
The System Properties tutorial.
The javadoc for System.setProperty(String key, String value).

Jetty and Java Agent Don't Work Using Logging

I'm trying to run Jetty with java agent instrumentation.
I'm using the following command:
java -javaagent:%JETTY_HOME%/lib/ext/aspectjweaver-1.8.9.jar -jar %JETTY_HOME%/start.jar
It works as expected.
But, when adding a logging module to Jetty (java -jar %JETTY_HOME%/start.jar --add-to-start log4j) and then start the Jetty server it runs without instrumentation at all.
Note: I was able to start the agent with instrumentation when I used the full output of '--dry-run' command.
Any idea why the agent wasn't loaded when running the standard jetty startup command with a logging module?
Thanks,
Dror
Run java -jar start.jar --list-config from your jetty base.
It's likely that your configuration is triggering an --exec call which will fork a new JVM.
The -javaagent command would not be passed forward to that new JVM.

Can a java .war file contain a webserver

I'm looking into installing Jenkins, in the instructions it says
"Easy installation: Just java -jar jenkins.war, or deploy it in a
servlet container. No additional install, no database."
I understand the servlet container method, but does the above statement mean that just installing Java and running the .war file will somehow spinup a webserver and start serving http request ?
Yes, the war file contains the built-in Winstone servlet container, and running that command will start it and make it listen for requests on port 8080.
Edit: Jenkins 1.535 and above bundles Jetty (rather than Winstone). You can still run it with java -jar jenkins.war.
Jenkins comes bundled with Winstone, a very lightweight servlet container. As such, Jenkins can be started from the command line as stated by the instructions without any additional software installation.

Where do I locate Java Servlet container

I have installed JRE and now I'm trying to install Solr by following the Solr Reference Guide.
The guide dictates:
Stop your Java servlet container.
Copy the solr.war file from the Solr distribution to the webapps directory of your servlet container...
Start your servlet container, passing to it the location of your Solr Home in one of these ways:
Set the Java system property solr.solr.home to your Solr
Home. (for example, using the example jetty setup: java
-Dsolr.solr.home=/some/dir -jar start.jar). Configure the servlet container so that a JNDI lookup of java:comp/env/solr/home by
the Solr webapp will point to your Solr Home. Start the
servlet container in the directory containing ./solr: the default Solr
Home is solr under the JVM's current working directory
($CWD/solr).
Questions:
Where do I find this Java Servlet container?
Where/how do I execute those commands to start/stop/configure the Java Servlet container?
How do I set Java system properties?
Mine was quite straight forward. After downloading the file, I extracted it to a folder on my Server. I used the Command Line to complete the installation.
With the Command line, navigate to the example folder inside the solr-X.XX.X folder you extracted the file to, once there run the java command:
cd C:/<folder>/app/solr-X.XX.X/example
java -jar start.jar
It would perform the installation for you, like magic.
You can access SOLR by visiting localhost:8983/solr
I'm using a Windows Server though, it may be different on yours, but I guess the logic is the same.
You should to install Apache Tomcat. All information about these servers you can find here.
You also could to install Jetty, JBoss, vSphere, tc vFabric Server ... but I think that you'll setup tomcat and solr easier (it is mine personal opinion, if you are familiar with any one, use them of course).
Tomcat setup is described here.

Using Tomcat 7 on Ubuntu 12.10 --- How To?

I am a newbie to Ubuntu 12.10 and moved to it from Windows.
In Windows I have configured the environment variable to include servlet-api.jar in the CLASSPATH variable so I do not have to type -cp <path to servlet-api.jar> every time I compile.
For normal Java programs, I have set the JAVA_HOME using:
export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386" >> ~/.bashrc
With that solved, I need to set the CLASSPATH but do not know how to.
Please help on that!
Also I read on some forums that I should change the CLASSPATH manually each time I compile because changing the CLASSPATH may upset other applications.
Next, I tried this command to start Tomcat
service tomcat7 start but I got an error :
You need root privileges to run this script I do not know why. Help me solve that!
And if within the scope, please tell me how the directory structure of tomcat in Windows differs from that in Ubuntu.
Misc
I used sudo apt-get install tomcat7 to get Tomcat 7.
You don't say whether you're using an Ubuntu Tomcat package or a standalone Tomcat installation.
For a standalone Tomcat, you will be starting and stopping it using the scripts in its bin directory: startup.sh and shutdown.sh. In that case, Tomcat will use the environment variable values set at the time of invocation.
When doing this, you can set the environment variables prior to running these commands.
The same is true for the Tomcat 6 or Tomcat 7 that Ubuntu distributes as packages.
These neatly separate the $CATALINA_HOME directory (where the Tomcat 7 distribution is supposed to be) from the $CATALINA_BASE directory (where all of the changes and additions for a particular Tomcat instance are supposed to be. For the tomcat7 package, the first directory is /usr/share/tomcat7, while the second is /var/lib/tomcat7.
You can find startup.sh and shutdown.sh in /usr/share/tomcat7/bin and of course you can use them.
However, if you use the Tomcat provided with Ubuntu, you're probably going to want to run it just like any other standard system service, and the packages support this: e.g. in the case of tomcat7, you can just use
sudo service tomcat7 status
sudo service tomcat7 start
sudo service tomcat7 stop
sudo service tomcat7 restart
just like you can for any system service, and it uses the same mechanism as other system services do:
the shell script /etc/init.d/tomcat7 is used to start and stop Tomcat 7
/etc/default/tomcat7 is its configuration file, allowing a few parameters to be set
Using this method, you cannot specify additional environment variables for Tomcat to use by setting them in your shell or in /etc/default/tomcat7; they will not be passed to Tomcat.
However, there is a second method to set environment variables: if you set them in the shell script /var/lib/tomcat7/bin/setenv.sh (or, if you must, /usr/share/tomcat7/bin/setenv.sh), they will be picked up and passed to Tomcat. This method always works.
Finally, Tomcat offers finer control than just using a $CLASSPATH when it comes to specifying additional classes or JARs to be loaded on startup: in its conf/catalina.properties configuration file, you can set the common.loader, server.loader and shared.loader to a list of directories and/or JAR files to be loaded in additional to the standard ones.
All it means is that you should do that as a superuser - which translates to administrator in windows so you should try something like sudo service tomcat7 start
Try export CLASSPATH=/usr/share/tomcat7/lib/servlet-api.jar:/usr/share/tomcat7/lib/jsp-api.jar - This will depend on where your tomcat installation in located.
You can get more here
Cheers

Categories

Resources