LendKey

Wednesday, April 21, 2010

Run Terracotta Server as Windows Service

Terracotta is a JVM-level Java clustering solution. Lots of Java frameworks and libraries, such as Ehcache, Quartz, Web Session, have been offering clustered version over this nice platform.

This document describes steps to install Terracotta Server as a Windows Service that can be managed and executed just as any other standard Windows services, including auto startup when Windows is started and management through standard service management UI and command.

Setup Procedure


Preparation

1. Download and install JDK
    Goto http://java.sun.com to download JDK 1.6.
    As of now, JDK 1.6.0_u17 is certified and recommended.
    Install JDK to c:\java\jdk_1.6.0_17

2. Download and install Terracotta
    Open source version can be downloaded from http://www.terracotta.org.
    Enterprise commercial version can be downloaded by contacting sales@terracottatech.com.
    If download is in form of tar.gz file, untar it to c:\terracotta, such as c:\terracotta\terracotta-3.2.1_1.
    If download is in jar form, double click the file to start installation. Follow installation steps to install Terracotta to to c:\terracotta, such as c:\terracotta\terracotta-3.2.1_1.

3. If Terracotta License file is needed, copy it to Terracotta Installation Folder, such as \terracotta\terracotta3.2.1_1.

4. Download Windows Resource Kit Tools
    Go to http://www.microsoft.com to search and download Windows Resource Kit Tools that is suitable to the targeted Windows version.
    Run the installer and install it to the default folder.
    Copy anysrv.exe to c:\terracotta\terracotta-3.2.1_1\bin

5. Make sure sc.exe is included in your Windows installation. Otherwise, download it from Microsoft’s website

Configuration

1. Create tc-config.xml to be used by Terracotta Server, place it to bin folder under Terracotta installation home folder.

2. Create Registration file
    Create a text file with any text editor, copy paste the following text into the file and save it as tcservice.reg.

REGEDIT4
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Terracotta\Parameters]
"Application"="\"C:\\Java\\jdk1.6.0_17\\bin\\java.exe\\"
"AppParameters"="-cp c:\\terracotta\\terracotta-3.2.1_1\\lib\\tc.jar -server -Xms4G –Xmx4G -XX:+HeapDumpOnOutOfMemoryError -Dtc.install-root=c:\\terracotta\\terracotta-3.2.1_1 com.tc.server.TCServerMain"
"AppDirectory"="c:\\terracotta\\terracotta-3.2.1_1\\"


Note:
If you need to pass more parameters to TC server, yon can do so by appending them to the AppParameters section. For example, if I want to specify a tc-config.xml and a server name, I can change the AppParameters to:
"AppParameters"="-cp c:\\terracotta\\terracotta-3.2.1_1\\lib\\tc.jar -server -Xms4G –Xmx4G -XX:+HeapDumpOnOutOfMemoryError -Dtc.install-root=c:\\terracotta\\terracotta-3.2.1_1 com.tc.server.TCServerMain -f c:\\terracotta\\my-tc-config.xml -n Server1"


Install Service

1. Create Windows Service
    Open a command prompt window as System Administrator.
    Run the following command:
sc.exe create Terracotta binPath= “c:\terracotta\terracotta-3.2.1_1\bin\srvany.exe” type= own start= auto DisplayName= “Terracotta Server 3.2.1_1”
    It will create a new Windows Service named Terracotta which will be started automatically when Windows is started.

2. Modify service parameter, so it will start Terracotta Server
    In the same command prompt box, run:
regedit tcserver.reg
    It will modify the newly created service to run Terracotta Server as a Java application.

Test

1. In Windows Service Management window, start the service. Make sure there’s no error popped up

2. Run dev-console.bat from Terracotta Installation folder and connect to server_name 9520. Developer’s console should be able to login and display the standard Terracotta management interface.

Trouble Shooting

1. Pay attention to quotation signs and garbage characters when copy&paste code snippet from this article


2. To test run the service manually
Since Windows doesn't provide any console based information when starting service, it could be very difficult to trouble shoot problems.
If your service does not start as expected, it is highly recommended to run the service manually just to make sure everything is configured correctly.
To do so, you can follow the following steps:

  1. Open a Commend Shell
  2. Go to folder defined by AppDirectory
  3. Run the Application with AppParameters
In my example, it will be:

CD c:\terracotta\terracotta-3.2.1_1
C:\Java\jdk1.6.0_17\bin\java.exe -cp c:\terracotta\terracotta-3.2.1_1\lib\tc.jar -server -Xms4G –Xmx4G -XX:+HeapDumpOnOutOfMemoryError -Dtc.install-root=c:\terracotta\terracotta-3.2.1_1 com.tc.server.TCServerMain

Friday, March 26, 2010

Install ColdFusion 8 on Snow Leopard

