use of org.fusesource.hawtbuf.UTF8Buffer in project camel by apache.
the class MQTTEndpoint method createConnection.
protected void createConnection() {
connection = configuration.callbackConnection();
connection.listener(new Listener() {
public void onConnected() {
connected = true;
LOG.info("MQTT Connection connected to {}", configuration.getHost());
}
public void onDisconnected() {
// no connected = false required here because the MQTT client should trigger its own reconnect;
// setting connected = false would make the publish() method to launch a new connection while the original
// one is still reconnecting, likely leading to duplicate messages as observed in CAMEL-9092;
// if retries are exhausted and it desists, we should get a callback on onFailure, and then we can set
// connected = false safely
LOG.debug("MQTT Connection disconnected from {}", configuration.getHost());
}
public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack) {
if (!consumers.isEmpty()) {
Exchange exchange = createExchange();
exchange.getIn().setBody(body.toByteArray());
exchange.getIn().setHeader(MQTTConfiguration.MQTT_SUBSCRIBE_TOPIC, topic.toString());
for (MQTTConsumer consumer : consumers) {
consumer.processExchange(exchange);
}
}
if (ack != null) {
ack.run();
}
}
public void onFailure(Throwable value) {
// mark this connection as disconnected so we force re-connect
connected = false;
LOG.warn("Connection to " + configuration.getHost() + " failure due " + value.getMessage() + ". Forcing a disconnect to re-connect on next attempt.");
connection.disconnect(new Callback<Void>() {
public void onSuccess(Void value) {
}
public void onFailure(Throwable e) {
LOG.debug("Failed to disconnect from " + configuration.getHost() + ". This exception is ignored.", e);
}
});
}
});
}
use of org.fusesource.hawtbuf.UTF8Buffer in project fabric8 by jboss-fuse.
the class ClientInvokerImpl method getMethodData.
private MethodData getMethodData(Method method) throws IOException {
MethodData rc = null;
synchronized (method_cache) {
rc = method_cache.get(method);
}
if (rc == null) {
StringBuilder sb = new StringBuilder();
sb.append(method.getName());
sb.append(",");
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (i != 0) {
sb.append(",");
}
sb.append(encodeClassName(types[i]));
}
Buffer signature = new UTF8Buffer(sb.toString()).buffer();
Serialization annotation = method.getAnnotation(Serialization.class);
SerializationStrategy serializationStrategy;
if (annotation != null) {
serializationStrategy = serializationStrategies.get(annotation.value());
if (serializationStrategy == null) {
throw new RuntimeException("Could not find the serialization strategy named: " + annotation.value());
}
} else {
serializationStrategy = ObjectSerializationStrategy.INSTANCE;
}
final InvocationStrategy strategy;
if (AsyncInvocationStrategy.isAsyncMethod(method)) {
strategy = AsyncInvocationStrategy.INSTANCE;
} else {
strategy = BlockingInvocationStrategy.INSTANCE;
}
rc = new MethodData(strategy, serializationStrategy, signature);
synchronized (method_cache) {
method_cache.put(method, rc);
}
}
return rc;
}
use of org.fusesource.hawtbuf.UTF8Buffer in project fabric8 by jboss-fuse.
the class ClientInvokerImpl method request.
protected Object request(ProxyInvocationHandler handler, final String address, final UTF8Buffer service, final ClassLoader classLoader, final Method method, final Object[] args) throws Exception {
if (!running.get()) {
throw new IllegalStateException("DOSGi Client stopped");
}
final long correlation = correlationGenerator.incrementAndGet();
// Encode the request before we try to pass it onto
// IO layers so that #1 we can report encoding error back to the caller
// and #2 reduce CPU load done in the execution queue since it's
// serially executed.
DataByteArrayOutputStream baos = new DataByteArrayOutputStream((int) (handler.lastRequestSize * 1.10));
// we don't know the size yet...
baos.writeInt(0);
baos.writeVarLong(correlation);
writeBuffer(baos, service);
MethodData methodData = getMethodData(method);
writeBuffer(baos, methodData.signature);
final ResponseFuture future = methodData.invocationStrategy.request(methodData.serializationStrategy, classLoader, method, args, baos);
// toBuffer() is better than toByteArray() since it avoids an
// array copy.
final Buffer command = baos.toBuffer();
// Update the field size.
BufferEditor editor = command.buffer().bigEndianEditor();
editor.writeInt(command.length);
handler.lastRequestSize = command.length;
queue().execute(new Runnable() {
public void run() {
try {
TransportPool pool = transports.get(address);
if (pool == null) {
pool = new InvokerTransportPool(address, queue());
transports.put(address, pool);
pool.start();
}
requests.put(correlation, future);
pool.offer(command, correlation);
} catch (Exception e) {
LOGGER.info("Error while sending request", e);
future.fail(e);
}
}
});
// TODO: make that configurable, that's only for tests
return future.get(timeout, TimeUnit.MILLISECONDS);
}
use of org.fusesource.hawtbuf.UTF8Buffer in project fabric8 by jboss-fuse.
the class BaseDataStreamMarshaller method looseMarshalThrowable.
protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, DataByteArrayOutputStream dataOut) throws IOException {
dataOut.writeBoolean(o != null);
if (o != null) {
String className = o instanceof OpenwireException ? ((OpenwireException) o).getClassName() : o.getClass().getName();
looseMarshalString(new UTF8Buffer(className), dataOut);
looseMarshalString(new UTF8Buffer(o.getMessage()), dataOut);
if (wireFormat.isStackTraceEnabled()) {
StackTraceElement[] stackTrace = o.getStackTrace();
dataOut.writeShort(stackTrace.length);
for (int i = 0; i < stackTrace.length; i++) {
StackTraceElement element = stackTrace[i];
looseMarshalString(new UTF8Buffer(element.getClassName()), dataOut);
looseMarshalString(new UTF8Buffer(element.getMethodName()), dataOut);
looseMarshalString(new UTF8Buffer(element.getFileName()), dataOut);
dataOut.writeInt(element.getLineNumber());
}
looseMarshalThrowable(wireFormat, o.getCause(), dataOut);
}
}
}
use of org.fusesource.hawtbuf.UTF8Buffer in project fabric8 by jboss-fuse.
the class BaseDataStreamMarshaller method tightUnmarsalThrowable.
protected Throwable tightUnmarsalThrowable(OpenWireFormat wireFormat, DataByteArrayInputStream dataIn, BooleanStream bs) throws IOException {
if (bs.readBoolean()) {
UTF8Buffer clazz = tightUnmarshalString(dataIn, bs);
UTF8Buffer message = tightUnmarshalString(dataIn, bs);
Throwable o = createThrowable(clazz, message);
if (wireFormat.isStackTraceEnabled()) {
if (STACK_TRACE_ELEMENT_CONSTRUCTOR != null) {
StackTraceElement[] ss = new StackTraceElement[dataIn.readShort()];
for (int i = 0; i < ss.length; i++) {
try {
ss[i] = (StackTraceElement) STACK_TRACE_ELEMENT_CONSTRUCTOR.newInstance(new Object[] { tightUnmarshalString(dataIn, bs), tightUnmarshalString(dataIn, bs), tightUnmarshalString(dataIn, bs), Integer.valueOf(dataIn.readInt()) });
} catch (IOException e) {
throw e;
} catch (Throwable e) {
}
}
o.setStackTrace(ss);
} else {
short size = dataIn.readShort();
for (int i = 0; i < size; i++) {
tightUnmarshalString(dataIn, bs);
tightUnmarshalString(dataIn, bs);
tightUnmarshalString(dataIn, bs);
dataIn.readInt();
}
}
o.initCause(tightUnmarsalThrowable(wireFormat, dataIn, bs));
}
return o;
} else {
return null;
}
}
Aggregations