Skip to content

Instantly share code, notes, and snippets.

@aleung
aleung / ApplicationContextDumper.java
Created November 8, 2011 06:41
Dump beans information in a Springframework application context. Licensed under WTFPL v2. More detail in blog post: http://aleung.github.io/blog/2011/11/08/Dump-beans-information-from-live-Spring-application-context/
package leoliang.springtest;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractRefreshableApplicationContext;
@aleung
aleung / HttpServer.java
Created November 8, 2011 13:47
Tiny HTTP Server by Java, only supports GET method.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
@aleung
aleung / applicationContext.xml
Created June 15, 2012 04:00
Spring bean reference injection definition by external configuration.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType">
<context:property-placeholder location="file:bin/leoliang/spring/test1/bean.properties"/>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>localrepodeployer</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
@aleung
aleung / clean_aged_artifacts.rb
Last active April 24, 2017 18:07
Clean up unused (long time no download) artifacts from Artifactory repository. See: http://aleung.github.com/blog/2013/03/22/clean-aged-artifacts-from-artifactory/
#!/bin/env ruby
# --- Configuration ---------------------------------
# Remove artifacts which were created before $age_days ago and haven't been downloaded in recent $age_days.
$age_days = 730
# The repository to be cleaned.
$repo = 'repository-key'
@aleung
aleung / remove_conflict_non_unique_snapshots.rb
Created March 28, 2013 04:05
I have Maven (Artifactory) repository which was configured to store non-unique snapshots. When the configure was changed to store unique (with time-stamp) snapshots, if a new snapshot is deployed, there will be both unique snapshot and non-unique snapshot artifact of same version exists. If it happens, the snapshot artifact will be unable to dow…
#!/bin/env ruby
# --- Configuration ---------------------------------
$age_days = 3
# The repository to be cleaned.
$repo = 'repo-name'
$user = 'repo-admin'
@aleung
aleung / Scheduler
Created May 27, 2013 10:48
A simple Java scheduler, which execute scheduled tasks in current thread.
package leoliang.common;
import com.google.common.base.Preconditions;
/**
* Usage example: Run 15 times in every 250ms, begins at 5 seconds later.
*
* <pre>
Scheduler.every(250, Scheduler.TimeUnit.MILLISECOND)
.beginsAt(System.currentTimeMillis() + 5000)
@aleung
aleung / ActiveCountMetric.java
Created November 26, 2013 09:12
Invocation metric bases on codahale (Yammer) Metrics. 这是在Messaging性能测试时使用的metrics,记录了代码块调用的throughput(TPS,latency)和active count(并发数),数据每分钟写入csv格式文件。 用try + finally的方式对调用进行拦截检测,代码有侵入性。当时是测试使用,这样写起来最快。
public interface ActiveCountMetric {
void inc();
void dec();
}
@aleung
aleung / ipaddr.es
Last active November 5, 2015 03:43
Get local IP address of specific NIC in node.js
var _ = require('lodash');
var os = require('os');
function getIpAddr(nicName) {
let ifaces = os.networkInterfaces();
let iface = _.find(ifaces[nicName], iface => { return iface.family === 'IPv4' });
return _.result(iface, 'address');
}
module.exports = _.memoize(getIpAddr);
@aleung
aleung / mergelog.js
Created November 2, 2016 11:30
Merge consul agent logs which are fetched by Ansible
#!/usr/bin/env node
"use strict";
const fs = require('fs');
const readline = require('readline');
const logLineRegexp = /\s+(.+?)\[(.+?)\](.*)/;
function processFile(file, host) {