Feeds:
Posts
Comments

Archive for September, 2008

Have you ever faced strange or unexpected problems with your application after putting it into production? Is the same code works just fine when you test it? Is the problem just too hard to even simulate?Well if the problem does not appear when you test it but its there in production and assuming you have a staging environment i.e. your test environment is the same as your production environment. Then the first question is, is your application thread-safe? When you test it, it is only one user but in production is it more than one user?

The only way to ensure that your application is thread-safe is by running more than one thread at the same time. This is a very simple test but it can clear out very serious issues. Here is how you can test it:

Public class theardTest
{
	Public static void main(String[] args)
	{
		Thread userOne = new Thread()
		{
                      Public void run()
                      {
	                 //call your application here
                      }
                  };

                  Thread userTwo = new Thread()
                  {
                        Public void run()
                        {
                               //call your application here
                        }
                  };

                  userOne.start();
                  userTwo.start();
         }
}

If your application is failing here and there, then it is not a thread-safe application and it can not be accessed by more than one user concurrently. There could be many reasons for that like improper user of static modifier, collection iteration, etc.

If your application should be accessed concurrently, then it should be thread-safe.

Read Full Post »

Barcode Encoding in JAVA

Generating barcodes, be it 1D or 2D, have never been easier. I will be using the Barbecue open source project hosted on sourceforge to generate a 1D barcode. After the barcode is generated I will decode the barcode by uploading it to a website whose service we shall use. However its also very simple to write a barcode decoder which shall be a topic of discussion later.

How to ….

First of all you need to download the Barbecue . Following are the jars added in my classpath

  • barbecue-1.5-beta1.jar
  • jdom.jar

Following is the code to generate a sample barcode

package com.datel.barcode;

import java.io.File;

import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;

public class GenerateBarcode {

	public static void main(String[] args) throws Exception {

		Barcode barcode = BarcodeFactory.createCode128("HelloWorld:0505124456");
        File f = new File("c:\\barcode.png");
        BarcodeImageHandler.savePNG(barcode, f);

	}

}

Now you should find a file named barcode.png in you file system in C: drive. It should look like this

Barcode

Barcode

We shall try to decode this image the easy way by uploading it to a website. Navigate to this website: ZXing This is a website hosted by the ZXing project team who have a java api for decoding barcode images. Upload your image and click Submit Query. You should see this as the output

HelloWorld:0505124456

A few words….

Even the Barbecue encoder is only beta and has been beta since May 2007. Also I really don’t comprehend why in the world these cool guys would make the Barcode class extend from javax.swing.JComponent. This really drives me mad to see such a thing but the BarcodeImageHandler class comes to the rescue. Anyways keep up the good work.

Humble Insight…

After my extensive search on the web I could not find a single open source java barcode encoder worth mentioning. I would be very much happy if any one could mention a good open source barcode encoder. But there were quite a few decoders though. Sadly to mention, if you want to encode data into barcodes on production applications you will have to rely on some commercial products out there. They are very good at what they do and are can very easily be integrated into your application. Many have facilities for resizing/rotating your image and can be very handy at times.

Using Barbecue looks all nice, simple and dandy but when you start getting requirements from your customers saying “Hey I want to put 100 MB of data in a 2D barcode” then things get hard. Although that’s a bit exaggerated, I know cases where my colleagues were asked for all weird kinds of things to be done with barcodes. To them I have only one thing to say… “Technology was made to help mankind and was not made to be abused by mankind”.

There is tremendous potential in this barcode industry and advanced encoding techniques keep coming out.

Read Full Post »