Key components of an eCommerce system

Having an online presence is a must for any business in current world. For a company which is selling products, it will mean creating an ecommerce website, from where customers can purchase products. There can be businesses which are selling totally online (Amazon, ebay) and others which already have offline selling mediums (showrooms), having ecommerce site as an additional way to sell products (Bestbuy).

Depending on the business, and role of ecommerce in it, and complexity of the system will vary. For example, one can have listing of products online but actual selling is through phone, or user can order online which is being manually interpreted by backoffice employees, or a completely automated system. There can be different types of ecommerce systems as well, some selling tangible products like mobile phones, electronics, apparels, other might be selling airline tickets, music for download, a consultant service etc.

As noted, we can have multiple types of ecommerce systems, but some of the key components used by most sites are-

Product Information: Product is the sellable unit in ecommerce system (we can sell services as well, but then they will be considered as product for that particular system). We need to have as much information as possible about product so that it can be shown to customer, who can then make an informed decision on purchase. Information should be provided in industry accepted standards, so that it can be compared easily by customer.

Catalog: A catalog is online version of brochure or menu, which should contain all the products (services) business is providing. The products are further categorized/ sub-categorized into meaningful lists like electronics > kitchen appliances > microwave

Search: This is most used feature of any ecommerce website. Rather then looking for a product in catalogs, customer will prefer searching for it by keywords. A  good search functionality will make sure he gets what he is looking for (or better).

Shopping cart: All the selected products by customer will be shown in shopping cart. This is responsible for applying any offers, taxes, discount etc.

User Details/ Profile management: An ecommerce site might allow a user to shop without login to system. But at times it is desirable to let user create a profile, so that he does not need to enter his details (address, name, phone number etc) everytime he makes a purchase.

Order management: How can customer order for a product or a service? Can he order online, send emails, call up customer care or go to nearby store.

Payment: What are different modes of payment customer can use- Credit card, debit card, net banking, cash on delivery etc. Usually a third party payment gateway can be used to keep focus on business requirements.

Guarantee/ Warranty management: A product might have guarantee or warranty associated, which needs to be honored by the business to make sure customer is coming back to it.

Offers: Businesses need to come up with frequent offers/ discounts/ loyalty programs to boost up sales. Business also uses techniques like cross sell or up sell to increase profit and help customer make good choices.

Privacy Policy: A business might need to collect some information from customer like name, address, age (sometimes more private info) etc. What will happen to info collected from customer (kept private)? Also will cookies be used by the site?

Security: Customer might be sharing sensitive information online, so system needs to use https/ SSL for data collection. Additional requirement will be to secure data already collected by use of authorization and firewalls.

Customizing Perspective in Eclipse

At times you might want to add items/ perspectives into eclipse menus and toolbars. For example, when I installed Android plugin into eclipse, I was not able to find Android SDK and SVD managers in my eclipse. To add these, I had to

Go To Window-> Customize Perspective-> Command Groups Availability

Eclipse

Synchronization: Object vs Class level

In my last post I wrote about how synchronization can help making sure that multiple threads do not execute same block of code at the same time. I will take the concept of synchronization a little further here. Going back to previous example.

