Happy Coding, Happy Life

Java 8 - First Impression of Lambda Expression

| Comments

One of the most prominent and widely used languages in the world has Evolved.

Java ever gave us power on object oriented progrmming and we did the best we could with it.

Now there’s another new elegant way changing the java world-Lambda Expression, that will make our code more expressive, easier to write, less error prone, and easier to parallelize than has been the case with Java.

Lambda Expression

Let’s start from a simple example:

Suppose we’re asked to total the prices greater than $20, discounted by 10%. Let’s do that in the habitual Java way first.

Old way

final List<BigDecimal> prices = Arrays.asList(
  new BigDecimal("10"), new BigDecimal("30"), new BigDecimal("17"), 
  new BigDecimal("20"), new BigDecimal("15"), new BigDecimal("18"), 
  new BigDecimal("45"), new BigDecimal("12"));

  BigDecimal totalOfDiscountedPrices = BigDecimal.ZERO;   
  for(BigDecimal price : prices) { 
          if(price.compareTo(BigDecimal.valueOf(20)) > 0)
                  totalOfDiscountedPrices = totalOfDiscountedPrices.add(price.multiply(BigDecimal.valueOf(0.9)));
      System.out.println("Total of discounted prices: " + totalOfDiscountedPrices);

A Better way

Now we can do better, a lot better. Our code can resemble the requirement specification. This will help reduce the gap between the business needs and the code that implements it, further reducing the chances of the requirements being misinterpreted.

final BigDecimal totalOfDiscountedPrices = 
  .filter(price -> price.compareTo(BigDecimal.valueOf(20)) > 0)
  .map(price -> price.multiply(BigDecimal.valueOf(0.9)))
  .reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Total of discounted prices: " + totalOfDiscountedPrices);

Instead of explicitly iterating through the prices list, we’re using a few special methods:

  • Using filter() to get the record we are interested
  • Using map() to transfer to another result set
  • Using reduce() to compute the total on the result


It’s a whole new world in Java. We can now program in an elegant and fluent functional style, with higher-order functions. This can lead to concise code that has fewer errors and is easier to understand, maintain, and parallelize.

Asgard - Handy Tool for AWS Management & Deployment

| Comments

Asgard is named for the home of the Norse god of thunder and lightning. As the words described, it is closely relevant to the management and control in cloud.

Netflix build a tool named Asgard, which is used to control and manage their AWS cloud. In 2012, Netflix announced that Asgard was open-sourced.

Then…… it is time to start the story.

What is Asgard

Asgard is a web-based tool for managing cloud-based applications and infrastructure.

  • Built on the top of grails.
  • By introducing Application and Cluster terminology, enable users understand their cloud objets clearly.
  • Keep track cloud objects from the view of one application

Splunk - Receive Logs From Distributed Windows Machine

| Comments

Why do I need Splunk

Recently, our team is moving several legacy components running in Windows server from data-center to AWS. The transformation itself is not hard, but the eco-system like monitoring, alerting, troubleshooting is most important before getting started the transofrmation.

The article would talk about how to setup and config Splunk Universal Forwarder in Windows machine where the component is running.

Bying using SUF, Splunk central server can easily collect logs from different distributed machines, so that the OPS guy can query and analyze the logs from one portal rather than login on different distributed machines.

How to Setup Mongo Shard

| Comments

To simulate the process of production deployment of MongoDB, I used Vagrant to create a couples of VMs, and exprienced the journey of deployment mongo step by step as follow mentioned.

Mongo Shard Concepts

Before dived into the code, we can review the concepts related to Mongo Shard, it needs 3 components logically at least.

a) Config Server

The config server processes are mongod instances that store the cluster’s metadata. You designate a mongod as a config server using the –configsvr option. Each config server stores a complete copy of the cluster’s metadata.

b) Query Server

The query server are lightweight mongos instances and do not require data directories. You can run a mongos instance on a system that runs other cluster components, such as on an application server or a server running a mongod process. By default, a mongos instance runs on port 27017.

How to Setup Mongo repliSet

| Comments

Create one mongo repliSet

mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2

Run instances

mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128

Enable replicaSet

mongo --port 27017


| Comments





WebRTC - Understand the PeerConnection

| Comments



还记得之前讨论过的WebRTC架构图吗? 实际上,虽然RTCPeerConnection是一个暴露的连接接口, 但其内部封装了大量WebRTC编解码和协议处理的工作,因此使浏览器之间点到点的即时通讯变得简单。

WebRTC - Understand the MediaStream

| Comments


  • 1)获取本地媒体流(音频、视频)。
  • 2)与对端建立连接(包括防火墙和NAT的穿透)。
  • 3)初始化双方通信信道。
  • 4)交换通信元数据信息,比如分辨率、编解码方案、网络地址等。
  • 5)开始音频,视频或数据的通信。

WebRTC - the Revolution of Media Communication

| Comments


如今,互联网主流的音频、视频通信服务产品,如Skype, QQ, GTalk等, 都是各自厂商私有的技术。用户需要安装插件或者桌面客户端,才能使用其提供的音频或者视频通信功能。 如果某天,不使用任何插件,只通过浏览器,就能实现手机、平板、电视和电脑的视频或者音频通信;那会是什么场景? 而这正是WebRTC的愿景。

WebRTC(Web Real-Time Communication)由一套开放的标准、协议和一组JavaScript API构成。2010年,谷歌收购了VoIP软件开发商GIPS(Global IP Solutions),并获得了视频采集、编解码、网络传输、回音消除等RTC相关技术。2011年6月,谷歌开放了该部分源码,并积极推动该项技术,这奠定了RTC在Web领域发展的基础。



Page Object Understanding

| Comments

什么是Page Object

Page Object是Selenium中提出的一种测试设计模式。在Web前端自动化测试的过程中,Page Object可以称得上是居家必备的良品之一。PageObject将与Web测试页面交互的行为封装在其内部,旨在将每个页面或者相似页面的功能封装,例如页面中需要测试的元素(按钮,输入框,标题等),这样,通过在测试中访问Page类的相应方法来获取页面相应的元素,从而巧妙的避免了当页面元素id或者位置变化时,需要修改测试代码的情况。