Skip to content

Instantly share code, notes, and snippets.

View fehmicansaglam's full-sized avatar
🚶‍♂️
Wandering

Fehmi Can Sağlam fehmicansaglam

🚶‍♂️
Wandering
View GitHub Profile
@fehmicansaglam
fehmicansaglam / Application.java
Created February 9, 2012 18:50
Accept range header and write a partial content to the response with Play! Framework.
public static void downloadFile(final Long fileId) throws IOException {
response.setHeader("Accept-Ranges", "bytes");
notFoundIfNull(fileId);
File underlyingFile = ... //load file
String fileName = ...//name of the file
Header rangeHeader = request.headers.get("range");
if (rangeHeader != null) {
throw new PartialContent(underlyingFile, fileName);
@fehmicansaglam
fehmicansaglam / hanoi.html
Created February 13, 2012 10:59
jQuery HanoiTowers Solver
<html>
<head>
<title>Hanoi</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style type="text/css">
#hanoi-table {
width: 345px;
height: 18px;
background-color: #eee;
border: 4px solid #ccc;
@fehmicansaglam
fehmicansaglam / Application.java
Created February 28, 2012 09:22
Cache Manager for play.db.jpa.Model static methods
public static void stats() {
Long userCount = CacheManager.get(CacheKey.USER_COUNT);
Long roleCount = CacheManager.get(CacheKey.ROLE_COUNT);
Long eventCount = CacheManager.get(CacheKey.EVENT_COUNT);
Long cumleCount = CacheManager.get(CacheKey.CUMLE_COUNT);
Long cumleEnabledCount = CacheManager.get(CacheKey.CUMLE_ENABLED_COUNT);
render(userCount, roleCount, eventCount, cumleCount, cumleEnabledCount);
}
@fehmicansaglam
fehmicansaglam / HazelcastWithJavaClientTest.java
Created March 18, 2012 09:30
Memcached, Redis ve Hazelcast performans karşılaştırması
package concurrency;
import com.hazelcast.client.ClientConfig;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Test;
import workers.HazelcastWorker;
@fehmicansaglam
fehmicansaglam / Application.java
Created March 23, 2012 18:53
Play! Framework ile Asenkron HTTP
public class Application extends Controller {
public static void index() throws Throwable {
Either<User, Throwable> result = await(Client.createUser("testuser",
"Foo",
"Bar"));
if (result._1.isDefined()) {
result = await(Client.getUser("testuser"));
if (result._1.isDefined()) {
@fehmicansaglam
fehmicansaglam / (Akka)Producer.java
Created April 3, 2012 14:44
Akka ya da Java ExecutorService ve Hazelcast ile kullanıcı bildirim kuyruğu
public class Producer extends UntypedActor {
private static final Gson gson = new GsonBuilder().create();
private final String[] keys = {"user1", "user2", "user3", "user4", "user5", "user6"};
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof Integer) {
Integer opCount = (Integer) message;
final Random random = new Random();
@fehmicansaglam
fehmicansaglam / SetsTest.java
Created April 30, 2012 07:12
Test unsychronized Set implementations
private static final int COUNT = 1000000;
private static void testAdd(Set<String> set) {
final long start = System.nanoTime();
for (int i = 0; i < COUNT; ++i) {
set.add("test" + Math.random());
}
final long elapsed = System.nanoTime() - start;
System.out.println("testAdd: " + elapsed / 1000);
}
@fehmicansaglam
fehmicansaglam / twitter-graph.js
Created May 2, 2012 19:45
Twitter takipçi grafı
var createNode = function(data){
return $.ajax({
type: 'POST',
url: 'http://localhost:7474/db/data/index/node/people?unique',
data: data,
contentType: 'application/json',
dataType: 'json'
});
}
@fehmicansaglam
fehmicansaglam / JsonFormat.java
Created May 8, 2012 14:12
Pretty-Print JSON in Java
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);
@fehmicansaglam
fehmicansaglam / ApiClient.java
Created June 2, 2012 12:48
AsyncStreamHandler
public static ListenableFuture<Response> readFile(final String documentUuid,
final OutputStream os) {
final String requestUrl = ...;
try {
return new AsyncHttpClient().prepareGet(requestUrl)
.addQueryParameter("documentUuid", documentUuid)
.execute(new AsyncStreamHandler(os, documentUuid));
} catch (final IOException e) {
throw new UnexpectedException(e);