Load Balancing Apache Tomcat with nginx

Reading Time: 5 minutes

Nginx (pronounced ‘engine x’) is an HTTP and reverse proxy server. It is well known for its high performance and stability. It is pretty feature rich and very simple to configure. Nginx hosts nearly 12.18% (22.2M) of active sites across all domains. Nginx uses event-driven architecture to handle requests. When compared to a thread-per-request model, event-driven is highly scalable with a low and predicatble memory footprint.

Nginx is very easy to setup as a load balancer for an Apache Tomcat farm. In this blog post, I will show you how to set it up as a round-robin load balancer for two Apache Tomcat servers.

You first need to install nginx, you can find the installation instructions here. If you are on a Mac, then you could use Homebrew
brew install nginx
After installing, you can run it using the nginx command
sudo nginx
You can now test the installation by opening the following URL in a browser
http://localhost:8080
Lets change the default port 8080 to port 80. You can do that in the nginx.conf file which by default is located at /usr/local/etc/nginx/nginx.conf. First stop nginx
sudo nginx -s stop
Now open the nginx.conf file and locate the listen attribute of the server. Change the value from 8080 to 80. Here is where I made the change in my configuration:

server {
listen 80;
server_name localhost;

Save the file and start nginx. You should now be able to test the configuration change by visiting http://localhost
Install two instances of Apache Tomcat. Open their server.xml files and change the HTTP port numbers to 8080 and 8081 respectively. You can find more information on Apache Tomcat configuration here. Once you have made the change and saved the configuration files, you should now start each instance of Apache Tomcat. Here are a few ways you can start Apache Tomcat. Typically you should be able to locate the bin directory of inside your Tomcat installation and invoke the startup.sh file as shown:
bin/startup.sh

Now that the Tomcat instances are started, lets setup the round robin load balancer. You would need to use the nginx upstream module. The upstream module allows you to group servers that can be referenced by the proxy_pass directive. In your nginx.conf, locate the http block and add the upstream block to create a group of servers:

http {
upstream tomcat_servers{
ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}

ip_hash above configures the load balancing method where requests are routed to servers based on IP Addresses of the client i.e. a request from a client with a particular IP will always go to the same backend Tomcat Server.

Finally, you need to locate the location block within the server block and map the root(/) location to the tomcat_servers group created using the upstream module above.

location / {
proxy_pass http://tomcat_servers;
}

Thats it!. Restart nginx and you should now be able to send a request to http://localhost and the request will be served by one of the Tomcat servers.

Love for your data loader never stops

Reading Time: 2 minutes

Just when you thought it couldn’t get any better, it got better. Dataloader.io, the most popular Salesforce data loading solution on the Salesforce AppExchange now supports importing and exporting of files to and from Dropbox!

dataloader.io and dropbox

Data loading aficionados can now quickly and easily import or export data directly to and from their Dropbox account. By simply entering your Dropbox credentials, users can make Dropbox their source for CSV files. Similarly, exporting to Dropbox is as easy as choosing Dropbox as your connection and destination folder from a tab. Then, by following the standard steps to import and export data with dataloader.io, you’ll be up and running in no time – it’s that simple!

If you are not already using dataloader.io, head over to the Salesforce AppExchange to get started. Interested in doing more with Dropbox? Check out our Anypoint™ Dropbox connector page to learn about how to extend the features of Dropbox across third party applications and services. If you’re interested in what other Salesforce integration solutions MuleSoft has to offer, visit our Salesforce solutions page.

Also, check out this nifty infographic for data loading tips if you’re already a dataloader.io pro:

dataloader dropbox connectivity

A brief introduction to JavaScript objects

Reading Time: 15 minutes

When you come from a class based programming language, doing objects in JavaScript feels weird: Where is the class keyword? How can I do inheritance?

As we are going to see, JavaScript is actually pretty simple. It supports class like definition of objects and single-inheritance out of the box.

But first, a small tip to change your reading experience. By using your browser JavaScript console, you can play with the examples without leaving this page:

  • Chrome: MacOSX Cmd-Alt-J / Windows Ctrl-Shift-J
  • Firefox: MacOSX Cmd-Alt-K / Windows Ctrl-Alt-K
  • Safari: Cmd-Alt-C (only if you enable the Develop menu on Advanced Settings)
  • IE8+: Press F12 and go to the console

The basics

Ok, all set. Now the first step, define an object:

var point = {x: 1, y: 2};

As you can see the syntax is pretty straight forward, and object members are accessed by the usual means:

point.x // gives 1

Also we can add properties at any time:

var point = {};
point.x = 1;
point.y = 2;

The elusive this keyword

Now lets move on to more interesting things. We have a function that calculates the distance of a point to the origin (0,0):

function distanceFromOrigin(x, y) {
    return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}

The same function written as a method of point looks like this:

var point = {
    x:1, y:2,
    distanceFromOrigin: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
};

If we evaluate: point.distanceFromOrigin(), the this keyword becomes point.
When you come from Java it may sound obvious, but as we go deep into the details of JavaScript, is not.

Functions in JavaScript are treated like any other value, it means that distanceFromOrigin doesn’t have anything special compared to the x and y fields. For example we can re-write the code like this:

var fn = function () {
    return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
};
var point = {x:1, y:2, distanceFromOrigin: fn };

How this is determined?

JavaScript knows how to assign this, because of how distanceFromOrigin is evaluated:

point.distanceFromOrigin();

But doing just fn() will not work as expected: it will return NaN, cause this.x and this.y are undefined.
Confused? Lets go back to our initial point definition:

var point = {
    x:1, y:2,
    distanceFromOrigin: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
};

Since distanceFromOrigin is like any other value, we can get it and assign it to a variable:

var fn = point.distanceFromOrigin;

Again fn() returns NaN. As you can see from the two previous examples, when a function is defined there is no special binding with the object. The binding is done when the function is called: if the obj.method() syntax is used this is automatically set to the receiver.

It’s possible to explicitly set this?

JavaScript functions are objects, and like any object they have methods.
In particular a function has two methods apply and call, that executes the function but allows you to set the value for this:

point.distanceFromOrigin() // is equivalent to…
point.distanceFromOrigin.call(point);

For example:

function twoTimes() {
    return this * 2;
}
twoTimes.call(2); // returns 4

Defining common behavior

Now suppose that we have more points:

var point1 = {
    x:1, y:2,
    distanceFromOrigin: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
};
var point2 = {
    x:3, y:4,
    distanceFromOrigin: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
};

It makes no sense to copy & paste this snippet each time that you want to have a point, so a small refactoring helps:

function createPoint(x, y) {
    var fn = function () {
            return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
    return {x: x, y:y, distanceFromOrigin:fn};
}
var point1 = createPoint(1, 2);
var point2 = createPoint(3, 4);

We can create lots of points in this way, but:

  • It makes an inefficient use of memory: fn is created for each point.
  • Since there is no relationship between each point object, the VM cannot make any dynamic optimization. (ok this is not obvious and depends on the VM, but it can impact on execution speed)

To fix these problems JavaScript has the ability to do a smart copy of an existing object:

var point1 = {
    x:1, y:2,
    distanceFromOrigin: function () { 
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2)); 
    }
};
var point2 = Object.create(point1);
point2.x = 2;
point2.y = 3;

Object.create(point1) uses point1 as a prototype to create a new object. If you inspect point2 it will look like this:

x: 2
y: 3
__proto__
    distanceFromOrigin: function () { /* … */ }
    x: 1
    y: 2

NOTE: __proto__ is a non-standard internal field, displayed by the debugger. The correct way to get the object prototype is with Object.getPrototypeOf, for example: Object.getPrototypeOf(point2) === point1

This way of handling objects as copies of other objects is called prototype-based programming, and conceptually is simpler than class based programming.

The ugly syntax part

So far I told you the nice part of the history.

Object.create was added in JavaScript 1.8.5 (aka ECMAScript 5th Edition or just ES5). So, how objects were cloned in previous versions of the language?

Here comes the ugly syntax part. Every function is an object, so we can add properties to functions dynamically:

fn.somevalue = 'hello';

Suppose for a minute that we have Object.create. So we can use function objects and Object.create to get all the information required to copy and initialize objects in a single step:

// we store the "prototype" in fn.prototype
function newObject(fn, args) {
    var obj = Object.create(fn.prototype);
    obj.constructor = fn; // we keep this reference... just because we can ;-)
    fn.apply(obj, args); // remember this will evaluate fn with obj as "this"
    return obj;
}

Ok, but I told you that we don’t have Object.create yet, what we do?
JavaScript has a keyword that does the same as the newObject function:

newObject(fn); // is equivalent to..
new fn()

NOTE: For explanation purposes I’ve shown how to implement new using Object.create. Take account that new is a language keyword, and even when it’s semantically equivalent to newObject, the implementation is different. In fact for some JavaScript engines creating objects with new is slightly faster than Object.create.
Also Object.create is a relative recent addition, in old engines like IE8, the usual trick is to implement it using new. I showed Object.create first because it makes things easy to understand.

Why JavaScript has this strange use of functions? I don’t know. My guess is that probably the language designers wanted to resemble Java in some way, so they added a new keyword to simulate classes and constructors.

By using new you can write the previous point example like this:

// the point constructor
function Point(x, y) {
    // "this" will be a copy of Point.prototype
    this.x = x;
    this.y = y;
}
// the prototype instance to copy
Point.prototype = {
    distanceFromOrigin: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
};

Now each time that we do new Point(x, y) we get a new point:

var point1 = new Point(1, 2);
var point2 = new Point(2, 3);

Things to know about prototype and constructor

When you evaluate obj.x, the engine follows this logic:

  1. Does obj defines x? If the answer is yes, then x from obj is used.
  2. Otherwise search for x in the prototype.
  3. If not found yet, continue with the prototype of the prototype.

As you can see this is similar to the method lookup used in class based programming languages, just replace prototype with super class.

But since the prototype field is almost like any other field, we can do cool dynamic stuff like adding new methods to existing instances:

var hello = "Hello";
String.prototype.display = function () { console.log(this.toString()); }
hello.display()

And what about constructor?
Every object in JavaScript has a constructor property, even if you don’t define it. When the object created using new the constructor property points to the function used to create the object.

Single inheritance

We can apply what we learned to do single-inheritance:

// the "super class"
function Point(x, y) { this.x = x; this.y = y; }
Point.prototype = {
    distanceFromOrigin: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
};

// the "sub class": ColoredPoint extends Point
function ColoredPoint(x, y, color) {
    // call the "super constructor"
    Point.call(this, x, y);
    this.color = color;
}
// We use a clone of Point.prototype as the extension prototype
ColoredPoint.prototype = Object.create(Point.prototype);
// we extend Point with the show method
ColoredPoint.prototype.show = function () {
    console.log('Point with color ' + this.color + ' at (' + this.x + ',' + this.y + ')');
}

// finally we can use colored points:
var p = new ColoredPoint(10, 20, 'red');
console.log(p.distanceFromOrigin());
p.show();

As you can see it’s possible to do single inheritance, but there are a lot of required steps. That’s why there are so many JavaScript libraries to simplify the definition of objects.

In a next post I’ll share my experiences on creating barman (one of the many JavaScript object definition libraries that are out there). And I’ll use that experience to discuss some “advanced” techniques to share behavior like mixins, and traits.

How to build a Processing Grid (An Example)

Reading Time: 8 minutes

In his “To ESB or not to ESB” series of post, Ross Mason has identified four common architectures for which Mule is a great fit: ESB, Hub’n’Spoke, API/Service Layer and Grid Processing. In this post we are going to detail an example for the latter architecture. We will use Mule to build a scalable image resizing service.

Here is the overall architecture of what we intend to build:

As you can see, we allow end users to upload images to a set of load balanced Mule instances. Behind the scene, we rely on Amazon S3 for storing images waiting to be resized and Amazon SQS as the queuing mechanism for the pending resizing jobs. We integrate with any SMTP server for sending the resized image back to the end user’s email box. As the number of users will grow, we will grow the processing grid simply by adding more and more Mule instances: indeed, each instance is configured exactly the same way so the solution can easily be scaled horizontally.

Read on to discover how easy Mule makes the construction of such a processing grid…

Continue reading