I need to test something in ColdFusion 8 on my MacBook Pro with Snow Leopard. After I downloaded and unzipped the installer, the installer simply did not run. I double click on the ColdFusion 8 Installer.app from Finder, nothing happened. I guess the Install Anywhere script which was used to create installer for CF 8 might have issue with Snow Leopard.

From internet, there're lots of articles about how run CF8 in Snow Leopard, no one really talked about installation part. Hope I'm not the only one that had this problem.

Anyway, I do managed to find a way to do the installation. Here's what I did:
1. Open a Terminal
2. cd ColdFusion\ 8\ Installer.app
3. cd Contents/Resources/Java
4. java -cp ./IAClasses.zip:Execute.zip:installscript.zip:. com.zerog.ia.installer.Main
5. Follow the steps and I was able to finish setup and run CF8

Cheers!

Li

Monday, November 16, 2009

PHP cannot connect to mysql when run inside Apache

I ran into a very strange issue when Apache/PHP/MySQL.
What happend was my php code suddenly cannot connect to mysql on another machine any more. The error message is:
mysqli_connect() [function.mysqli-connect]: (HY000/2003): Can't connect to MySQL server on '218.241.156.222' (13)
The strange thing was that if I connect mySQL from command line, even run the same code by php directly from command line, it worked just fine.

After searched around, I finally found the answer. Looks like it has something to do with SELinux settings.

To fix the issue, I only need to run:
setsebool httpd_can_network_connect_db 1

Before I ran the script, the file content of /selinux/booleans/httpd_can_network_connect_db was:
0 0

After I ran the script, content changed to:
1 1

Saturday, November 7, 2009

Trick to let iTune handle Chinese song names and tags correctly in Mac OS X

When play Chinese songs in iTune, it always tends to show the song information, such as name, artist, album, etc. as garbage text.

When I was running Mac OSX Tiger and Leopard, the trick I used was to open iTune application information by right-click and choose Get Info.
In the information windows, remove all languages except Chinese. So next time, when iTune is started, it starts in Chinese mode. And it handles all Chinese songs correctly.

In Snow Leopard, the language selection is missing in Get Info dialog. It took me a few weeks to find the new trick.
cd /Applications/iTunes.app/Contents/Resources
mkdir lang_bak
mv *.lproj lang_bak
cp -r lang_bak/zh_CN.lproj .

Then start iTune, you will get it in Chinese mode again and just as the trick before, all Chinese songs are handled correctly.

Hope these tricks can be helpful to Chinese Mac users.

Enjoy!

BTW: If you speak other language and suffers the sames kind of problem, I guess you can try to delete all other languages other than your preference. Hopefully that will work for you too.

Sunday, May 17, 2009

Monitor detailed SQL transactions issued by Hibernate

Hibernate help us translate access to Java objects from HQL to native SQL and automatically take care of object relationships, which saves developers lots of time, but at the same time, hide lots of details, such as how cross table queries are created, how tables are related and, very important, what data parameters are used for each prepared statetment is called.
To show SQL commands generaeted by Hibernate, we can normally use Hibernate config:
true
With this setting, Hibernate dumps all SQL it executed against DB as below:


Hibernate: select device0_.iid as iid1_0_, device0_.iname as iname1_0_, device0_.iip as iip1_0_, device0_.iinfourl as iinfourl1_0_, device0_.istate as istate1_0_, device0_.irecordState as irecordS6_1_0_, device0_.idevlocation as idevloca7_1_0_ from ieai_device device0_ where device0_.iid=?


However, if you want to see what exact value is passed into the SQL statement, and further more, what result was returned from the SQL command, we need to do more.
Basically we need to set log4j settings of the follow two Hibernate classes into DEBUG:

org.hibernate.SQL
org.hibernate.Type


So the log4j.properties file will have the following settings:

log4j.logger.org.hibernate.SQL=DEBUG,console
log4j.logger.org.hibernate.type=DEBUG,console


Then Hibernate will dump debug information as below:

76033 [DEBUG] AbstractBatcher.log(): - select device0_.iid as iid1_0_, device0_.iname as iname1_0_, device0_.iip as iip1_0_, device0_.iinfourl as iinfourl1_0_, device0_.istate as istate1_0_, device0_.irecordState as irecordS6_1_0_, device0_.idevlocation as idevloca7_1_0_ from ieai_device device0_ where device0_.iid=?
Hibernate: select device0_.iid as iid1_0_, device0_.iname as iname1_0_, device0_.iip as iip1_0_, device0_.iinfourl as iinfourl1_0_, device0_.istate as istate1_0_, device0_.irecordState as irecordS6_1_0_, device0_.idevlocation as idevloca7_1_0_ from ieai_device device0_ where device0_.iid=?
76033 [DEBUG] NullableType.nullSafeSet(): - binding '3' to parameter: 1
76043 [DEBUG] NullableType.nullSafeGet(): - returning 'Device3' as column: iname1_0_
76044 [DEBUG] NullableType.nullSafeGet(): - returning '192.168.0.12' as column: iip1_0_
76044 [DEBUG] NullableType.nullSafeGet(): - returning 'http://www.infolexllc.com/site/' as column: iinfourl1_0_
76047 [DEBUG] NullableType.nullSafeGet(): - returning '1' as column: istate1_0_
76047 [DEBUG] NullableType.nullSafeGet(): - returning '2' as column: irecordS6_1_0_
76048 [DEBUG] NullableType.nullSafeGet(): - returning 'test device location' as column: idevloca7_1_0_