public void test()
{
 synchronized(this){
  try {
   count++;
   Thread.sleep(1000);
   System.out.println(“Hello from Runnable:”+count);
   } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

synchronized(this) or using synchronized keyword at method level makes sure that no 2 threads execute the block of code at the same time. But there is a bit more to it. To elaborate, I have modified my test method slightly

public void test()
{
 synchronized(this){
  try {
   count++;
   Thread.sleep(1000);
   System.out.println(Thread.currentThread().getName()+" says Hello from  R unnable:"+count); 
   } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

And modified calling main method

public static void main(String s[])
{
Runnable r=new RunnableTest();
Runnable r2=new RunnableTest();
for(int i=0;i<20;i++)
{
Thread t1=new Thread(r);
t1.setName("1");
t1.start();
Thread t2=new Thread(r2);
  t2.start();
  t2.setName("2");
 }
}

Now if I run this we will see something like

2 says Hello from Runnable:1
1 says Hello from Runnable:1
1 says Hello from Runnable:2
2 says Hello from Runnable:2
1 says Hello from Runnable:3
2 says Hello from Runnable:3

And if you will observe the output, it is appearing in pairs. So let us understand what is happening here.

First change, instead of one object of runnable we are using 2 objects, this is to basically highlight the behavior in synchronized(this) block. Second change is that I added name for thread so that we can print it out to understand which thread is actually executing.

Coming back to synchronized(this), look at the “this” keyword. “this” in Java refers to current object. So when we synchronize on this, it means we are synchronizing on current object, so as we are using two different objects, hence we see the behavior that two stream of threads is executing simultaneously in synchronized block. So we can say that with synchronized this, we make sure only one thread of the object is executing in synchronized block at a time.

Now there can be a situation where you want exactly one thread for whole class gets executed at a time, no matter if thread belongs to same object or not. There is a simple solution to it.

public void test()
{
 synchronized(RunnableTest.class){ //Change this with class name
  try {
   count++;
   Thread.sleep(1000);
   System.out.println(Thread.currentThread().getName()+" says Hello from  Runnable:"+count);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

There are other options for synchronization as well, not of much practical use (atleast I have not used them ever), but worth noting.

synchronized("1")

Remember, string is an object. So we are synchronizing based on a constant object here, which will make sure only one thread executes at a time, irrespective of class or objects. So for our example this will be same as synchronized(RunnableTest.class).

A more practical use can when we are trying to synchronizing a group of thread.

String key=Thread.currentThread().getName();
public void test()
{
 synchronized(key){
  try {
   count++;
   Thread.sleep(1000);
   System.out.println(Thread.currentThread().getName()+" says Hello from Runnable:"+count);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Here I am using key to group threads based on which synchronization is done. The key here is simply name of thread, but it can be more realistic in real scenario.

Handling Multithreading with Synchronization

In last post I wrote about how can we speed up our processing using multiple threads in Java, basically doing parallel processing using threads. Using multithreading definitely helps speed up processing, but it can also result in corrupting data if not used properly. Let’s look at this example

//A simple Runnable class
public class RunnableTest implements Runnable{
 int count=0;
 public void test()
 {
  try {
   count++;
   Thread.sleep(1000); //This will just introduce some delay
   System.out.println("Hello from Runnable:"+count);
   } catch (Exception e) {
    e.printStackTrace();
  }
 }

@Override
public void run() {
test();
}
}

//and call it from somewhere like
Runnable r=new RunnableTest();
for(int i=0;i<20;i++)
{
Thread t1=new Thread(r);
t1.start();
}

Output:
Hello from Runnable:20
Hello from Runnable:20
Hello from Runnable:20
Hello from Runnable:20
Hello from Runnable:20
..
..

We are calling run method 20 times which in turn calls test. We have deliberately introduced some delay in test to give it a feel of real method which is taking time to execute some statements. The output depicts that instead of count being printed as 1, 2, 3 and so on, it is 20 for all the calls. Which means that all the threads are trying to execute same block of code simultaneously. Though parallel processing of code is desirable behavior from multithreading, it can cause issues in scenarios mentioned above.

Let’s say in actual code we are trying to write data into a database. Instead of count we have ID. So first thread creates and ID as X. Then it does some processing and creates an insert statement. While it is doing the processing, a second thread starts executing the same code and creates another ID Y which overrides the ID X (same as count is being modified). So when our thread 1 reaches to the code where it is inserting the data, it will try inserting ID as Y (as X is overridden by Y). Then thread 2 comes and again try to insert ID Y, causing an error.

So in real world we can have blocks of code (or complete methods) which we want to make sure does not get executed by multiple threads at the same time. Fortunately Java provides a simple way to do this. The keyword is synchronized.

public void test()
{
 synchronized(this){
  try {
count++;
Thread.sleep(1000);
System.out.println("Hello from Runnable:"+count);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Now if we run the same code, we will get

Hello from Runnable:1
Hello from Runnable:2
Hello from Runnable:3
Hello from Runnable:4
Hello from Runnable:5
Hello from Runnable:6

Synchronized block makes sure that only one thread is executing blocked code at a time. Another way to synchronize is define method itself as synchronized.

public synchronized void test()

This will make sure only one thread enters this method at a time.

Multithreading: Runnable vs Thread

What is multithreading? First of all let us understand what is thread? A thread is simply flow of control in a program/ process.

Now Let’s understand what is multithreading? Let’s take an example, there is a code which reads data from a file and uploads it to a database. Let’s say there are 10 such files and each file takes 1 hr to finish data upload. In a single threaded environment, it will simply translate to 10 hrs job as our code will read and upload files one by one.  Whereas when we move to multithreading, that is multiple threads are executing at the same time asynchronously, it will result in parallel processing of these files and the total time taken might be close to time taken by one file only.

Before getting into details lets understand how to implement multithreading in Java? There are 2 ways to achieve this.

//1. Extend Thread class
public class ThreadTest extends Thread{
 public void test()
 {
  System.out.println("Hello from Thread");
 }
 public void run()
 {
  test();
 }
}
 //2. Implement Runnable interface 
public class RunnableTest implements Runnable{
 public void test()
 {
  System.out.println("Hello from Runnable");
 }
@Override
 public void run() {
  test();
 }
}

//And somewhere in the main method 
public static void main(String s[])
{
 Thread t1=new ThreadTest();
 t1.start();
 Thread t2=new Thread(new RunnableTest());
 t2.start();
}

As you can see the important method while using threads is “run”. This method gets called automatically by JVM whenever a thread starts.

Note that we do not call the run method directly, as that will be like calling a normal method and will wait for control to come back to calling class. Whereas using start will make sure the control flow continues without waiting for method call return. To understand what I am referring here, lets change the main method slightly

for(int i=0;i<20;i++)
{
 Thread t1=new ThreadTest();
 t1.start();
 Thread t2=new Thread(new RunnableTest());
 t2.start();
}

We will something like (no order)

Hello from Thread
Hello from Runnable
Hello from Thread
Hello from Runnable
Hello from Runnable
Hello from Thread
Hello from Thread
Hello from Thread
Hello from Runnable
Hello from Runnable

Now let’s change it to run

for(int i=0;i<20;i++)
{
 Thread t1=new ThreadTest();
 t1.run();
 Thread t2=new Thread(new RunnableTest());
 t2.run();
}

Output

Hello from Thread
Hello from Runnable
Hello from Thread
Hello from Runnable
Hello from Thread
Hello from Runnable

So not actually using multithreading here.

Coming back to original example. Now you can see if we write the code for uploading the data from file to run method, and execute it in multithreaded fashion, we will be able to upload files simultaneously.

We have seen 2 ways to create threads in Java, one by extending thread class and other by implementing runnable. Which is better?

Usually we go for implementing runnable. When implementing runnable we can only override run method, and that is the only thing which we need in most of the cases. Whereas when we extent Thread, we can override other methods like start, sleep, stop etc, which is usually not required. Additionally, extending Thread has the obvious implementation that now our class cannot extend any other class (as Java allows only one class to be extended).

Getting rid of Babylon virus

Well, it is not exactly a virus, but babylon plugin can impose itself on your browser in form of search toolbars or default search setting. It has happened to me more than once now, so sharing some quick steps of getting rid of it here. Please note that at times this app/ toolbar will be stored as any other name, so look out for anything suspicious and out of place.

1. Remove the app: Check if a program is shown under add/ remove program (go to control panel-> uninstall program) with name babylon. Remove it.

2. Remove toolbar: Check if toolbar exists under browser toolbars, and remove it.

Firefox: Go to Add Ons->look for babylon

IE: Go to Manage Add Ons->Toolbars and extensions->look for babylon

3. Remove as default search engine: For IE Manage Add Ons-> Search providers->babylon (in my case it was named just default)

More info www.pcmag.com/article2/0,2817,2418379,00.asp

Error using Maven with Eclipse Kepler

When I tried using Maven’s embedded version in Eclipse Kepler, it gave an error.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

A little googling showed this as a genuine issue with embedded version of Maven in Eclipse. The workaround is to download external maven and link to eclipse.

For windows.

  1. Download Maven zip from http://maven.apache.org/download.cgi
  2. Unzip the file to some location say “D:\Maven”
  3. In Eclipse go to Windows->Preferences->Maven->Installation; Click add and provide the path in step 2 (D:\Maven)

Related http://stackoverflow.com/questions/11916706/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder-error

Message Oriented Middleware

In last post I talked about what is middleware, I will focus on message implementation of same today. Message oriented middleware or MOM mostly uses message queues to send and receive data between two systems.

In simple terms, a message is anything that is being sent from one system to another. Mostly MOM uses XML formats, sometimes SOAP based requests or plain texts. An example MOM system will send message to a  message queue or MQ, from where the receiver will pick up the message.

Advantages of Message Oriented Middleware

  1. Persistence: In normal client-server architecture, we will need to make sure both the systems to be available to have a successful communication. Whereas if we are using MQs, one system can still send messages even if the second is down.
  2. Support for Synchronous and Asynchronous communication: by default the communication is asynchronous but we can implement a synchronous system where a request message sender will wait for the response from other party.
  3. Messages can be consumed at will: If one system is busy when messages are received (which do not need immediate response), it can consume the messages when load is less. For example, a lot of systems are designed to consume the messages at non business hours.
  4. Reliability: As messages are persistent, threat of losing information is low even if one or more systems are not available. Additional security mechanism can be implemented in MQ layer.
  5. Decoupling of systems: Both client and server work independently, and often do not have knowledge for other end. System A creates a message and adds to message queue, without concerning who will pick it up as long as it gets the response message (if required). So one system can be written in Java and other can be in Dot Net.
  6. Scalability: As both machines involved in interaction are independent of each other, it is easier to add resources at either end without impacting the whole system.
  7. Group communication: Sender can send message to multiple queues or same queue can have multiple listeners. In addition Publisher- Subscriber approach can help broadcast a message.

Types of Messaging:

Point to Point: This is a simple messaging architecture where a sender will directly send a message to receiver through message queue.

Publisher-Subscriber (Pub-Sub): This type of communication is required when sender wants to send messages to multiple receivers. Topics are defined to which subscriber can subscribe and receive requests based on same. For example, say a core banking system can trigger messages on various events like new account open, a withdrawal is made, interest rate changed etc. For an event, multiple other systems might want that information to take an action, so say for all withdrawal events, systems like fraud detection, mobile messaging system, daily reporting system, account maintenance system subscribe. Whenever, publisher publishes the message to “Withdrawal” topic, all of these systems will receive the message and take appropriate action.

Forgot your Tortoise SVN Password?

It happen to me a couple of times. Basically in Tortoise svn, you just need to add the password once and save it, it will keep working for months or years. In case I need to checkout another repository and it would ask the password again, it would be a nightmare.

But this simple tool helped- http://www.leapbeyond.com/ric/TSvnPD/

Download the exe file and execute it, it will pull out all the passwords saved for SVN.

Union vs Union All

The simple difference between using Union and Union All is that Union selects only distinct records from both the resultsets, whereas Union All will return everything.

But today I figured out one more use of union All today. In a ms sql query, I was using union to add up resultset of multiple queries, and got this error

“The text data type cannot be selected as DISTINCT because it is not comparable.”

As I knew none of my queries will return duplicate records, I did not need distinct clause. Replacing Union with “Union All” did the trick in this case.