Wednesday, November 28, 2012

Setting Tomcat for development and production

In this post I will describe how to setup great Tomcat installation.


Installation

Download Tomcat for your environment from official download site. Don't download Windows Service Installer even if you plan to use Tomcat as service.

Unzip Tomcat to desired directory.

Changing logging engine to Logback

After installing Tomcat I will change logging to use Logback following this instructions.
  • copy everything from tomcat-slf4j-logback/bin to destination/bin (this will overwrite tomcat-juli.jar)
  • copy logging configuration in tomcat-slf4j-logback/conf to destination/conf
  • delete logging.properties from destination/conf
  • copy logback-core.jar and logback-access.jar to destination/lib
Change Valve in server.xml with:
 <Valve className="ch.qos.logback.access.tomcat.LogbackValve" quiet="true" filename="${catalina.home}/conf/logback-access-localhost.xml" />  

After that you should run tomcat with startup.bat to check that everything works.

Setting Tomcat users

In directory conf/ you should edit tomcat-users.xml. Add this inside tomcat-users tags:
 <role rolename="manager-gui"/>  
 <role rolename="admin-gui"/>  
 <user username="tomcat-mng" password="*****" roles="manager-gui,admin-gui"/>  

Adding Tomcat to Eclipse

Add new Tomcat server as usually. First add Server runtime enviroment (Windows->Preferences), and then add new Server using that runtime.
You should adjust Publishing and Timeouts as preferred. I use "Never publish automatically" , start 450s and stop 150s. 
After that you should set "Launch Configuration", in VM arguments you should add:
 -Djuli-logback.configurationFile="<absolute tomcat home path>\conf\logback.xml"  
In Server project on your workspace you should check that server.xml has right Valve set.

Now you can start server to check that everything is OK.

Running Tomcat as Windows Service

You should install Tomcat as a service using service.bat. Advanced setup is described here.

 service.bat install  

You should run this command as an admin. This will add Tomcat to your services, but will not enable monitor service and edit configuration service. So I use this command:

 tomcat7w.exe //MS//   

You install service with specific name:

 service.bat install [service_name]  

Then you need to rename tomcat7.exe and tomcat7w.exe to [service_name].exe and [service_name]w.exe respectively. You should run this programs as administrator.

Now you can change setup so Tomcat will be started automatically. Also you should add
 -Djuli-logback.configurationFile="<absolute tomcat home path>\conf\logback.xml"  
to Java Options.

Redirecting Stdout and Strerr

When you run Tomcat in production you should redirect stdout and stderr to null.

Tuesday, November 27, 2012

Connecting Tomcat with PostgreSQL on OpenShift




Now that we have created Tomcat on OpenShift we can use one of the Gears to install Database server. One of the advantages of OpenShift is that it allows user to create full flagged RDBS for free. I will explain here how to install PostgreSQL but you can choose MySQL or MongoDB if you need NoSQL database.

Creating a gear

You should log in your account on OpenShift and select My Applications tab and click on a name of your application. This will take you to configuration page.


Now you should choose Add Cartridge and select PostgreSQL database. On the next screen you should just click Add Cartridge. After initialization is done you will be prompted connection parameters for your new database and instructions how can you remove this cartridge. You should take note of information on this screen because once you close it you will not be able to get that information.

If you followed my instructions PostgreSQL will be installed on a same Gear as Tomcat so you will still have 2 free Gears.

Connecting Tomcat and PostgreSQL

If you want to connect to your new database from Eclipse or pgAdmin you need to start port forwarding and that can be only done from RHC tool and is only enabled while you run port forwarding command (rhc port-forward -a tomcat).
If you install MySQL database you can use  phpMyAdmin interface for interaction with database, but I prefer PostgreSQL.

Next we need to copy PostgreSQL JDBC driver to Tomcat /lib directory, I used WinSCP for this, or you can do it trough git if you followed my instructions from last post.

You need to add resource setting to global section of server.xml
 <Resource   name="jdbc/tomcat"   
       auth="Container" type="javax.sql.DataSource"   
       driverClassName="org.postgresql.Driver"   
       url="jdbc:postgresql://OPENSHIFT_INTERNAL_IP:5432/tomcat"   
       username="admin"   
       password="****"   
       maxActive="20"   
       maxIdle="10"   
       maxWait="-1"/>  
And also add in context.xml:

 <ResourceLink name="jdbc/tomcat" global="jdbc/tomcat"  
     type="javax.sql.DataSource" />  

After that you should restart server.

You should note that I put OPENSHIFT_INTERNAL_IP in DB url and this constant will be changed in start script that is in action_hooks directory. If you are not using GIT for managing Tomcat configuration you should set this to real IP address.

You can check internal IP with command: echo $OPENSHIFT_INTERNAL_IP
(You shoud be logged in with putty), or you can note what IP is displayed when you port forward.

Getting data source in Java is out of the scope of this post, but you can check out my demo program on GitHub at https://github.com/McNullty/db-tester.

There are examples of connecting to database with Apache BasicDataSource and using JNDI.




Monday, November 26, 2012

Creating Tomcat application on OpenShift - Take two

So..., my first post on creating Tomcat installation on OpenShift had some "bugs". In this post I will describe better way of initializing Tomcat.

