[ java ] URLConnection

URL url = new URL(abTestSettings.getUrl());
URLConnection conn = url.openConnection();
conn.setConnectTimeout(100);
conn.setReadTimeout(500);
conn.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conn.getOutputStream());
outputStreamWriter .write("param1=value1&param2=value2");
outputStreamWriter .flush();

// response

String contentLength = conn.getHeaderField("Content-Length");
int responseSize = 0;
if (contentLength != null){
try{
responseSize = Integer.parseInt(contentLength);
}catch (NumberFormatException nfe){
}
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
\\ line
}

outputStreamWriter .close();

bufferedReader.close();

URLEncoder

URLEncoder.encode(str, "UTF-8")

[ java ] Thread

Runnable

public class HelloRunnable implements Runnable {

public void run() {
System.out.println("Hello from a thread!");
}

public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}

}

ref : https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

javax.crypto.Mac

Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
 SecretKeySpec secretKeySpec = new SecretKeySpec(sharedSecret.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secretKeySpec);
byte[] calculatedSignatureBytes = sha256_HMAC.doFinal(signedString.getBytes("UTF-8"));

org.apache.wink.client

Resource resource =
client
.resource(
"http://" + HOST_NAME + ":" + PORT + "/nodecollector/" + hypervisorIP + "/"
+ HOST_RESOURCE);

resource.header(HttpHeaders.CONTENT_TYPE, 'application/json');

resource.queryParam(HYPERVISOR_KEY, hypervisorType.getValue())
.queryParam(USER_KEY, user).queryParam(PASSWORD_KEY, password);

ClientResponse response = resource.accept(MediaType.APPLICATION_XML_TYPE).get();
HostDto host = response.getEntity(HostDto.class);

ref : http://www.programcreek.com/java-api-examples/index.php?api=org.apache.wink.client.ClientResponse

net.spy.memcached

MemcachedClient c = new MemcachedClient(
new InetSocketAddress("hostname", portNum));

// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value.
Object myObject = c.get("someKey");

lock memcache with cas
The cas() operation internally adds the timestamp to the request it sends to the memcache service. The service then compares the timestamp received with a cas() operation to the timestamp currently associated with the key. If they match, it updates the value and the timestamp, and returns success. If they don’t match, it leaves the value and timestamp alone, and returns failure. (By the way, it does not send the new timestamp back with a successful response. The only way to retrieve the timestamp is to call gets().)
ref : http://neopythonic.blogspot.com/2011/08/compare-and-set-in-memcache.html

// obtain CAS token value using gets method
CASValue casValue = mcc.gets("tutorialspoint");

// display CAS token value
System.out.println("CAS token - " + casValue);

// try to update data using memcached cas method
CASResponse casresp = mcc.cas("tutorialspoint", casValue.getCas(), 900, "Largest Tutorials-Library");

while (!CASResponse.OK.equals(casresp))
{

// retry

}

ref : http://www.tutorialspoint.com/memcached/memcached_cas.htm