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

1
2
3
4
5
6
7
8
9
10
11
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.

1
2
3
4
5
6
7
final BigDecimal totalOfDiscountedPrices = 
prices.stream()
  .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

Summary

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
rs.initiate()
rs.conf()
rs.status()

Dora诞生记

| Comments

趁着在墨尔本和西安Office上Hackday获奖的喜悦劲,赶紧来一篇Dora诞生记。
提醒偶记住这个特殊的日子,同时也感谢在过去两个月,牺牲个人时间,奋斗在Dora(X-Robot)上的Casa的兄弟姐妹们。

-2013年10月20

和Charley午餐的时候,无意间谈论起了各自想做但没时间做的idea。

幸运乎,找到一个我俩都认为不错但又没被完全实现的一个想法<<公交来了>>(旨在帮助等车族们准确的掌握公交将来的时间,为既不想迟到又想多赖几分钟在床上的兄弟提供福音)。

WebRTC - Understand the PeerConnection

| Comments

在上篇文章里,我们已经了解了MediaStream。那么这篇文章里,我们就来重点了解RTCPeerConnection。

RTCPeerConnection表示浏览器之间点对点的连接。只有当连接建立后,浏览器的两端才能真正完成流媒体数据的传输。

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

WebRTC - Understand the MediaStream

| Comments

WebRTC从设计之初,目的就是为开发人员提供更加简便的方式构建基于Web的视频、音频应用。对于任何的WebRTC应用程序,只需要做如下几件事情:

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

WebRTC - the Revolution of Media Communication

| Comments

什么是WebRTC

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

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

WebRTC使浏览器之间能够通过点对点的通信方式,直接完成视频、音频以及数据的实时传输。同时,WebRTC容许开发人员通过调用浏览器支持的API,使用HTML5标签和JavaScript轻松的实现基于Web的音频、视频应用。

目前,WebRTC的标准还在不断完善中,W3C、IETF和其他一些浏览器厂商正在积极的推动WebRTC的发展。WebRTC开创了浏览器之间能够直接通信并完成视频或者音频传输的新时代。对于构建开放的、标准的,无插件,免费的视频音频通信应用,迈出了划时代的一步。

Page Object Understanding

| Comments

什么是Page Object

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

言而总之,总而言之,PageObject就是将Web页面元素的变化封装起来,提供API供外部测试代码调用,从而达到将测试代码于Web页面元素的变化解耦。