First change is that I installed client tools as instructed in this link. While installing GIT I choose using ssh2 and not Putty plink.

Second change is that I used template from GitHub  (https://github.com/lulinqing/openshift-tomcat-quickstart)

So this is the steps that I used:
  • Creating DIY application: rhc app create -a tomcat -t diy-0.1
You should be positioned in directory where you want local git repo to be.
  • Go into created directory: cd tomcat
  • Add another remote git repo: git remote add upstream -m master git://github.com/lulinqing/openshift-tomcat-quickstart.git
  • Pull changes from repo: git pull -s recursive -X theirs upstream master
After I pulled configuration I have changed file /.openshift/action_hooks/deploy. This file is script that executes every time that you push to OpenShift repo. Default script will only copy files if tomcat directory doesn't exist, I have changed script to copy complete tomcat on every git push.

/.openshift/action_hooks/deploy:
 #!/bin/bash   
 # This deploy hook gets executed after dependencies are resolved and the   
 # build hook has been run but before the application has been started back   
 # up again. This script gets executed directly, so it could be python, php,   
 # ruby, etc.   
 set -x   
 if [ -d $OPENSHIFT_DATA_DIR/tomcat ] then   
    rm -rf $OPENSHIFT_DATA_DIR/tomcat/   
 fi   
 cp -rf $OPENSHIFT_REPO_DIR/diy/tomcat $OPENSHIFT_DATA_DIR   
 cd $OPENSHIFT_DATA_DIR/tomcat   
 rm -rf logs   
 ln -s $OPENSHIFT_DIY_LOG_DIR logs   
 sed -ig 's/OPENSHIFT_APP_DNS/'$OPENSHIFT_APP_DNS'/' conf/server.xml   

I have also changed password in file tomcat-users.xml.

That's it, now you can commit changes and push them to Openshift: git push
And wait few moments to propagate changes and you can log in to your new tomcat installation.

I have created GitHub fork with my changes at https://github.com/McNullty/openshift-tomcat-quickstart so you can use this repo as remote stream if you don't want to manually change anything.

Next post will be on connecting to database, I promise :-).

Monday, November 5, 2012

Creating Tomcat application on OpenShift

OpenShift

OpenShift is JBoss Cloud solution. I like OpenShift better than Google App Engine because it provides real RDBS if you select MySQL or PostgreSQL, so you can develop simple applications that could be later transferred to some other server.
OpenShift doesn't provide Tomcat out of the box like it does for JBoss AS, PHP or Ruby, but you can install it trough DIY cartridge.

Connecting to OpenShift

Opening account on OpenShift is trivial, I used TOMCAT promo code but you can find a lot of other codes (I don't know what promo code exactly do but - whatever)
 
I have followed tutorial for installing Tomcat on OpenShift with few differences that I will describe here.
First of all, I didn't install RHC client tool because I created application using web interface and all other actions were done trough Eclipse (with git), SSH (Putty) and WinSCP.
Instructions for setting connection with Putty is described here, and WinSCP setup is almost identical.

After I confirmed my account, I opened Create Application tab and selected Do-It-Yourself web cartridge. Configuring application  name and namespace is trivial and you shouldn't have trouble there.  

Shutting Down default Ruby application

We can check what application is running with command ps x

> ps x

PID TTY STAT TIME COMMAND
4682 ? S 0:00 sshd: 6e35702606914790aaddacd6e3eeb7ca@pts/0
4683 ? Ss 0:00 /bin/bash --init-file /usr/bin/rhcsh -i
10884 ? S 0:00 sshd: 6e35702606914790aaddacd6e3eeb7ca@notty
10885 ? Ss 0:00 /usr/libexec/openssh/sftp-server
24328 ? S 0:00 ruby /var/lib/stickshift/6e35702606914790aaddacd6e3eeb7ca/app-root/runtime/repo//diy/testrubyserver.rb 127.8.212.129 /var/lib/stickshift/6e357026069147
24341 ? R+ 0:00 ps x


We can then stop running server by going into directory app-root/repo/.openshift/action_hooks and running script stop or just by killing ruby process.
./stop

Installing and configuring Tomcat

After we have stopped server this way we can continue following tutorial for installing Tomcat.

I made all changes directly trough Putty, but you should be able to do everything with Eclipse and git plugin

My start script:
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
set -x
cd ~/tomcat/data/apache-tomcat-7.0.32/
sed -ig 's/OPENSHIFT_INTERNAL_IP/'$OPENSHIFT_INTERNAL_IP'/g' conf/server.xml
bin/startup.sh


My stop script:
#!/bin/bash
# The logic to stop your application should be put in this script.
~/tomcat/data/apache-tomcat-7.0.32/bin/shutdown.sh


You can now start and stop tomcat server by going into ~/tomcat directory and running:
[tomcat-javafiend.rhcloud.com tomcat]\> ./tomcat_ctl.sh start
or
[tomcat-javafiend.rhcloud.com tomcat]\> ./tomcat_ctl.sh stop

That's it! You can start your Java fun with Tomcat!

Friday, November 2, 2012

Hello World!

Hello World!

This blog is meant to be reference tool for me personally, but if it helps anybody else, even better.

I’m going to be writing about my java experiments and personal projects. First real post will be about opening OpenShift account and setting Tomcat envirioment.