Captain API of Cockpit

Captain plans committed assignment through mapping duties with start/given, when, then and executes them non blocking asynchronously in channel and ends with delivery.

Manna Mahmud
5 min readDec 11, 2018

--

CAPTAIN API in action

We discuss this Captain API at bottom; if you with no time to read all then please scroll to bottom.

Its been long we are working on this java framework called “COCKPIT” targeting next year mid. Cockpit is an engine for intranet, so large organizations can have their own network in cloud quickly and connect with their legacy systems by reactive microservices and creating quick single page frontends which will be a mobile app and desktop application too.

To make it simple for java developers we bring in frontend development framework also with java. You need not to know Nodejs or React or Angular or Vue or write unmanageable tons of javascripts. You can directly write HTML5 with binding java pojo models with a css file and manage it in a java class, ops not in serve side but in client side, is not it interesting? and MOST Interesting debugging frontend in IDE as its java!! and see immediate affect after build. And its reactive and faster. And its a maven project no need of different build tools for different sub projects. All backend gateway, microservices and frontends under one pom!! and one build.

and even no need of any tomcat or apache server, you can easily write your own proxy, gateway or microservice with jersey in few minutes and deploy and bind with gateway just by one line add. Its stateless with port binding and having SSO features and its following 12 factor architecture closely.

Engine & Gadgets

It has two parts; Engine if you start the engine you will get the mother portal ready with Gadget Loader. You can create JSON layouts and add gadgets per page and there is a design mode of the mother portal which will give drag and drop feature to design gadgets and layouting. and you can design pages on runtime on the fly. So develop a gadget and use it any where and it supports multi instance, offline mode, websockets. And you can use the gadget in web, desktop and mobile too!! is not it life saving? But how this has become possible?

Cockpit is fully a java based framework to support all need of intranet in a cloud. Java can run in Desktop and Android so here we use the JVM of them to run. But how we do it web, its also possible by available javascript JVMs for browsers which can do AOT compilation of javacode for browser or also can run java class directly in browser by just in time compilation of classes (its not GWT type thing but straight deals java class), so we used that feature, so instead of requireJs, we have our own classloader which can load anytime any Gadget java class quickly asynchronously. And as having own proxy and API gateway and frameworks, cockpit can provide as many website as needed, using same gadgets by creating instances on runtime with user, company wise data. How? lets discuss on backend then.

The backend is on netty. Netty is a non blocking network programming framework and tools project. We created a API over it to create Gateway and Microservices in few line of codes. And bind microservices with Gateway by one declaration, I wrote something before about it, you can read if you feel interested: https://medium.com/@mrmanna/netty-jersey-marriage-b7f12ac8d4a9

We have used Jersey to write microservice endpoints as most developers are familiar with this API but it was not asynchronous. We integrated with netty and made it deliver asynchronously. But we were not happy, so looking for more functional way to process data in microservices so we come up with a new API called Captain API.

What is Captain API? It is a promise based asynchronous functional execution API of tasks. CAPTAIN is responsible to deliver by handling states (success,fail) of tasks according to plan and which to invoke after which task depending on runtime results. It starts like Captain gets a assignment, he commits to the assignment and first thing first, creates the Duty plan. Which will be executed in future. Captain duties can be planned like scrum stories Given When Then. Ok, this was for normal readers but if you are a java developer than this is Promise which we take from netty channel which has a listener waiting for prepared Response for him which he can write back to right channel from this jersey container. And we do not invoke promise listener until our Captain accomplishes his duties to prepare the Response. So Captain (which is a RunnableFuture) has a Priority List as queue (FIFO) where he puts his plan and in his plan he puts Duties which are also Future objects with a FunctionalInterface which we can write with lambda expression quickly. The priority list executed based on tags(start, when, then) and sequence of queue and runtime state change of duty to success or failure. Whats the benefit? first can you see the image no nested IF ELSE and then non blocking and then you can run and have two captains executing two plans parallel and yes its asynchronous. There are plenty more benefits of Captain, we are happy to have him at our home ;). Soon we plan to do a Coffee with Captain, if you are in Dhaka, you can drop me your email in comments. I will update you on the date to join.

Manna, Cloudoffice.

--

--