If you are a newbie in Hibernate, you will be amazed to see what is going on underneath by Hibernate, either in a positive and encouraging way or a very bad negative way.

Cheers!

Wednesday, May 13, 2009

Customize Eclipse JVM in Mac

On Windows, we can change eclipse.ini under eclipse folder to put extra parameters for JVM.
On Mac, the ini file is located at:
eclipse/Eclipse.app/Contents/MacOS/eclipse.ini

Monday, May 4, 2009

How to bing dyanmic IP to a subdomain

We have a server with dedicated IP address in my company's headquarter. There're a couple of branches at other locations. We need to setup servers in each branch and allow accessing to those servers through dedicated subdomain name. But we do not have static IP for those branches, so it's not possible to bind any IP in our DNS server at headquarter.

If you have the same need as we are, here's what we did to resolve the problem.

Basically we need to setup DNS Update service at branch site to perform remote DNS update to headquarter to update IP of subdomains used by branch site periodically, so headquarter's DNS will always know the correct IP of each branch server.

For example, our main domain is infolexllc.com, and we need to setup a branch server at New York which will be called ny.infolexllc.com, here's what we do at servers at both ends (both CentOS 5.X):

At branch site(where only dynamic IP is available):
1. Install NS Update:
yum install bind-utils

2. Create folder to hold related files and scripts:
mkdir /root/nsupdate
cd /root/nsupdate

3. create key files for NS update
dnssec-keygen -a HMAC-MD5 -b 512 -n USER ny.infolexllc.com
Kny.infolexllc.com.+157+47223

4. You should have two new files created under the current folder:
Kny.infolexllc.com.+157+47223.key  Kny.infolexllc.com.+157+47223.private

5. create a new shell file: donsupdate.sh:

Make sure you change value of the script based-on your own environment.
6. Open the newly generated key file Kny.infolexllc.com.+157+47223.key
ny.infolexllc.com. IN KEY 0 3 157 QLwMCWdqUJ/ZOsOsdF4Dj/mYD2XwmqSrPL540JE3dwG7FXZwrJulOp16 Y0SySnfOo7+5s1mhZhUiAHxVOSoXnQ==

Remember the key string started from QlwMC.. all the way to the ending ==

On headquarter server:

7. Modify named comnfiguration to allow remote DNS update from ny.infolexllc.com

Open /etc/named.conf
Find zone "infolexllc.com" {
Insert the following section in the configuration file before the zone:
key ny.infolexllc.com.{
algorithm hmac-md5;
secret "QLwMCWdqUJ/ZOsOsdF4Dj/mYD2XwmqSrPL540JE3dwG7FXZwrJulOp16 Y0SySnfOo7+5s1mhZhUiAHxVOSoXnQ==";
};

Insert the following lines at end of the infolexllc.com zone:
allow-update {
key ny.infolexllc.com. ;
};

Restart DNS server:
/etc/init.d/named restart


Now the headquarter DNS server should allow remote DNS update from ny.infolexllc.com

On branch server:
8. Test DNS update script:
run
donsupdate.sh

You should see messages like below:

Detected IP address is:123.123.123.123
IP has been changed or one hour has been passed, save the flag and do update...
Creating key...
before getaddrinfo()
Outgoing update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 0
;; flags: ; ZONE: 0, PREREQ: 0, UPDATE: 0, ADDITIONAL: 0
;; UPDATE SECTION:
ny.infolexllc.com. 0 ANY A
ny.infolexllc.com. 84600 IN A 123.123.123.123


Reply from update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 25321
;; flags: qr ; ZONE: 0, PREREQ: 0, UPDATE: 0, ADDITIONAL: 1
;; TSIG PSEUDOSECTION:
ny.infolexllc.com. 0 ANY TSIG hmac-md5.sig-alg.reg.int. 123123123123 300 16 ABCabcABCabc123+12aBcde== 25321 NOERROR 0


The status: NOERROR indicates a succeful update.
If you see any other error in status, go check /var/log/messages on head quarter server to find clue.

9. Test the new domain:
ping ny.infolexllc.com

You should see the new domain can be resolved to the dynamic IP currently assigned to branch server.

10. Once update script is working, you can create a cron job on branch server to run the script every 10 minutes. The script itself will check if IP has not been changed in the past hour, it will skip remote update.

I've done this for our branch servers several times. It works very well so far. I hope this set up can help resolving your problem. Feel free to let me know f you have any feedback or question.

Cheers!