Friday, October 19, 2012

Java Obtain Thread Dump

In Java, a thread dump can be useful in giving you some clues when an application is hanging or consuming a lot of CPU resources (e.g. infinite loop).

Obtaining one is actually quite easy, via the "jstack" utility shipped with the JDK.

Just login to the server and issue the command "jstack <pid>".

Thursday, October 18, 2012

Linux Load Average

The "Load Average" metric that you see in Linux using a tool like "top" actually refers to the number of processes that are wanting to use CPU time at each point in time. One advantage of using this definition over just raw CPU utilization is that it takes into consideration situations where the system isn't able to work on processes due to waiting for IO etc. So it then becomes a good proxy for how quickly the system is likely to respond to new processes and tasks.

However, one gotcha with this is that it gives artificially low readouts for thread based programs such as Java. You can have a Java process with hundreds of threads, most of which waiting to use the CPU as the CPU is maxxed out, and still get a low load average.

References:

Monday, October 8, 2012

Javascript Thread Safety

Browser so far run Javascript single-threaded, even if the programming model often uses asynchronous events, unless "WebWorkers" is explicitly used.

So no need to worry about thread-safety.

References:

AngularJS $resource Service

AngularJS provides the $resources service to provide an easy way to access REST resources.

Creating services can be as simple as follows:

angular.module('myResourceService', [ 'ngResource' ])
.factory(
'Cat',
function($resource) {
return $resource('/ws/cat/:catId',
{}, {
// resource with no custom method
});
})
.factory(
'Dog',
function($resource) {
return $resource('/ws/user/:dog/dogId',
{}, {
update: {method: 'PUT'} // custom method
});
});


Once they have been created, getting something from the web service is as simple as e.g.

Cat.get({catId:’xxxxx’},function(data) { // callback function contents });


By default, the following methods are implicitly declared for each resource:

{ 'get':    {method:'GET'},
 'save':   {method:'POST'},
 'query':  {method:'GET', isArray:true},
 'remove': {method:'DELETE'},
 'delete': {method:'DELETE'}
};


Notice that PUT is not supported, which is why it has to be added as a custom method (see example on top).

References:

Sunday, October 7, 2012

AngularJS Select Input

In HTML template

<select ng-model="langId" ng-options="lang.id as lang.name for lang in languages">
</select>

In the controller

$scope.languages = [
   {id:'en', name:'English'},
   {id:'zh', name:'Chinese'}, etc....
   ];

Example fiddle: http://jsfiddle.net/2JWb2/5/

AngularJS API Reference for $watch

And also some other methods. They're hidden under the "scope" API and rather difficult to find.
http://docs.angularjs.org/api/ng.$rootScope.Scope#$watch

AngularJS "ng-include" Gotcha with Controllers

I had a page with many forms in an accordion style grouping, so I decided to have the individual forms in separate HTML documents which are pulled together using "ng-include". All these forms, however, were using the same Controller declared in the parent document.

This turned out to be problematic, as I was finding that the controller's scope variables were not being updated. After much troubleshooting, it seems that every HTML fragment needs its own controller. I'm still not exactly sure why but this seems to be the only way to make things work.

Wednesday, October 3, 2012

Tuesday, October 2, 2012

MySQL Change User Password

Done as follows:

SET PASSWORD FOR 'dbuser'@'%' = PASSWORD('xxxxxxxx');