com.kolibrifx.plovercrest.server.internal.local.LocalTableWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plovercrest-server Show documentation
Show all versions of plovercrest-server Show documentation
Plovercrest server library.
The newest version!
/*
* Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
*/
package com.kolibrifx.plovercrest.server.internal.local;
import java.nio.ByteBuffer;
import org.apache.log4j.Logger;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.client.TableSerializer;
import com.kolibrifx.plovercrest.client.TableWriter;
import com.kolibrifx.plovercrest.client.internal.ErrorListenerSubject;
import com.kolibrifx.plovercrest.server.streams.StreamWriter;
public class LocalTableWriter extends TableWriter {
private static final Logger log = Logger.getLogger(LocalTableWriter.class);
private final StreamWriter tableWriter;
private final ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
public LocalTableWriter(final StreamWriter tableWriter,
final TableSerializer serializer,
final ErrorListenerSubject errorListenerSubject) {
super(serializer, errorListenerSubject);
this.tableWriter = tableWriter;
}
@Override
public void write(final T m) {
try {
buffer.clear();
serializer.serialize(buffer, m);
buffer.flip();
tableWriter.write(serializer.getTimestampForMessage(m), buffer);
} catch (final PlovercrestException e) {
log.error(e.getMessage(), e);
errorListenerSubject.onWriteError(e, m);
// not rethrowing exceptions; we want to be compatible with the remote interface,
// and attempt to write subsequent writes.
// TODO: this is bad for connect errors, find a better strategy there...?
}
}
@Override
public void freeze() {
tableWriter.freeze();
}
@Override
public boolean isFrozen() {
return tableWriter.isFrozen();
}
@Override
public void unfreeze() {
tableWriter.unfreeze();
}
@Override
public void flush() {
tableWriter.flush();
}
}