Check the tomcat app.log in case of unexpected error

If you encounter some unexpected error with your web app, the best place to check what’s wrong is Tomcat’s app.log file.

1. Firstly, ssh to the server containing your app:

$ssh -A xxx.dev.XXX ( you can set shortcuts like in this file  ~/.ssh/config)

2. Get the root access

$sudo su –

3. Get the tail of the log. Tail command is useful to see the latest log entries and -f helps to keep refreshing it.

$tail -f app.log

Then replicate the bug, you should see the error message there. It can be null pointer exception for instance.

JavaScript, why are you like this?

JavaScript have some weird quirks. And one of them is how pointer “this” works.

look at the code below:

[code language=”javascript”]
var c = {
name: "this is c object",
log : function(){
this.name = "updated c object";
var setName = function(newname){
this.name = newname;
}
setName("Updating again! The c object");
console.log(this);
}
}
[/code]

Look at the line 6, you might think that name has been set to value “Updating again! The c object”, because this is pointing to c object. BUT, if you run the code, you will find out that actually line 6, is creating new Global variable name, and setting it’s value to “Updating again! The c object”. This happens because of how the execution context is created in JavaScript. In JS, the interpreter will hoist all the variables and methods first. Meaning before real execution line 6 is interpreted and variable is set to global variable(sort of being pushed all the way up the code).

The solution would be to store the reference to c object then pass it.

[code language=”javascript”] var c = {
name: "this is c object",
log : function(){
var self = this;
// note some developers use "that" instead of self
self.name = "updated c object";
var setName = function(newname){
self.name = newname;
}
setName("Updating again! The c object");
console.log(self);
}
} [/code]

This way, we will not set the name to be global variable and it will be set to c object. So c.name will return “Updating again! The c object”

String … and Generating Javadocs

In java we can pass some arguments in methods. Sometimes we want to pass the array of objects. So if you see writeText(String … text) is actually nothing but array of Strings. It’s same as writeText(String[] text).

To easily Generate javadocs in Intelij Idea you can use this shorcut.

type /** then press enter. This applies for the methods. Later you can add more details to that javadoc

ex.:

/**

public String addText(String[] text){

return ‘abc’;}

becomes

public String addText(String[] text){

return ‘abc’;}

Java Streams

Since java 8, we have access to java streams api. It works similar to java collections(List, Set, Queue).

The Java 8 Streams can be seen as lazily constructed Collections, where the values are computed when user demands for it. Actual Collections behave absolutely opposite to it and they are set of eagerly computed values (no matter if the user demands for a particular value or not).

Just like functional programming languages, Streams support Aggregate Operations. The common aggregate operations are filter, map, reduce, find, match, sort. These operations can be executed in series or in parallel.

The Streams also support Pipelining and Internal Iterations. The Java 8 Streams are designed in such a way that most of its stream operations returns Streams only. This help us creating chain of various stream operations. This is called as pipelining. The pipelined operations looks similar to a sql query. ex. a.b().c()

Source: https://dzone.com/articles/understanding-java-8-streams-1

GraphQL

I’ve started learning GraphQL and decided to share some information that I find useful.

GraphQL is a query language to retrieve data from the server.

Unlike REST queries, GraphQL is “smart”. Main benefit is that, instead of many REST nodes, we only have one smart node, that can provide us with the exact information we need.

Basic GraphQL queries

People commonly call everything that hits your GraphQL API server a “query”. But there are a lot of things mixed in there. What do we call a unit of work we’re asking the server to do? It could be a query, a mutation, or a subscription. 

GraphQL Operation.JPG

Selection sets

GraphQL Selection Set.JPG

  • Selection set: A set of fields requested in an operation, or nested within another field. A GraphQL query must contain a selection set on any field that returns an object type, and selection sets are not allowed on fields that return scalar types, such as Int or String.

Source: https://dev-blog.apollodata.com/the-anatomy-of-a-graphql-query-6dffa9e9e747

SCHEMA

[String!] vs [String]!

This means that the list itself can be null, but it can’t have any null members. For example, in JSON:

Now, let’s say we defined a Non-Null List of Strings:

This means that the list itself cannot be null, but it can contain null values:

Check if the JSP file on app server is latest

Sometimes deploying the app may not refresh jsp files on app server. So you might see in browser that app has old logic. To confirm that app server doesn’t have latest files go to app server. Typically:

$ssh -A appServer (https://sheberlik.wordpress.com/2017/09/20/connect-to-batch-server/)

Then switch to root

$ sudo su –

Then navigate to the folder

$cd /opt/tomcat/webapps/ROOT/WEB-INF/views/

then open the jsp file and see the content

If the file is old, just re-deploy via Jenkins

 

Living in the Singapore, cost of living, finding the room or apartment

It’s been almost 9 months I’ve worked in Singapore. I will be sharing some information for those whom can benefit from it.

The average cost that I spend per months goes as follow, all values are in SGD:

  1. Rent – 750 for common room(need to pay deposit 1 month when starting the contract )
  2. Transport(bus and mrt) – 70-80
  3. Communications(Singtel) – 70
  4. Food(fruits, lunch, dinner, snacks etc) – 500

Total: ~1400

The best way to find the room or apartment is to ask the agent to find it for you(you will need to pay the agent fee which equates to the half of the rent(for both room or apartment)). I find the agents from  http://www.orangetee.com most useful(can recommend to email to george.kyaw@orangetee.com please note he only handles the apartment rentals)

If you don’t want to pay agent fee, then your best chance is to go to easyroommate.com and put your add there.

Another places where to find the room/apartment is https://www.gumtree.sg/, http://www.propertyguru.com.sg , nestia.com (in no particular order). Or facebook groups.

How to merge the previous branch to current one using SmartGit

In Smartgit, click Merge, then click branches. Uncheck all. Then check the Origin->Previous sprint – > Merge to Working Tree.

Then see if there is conflicts. If there is one, you can either right click ->Resolve then choose our file or their file. 2nd way is to open conflict by clicking twice on the file, then clicking on the arrow to accept the correct code.

Then Commit->Push

More about JavaScript closures

From my understanding closures is extremely important concept in JavaScript. Let’s learn it in details:

Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions ‘remember’ the environment in which they were created.

Lexical Scooping

Lexical scooping is a type of closure.

function init() {
var name = “Mozilla”; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert (name); // displayName() uses variable declared in the parent function
}
displayName();
}
init();

Here you can notice that init() has function inside. Then when init is called, that function is called too.

Git: how to use cherry pick using smartgit

If you have have multiple git branches and you have pushed your code to branch x, but you also want it to be available on other branch too ex.: branch Y, then perform following steps:

  1. Checkout the branch Y, the place where you want to push your code
  2. Then from toolbar above, Branch->Cherry Pick
  3. Click Branches, and un-select the Head. Then select the branch X. Choose the commit you want to add to branch Y.
  4. Select and press Cherry pick and commit
  5. then press Push
  6. Done

Now branch Y has commit from branch X.