Friday, November 26, 2010

Java Chat

This is another find in my workspaces!

Featuring this time a simple text based chat, based on a self-developed protocol allowing server client communication.

  • Simple text chat
  • Multithreaded Server
  • Singlethreaded Client
  • fully swing compatible
  • Status messages in chat window (disconnects, errors etc.)
  • Overview list with all connected clients
  • Possibility to private/ group chat
How it works

The first thing to do is to start the server. This is as easy as it sounds just clicking on the "Start" button and we can leave the server alone, handling itself all connections etc.

Next step is to initiate a Client connection to the server. This can be done by starting the Client Chat application. Here we just need to specify the server's IP Address and an appropriate nick name.

Now we can see that the main window is enabled and we can now start chatting by typing some words into the textfiled below this chat window. On the right side the chatter list appears showing only one entry, us. Let's now invitate more people to show all features!

Next persons will be Alice and Bobby:

Here we can see that "Bobby" successfully connected to the server and received a message from "Chris". Furthermore a private message was sent to him only readable of him from "Alice".

After "Bobby" connected "Alice" also joined the chat and received the message from "Chris". Then "Alice" sent a private message to "Bobby". This could be done by unchecking "Chris" from the chat overview list on the right side.

This picture shows the final result when the user "chris" disconnected after chatting with Alice and Bobby.

If you want to know more how the connection is handled then you should take a closer look at the ServerClientHandlerThread and the ClientConnectThread. The main rule plays the MessageObject which is used to determine the connected Clients and their behavior.


Maven Project - with Source Code

Runnable Jars
Server: ChatServer.jar
Client: ChatClient.jar

J2ME Bluetooth Game Tic Tac Toe/ 3Gewinnt

Another ancient find in my workspaces!

Although today JavaFX should be the chosen platform when developing applications for limited resources devices, I once created a traditional J2ME game when JavaFX wasn't released.

The game is very simple and is equivalent to the good old known Tic Tac Toe principle. As far as I can remember I wanted to know whether it is possible to create a wireless connection between mobile devices running java.


  • Single Player
  • Multiplayer via Bluetooth
  • Prepared: AI Mode
  • Fully J2ME implemented using standard GUI

The result can be seen when deploying the JAR/ JAD to your mobile handset.

I don't have pictures showing the game right now, because it is years ago since development and usage. And maybe I'm going to re-package it using Maven for simpler integration.

Successfull usage on:

Sony Ericsson K750i
Sony Ercisson T650i

and some other Nokia i can't remember anymore ;-)


Project Folder - was built using WTK 2.5.2

Sunday, November 14, 2010

Create Cron Job to Monitor Jetty 7

If you are new to Linux, or just forgot a lot of knowledge over years like me you might want to know how to create a Cron Job.

A Cron Job is a Task in Linux which is to be executed automatically at specific times. This can be for instance a time synchronisation script which needs to update your system time every 24 hours.

But, before we dive into cron jobs we need to define a task which should be executed.


If you are working on a virtual server you are granted limited resources of the host system. That means you only have a certain amount of RAM. This leads to memory allocation problems when deploying large applications, e.g. in a webapplication server like Jetty.

Imaging now an OOM (OutOfMemory Exception) occurs at 3 o'clock in the morning, you as the system administrator will probanly be sleeping and no one can access your website due to the server's downtime. But even you were awake and notices it, you have to manually restart the server by hand. Annoying right?


In short: Check the process id of your java process connected to Jetty and if it's down restart the server.

Here is the source code of the bash script

#! /bin/sh

jettyPid=`pgrep -f "java -jar start.jar"`
mydate=$(date +"%Y-%m-%d %k:%M:%S")

if [ "$jettyPid" == "" ]

echo "$mydate WARNING: Jetty is shutdown! Trying to restart it now!"
#exec start of jetty server
cd $jettyHome
java -jar start.jar


In detail:

With pgrep we can access all process ids running on the system. I.e. we want to make sure that we only get the process id of the jetty server returned by passing the command name as parameter. We have to do this because there may be multiple 'java' processes running on the machine and we have to distinguish them.

The next is really straightforward, checking if we found a process id, if not setting our jetty home, cding to it and starting jetty.

Note: we have to switch the current working dir to jetty home, otherwise it wont find the .xml configuration files from /etc/* located in jetty home. But there is also the possibility to pass these parameters to the start.jar directly by calling 'java -jar start.jar OPTIONS=Server,jsp /etc/jetty.xml ... ', so you could start jetty from any working directory.

You can save the shell script and add execute permission with chmod 700

Activating the Cron Job

Now that we got our script it is time to move on to the cron jobs part and let it automatically execute it. To do this we have to create a cron job:

With crontab -e we tell Linux to open an editor to edit our cron jobs according to the logged in user. Now that this may be our first cron job, a little explanation on the format:

# m h dom mon dow command
  • m = minute
  • h =hour
  • dom=day of month
  • mon=month
  • dow=day of week
  • command = the command which shoud be executed
The least time period is one minute and that is what we want. So adding this entry will ensure that our script is executed every minute, every day, every year etc:

0-59 * * * * /opt/java/server/ >> /home/user/log/jettycheck.log #logs jetty errors and automatic restarts it

We also want to log the output from the script and therefore we have to create a .log file, e.g. in our users home log directory (log has to be created as well if it does not exist). If the nano editor was started to edit the crontab then we save with ctrl+o and exit with ctrl+x.

If we want to clear the log file, e.g. every 24 hours then we have to add following to the crontab:

0 0 * * * > /home/user/log/jettycheck.log #refreshes this log file every 24h

That's all you have to do to enable automatic monitoring of your jetty server! And a maximum of one minute downtime may be suitable for most personal websites.