Tuesday, May 24, 2016

Fixing startup order resolver warnings in WSO2 C5



To really understand the concepts and basics on startup order you need to follow up https://medium.com/@sameera.jayasoma/resolving-startup-order-of-carbon-components-in-wso2-carbon-5-0-0-497fe3287e67#.3rlhypnrv first. With WSO2 Carbon 5 development, you might come across continuous warnings like below when starting up the server.

Problem

WARN {org.wso2.carbon.kernel.internal.startupresolver.StartupOrderResolver} - Startup component wso2-microservices-server from bundle(msf4j-core:1.1.0.SNAPSHOT) will be in the pending state until Capability org.wso2.msf4j.Microservice from bundle(org.wso2.carbon.bpmn.rest:5.0.0.SNAPSHOT) is available

WARN {org.wso2.carbon.kernel.internal.startupresolver.
StartupOrderResolver} - Startup component wso2-microservices-server from bundle(msf4j-core:1.1.0.SNAPSHOT) will be in the pending state until Capability org.wso2.msf4j.Interceptor from bundle(org.wso2.carbon.bpmn.rest:5.0.0.SNAPSHOT) is available

What this means in simple terms is that microservice-server is waiting for a microservice from bpmn.rest component.

Solution
Now if you have written up few microservices, you could add them to your pom.xml like below. In this case since we know the exact number of microservices that we wrote we can directly define the count too.

<carbon.component> osgi.service;objectClass="org.wso2.msf4j.Microservice";serviceCount="10"
</carbon.component>


If you read above provided link you would get an idea on how this works. Basically we are telling that Microservice will be expecting 10 microservices (which implements org.wso2.msf4j.Microservice)
But adding this will only solve the first warning. In this usecase I have a class implementing Interceptor interface too. Therefore I could update it as below.

<carbon.component>osgi.service;objectClass="org.wso2.msf4j.Interceptor";serviceCount="1",
osgi.service;objectClass="org.wso2.msf4j.Microservice";serviceCount="10"
</carbon.component>

Monday, May 16, 2016

Lambda expressions: the easy way

With the introduction of Java 8, a key concept that came out was lambda expressions. It is a bit confusing at the start, but once you get the hang of it, it really simplifies the code. So here are few key points that helped me in understanding the concept of Lambda expressions.s

1. Behavior Parameterization

Behavior parameterization is the idea behind lambda expressions. That is now you can pass a code block as an argument to a method, just like you used to pass variables and objects.

2. Syntax of lambda

input parameters - > body of lambda  (expression/block of code)

The important thing to note here is , that if the body of lambda is an expression, it will return the value of the expression, hence you do not need to add keyword 'return'. However if you have a set of statements executed, then you need to add the keyword 'return' as well as an ending ';'.

for example,

 (int a, int b) -> a +b

(int a, int b) -> {
                           int c = a + b;
                           return c * a;
                        } ;

3. Functional interfaces

A functional interface is an interface that specifies only one abstract method.
Lambdas can be used in the context of functional interfaces. That is lambda expressions  can be treated as an instance of the mentioned functional interface, hence you do not need to specify the input parameter types.
For example if we consider 'Predicate' functional interface that is provided by Java, which returns a boolean value, we could write it like ,

Predicate<Person> p = (Person p1) -> p1.getAge();

also we could write it like ,
Predicate<Person> p = (p1) -> p1.getAge();

So there are a number of supported functional interfaces that are provided from Java, which you could check in [1] . However if you need to write your own custom functional interface, you could do it with the use of @Functional. This annotation is not mandatory, but it will throw an error if there is anything more than one abstract method defined.

[1] https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html


                         



Monday, May 2, 2016

Latest version of Javascript - ECMAScript!

ES6 also known as ECMAScript is the latest standardized version of javascript. Following is a good intro to what ECMAScript has.

 http://blog.teamtreehouse.com/get-started-ecmascript-6

 From the look of it, JavaScript seems to be getting more object oriented with concepts like classes, inheritance. Good move!