Skip to content

Instantly share code, notes, and snippets.

@debop
debop / MongoGridDatastoreConfigBase.java
Created April 30, 2013 05:07
ThreadLocal 을 이용한 Thread별 Bean
private static ThreadLocal<MongoOgmDao> MongoOgmDaoLocal =
new ThreadLocal<MongoOgmDao>() {
@Override
public MongoOgmDao initialValue() {
log.debug("현 ThreadContext 에 저장소를 생성합니다...");
return new MongoOgmDao();
}
};
@Scope("prototype")
public List<String> readAllLines(byte[] bytes, Charset charset) {
if (bytes == null || bytes.length == 0)
return new ArrayList<String>();
try (ByteArrayInputStream is = new ByteArrayInputStream(bytes);
Reader in = new InputStreamReader(is, charset);
BufferedReader br = new BufferedReader(in)) {
List<String> lines = new ArrayList<String>();
while (true) {
try {
fc = AsynchronousFileChannel.open(path,
StandardOpenOption.READ,
StandardOpenOption.DELETE_ON_CLOSE);
ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
Future<Integer> result = fc.read(buffer, 0);
while (!result.isDone()) {
System.out.println("Do something else while reading...");
AsynchronousFileChannel fc = null;
try {
fc = AsynchronousFileChannel.open(path,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
ByteBuffer buffer = ByteBuffer.wrap(StringTool.replicate(TEST_TEXT, 1000).getBytes(UTF8));
Future<Integer> result = fc.write(buffer, 0);
while (!result.isDone()) {
System.out.println("Do something else while writing...");
@debop
debop / FileChannelTest.java
Created April 29, 2013 02:45
bufferedStream
@Test
public void bufferedStreams() throws Exception {
Path path = Paths.get("channel.txt");
try (BufferedWriter writer = Files.newBufferedWriter(path, UTF8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
for (int i = 0; i < 100; i++) {
writer.write(TEST_TEXT);
}
writer.flush();
}
NettyTransceiver calcClient = new NettyTransceiver(new InetSocketAddress(CALC_SERVER_PORT));
Calculator.Callback proxy = (Calculator.Callback) SpecificRequestor.getClient(Calculator.Callback.class, calcClient);
System.out.println("Client built, get proxy");
proxy.add(2, 3, new Callback<Double>() {
@Override
public void handleResult(Double result) {
System.out.println("Callback... add(2, 3)=" + result);
}
@debop
debop / CalculatorServer.java
Created April 18, 2013 09:02
Async Calculator
package example.avro.rpc;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.AvroRemoteException;
import org.apache.avro.ipc.*;
import org.apache.avro.ipc.specific.SpecificRequestor;
import org.apache.avro.ipc.specific.SpecificResponder;
import java.io.IOException;
import java.net.InetSocketAddress;
@debop
debop / searchService.avdl
Created April 18, 2013 07:44
searchService.avdl
@namespace("example.avro.search")
protocol SearchService {
record Entity {
string rowId;
string @order("ascending") createdAt;
string text;
map<string> attrs;
}
@debop
debop / SearchServer.java
Created April 18, 2013 07:39
SearchServer
package example.avro.search;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.AvroRemoteException;
import org.apache.avro.ipc.NettyServer;
import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.Server;
import org.apache.avro.ipc.specific.SpecificRequestor;
@debop
debop / pom.xml
Created April 17, 2013 12:57
avro-maven-plugin
<build>
<plugins>
<!-- Avro Plugin -->
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>${avro.version}</version>
<executions>
<execution>
<id>schemas</id>