Search in sources :

Example 1 with Buffer

use of org.fusesource.hawtbuf.Buffer in project camel by apache.

the class LevelDBAggregationRepository method get.

public Exchange get(final CamelContext camelContext, final String key) {
    Exchange answer = null;
    try {
        byte[] lDbKey = keyBuilder(repositoryName, key);
        LOG.trace("Getting key index {}", key);
        byte[] rc = levelDBFile.getDb().get(lDbKey);
        if (rc != null) {
            answer = codec.unmarshallExchange(camelContext, new Buffer(rc));
        }
    } catch (IOException e) {
        throw new RuntimeException("Error getting key " + key + " from repository " + repositoryName, e);
    }
    LOG.debug("Getting key  [{}] -> {}", key, answer);
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) Buffer(org.fusesource.hawtbuf.Buffer) IOException(java.io.IOException)

Example 2 with Buffer

use of org.fusesource.hawtbuf.Buffer in project camel by apache.

the class LevelDBAggregationRepository method add.

public Exchange add(final CamelContext camelContext, final String key, final Exchange exchange) {
    LOG.debug("Adding key [{}] -> {}", key, exchange);
    try {
        byte[] lDbKey = keyBuilder(repositoryName, key);
        final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
        byte[] rc = null;
        if (isReturnOldExchange()) {
            rc = levelDBFile.getDb().get(lDbKey);
        }
        LOG.trace("Adding key index {} for repository {}", key, repositoryName);
        levelDBFile.getDb().put(lDbKey, exchangeBuffer.toByteArray(), levelDBFile.getWriteOptions());
        LOG.trace("Added key index {}", key);
        if (rc == null) {
            return null;
        }
        // only return old exchange if enabled
        if (isReturnOldExchange()) {
            return codec.unmarshallExchange(camelContext, new Buffer(rc));
        }
    } catch (IOException e) {
        throw new RuntimeException("Error adding to repository " + repositoryName + " with key " + key, e);
    }
    return null;
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) IOException(java.io.IOException)

Example 3 with Buffer

use of org.fusesource.hawtbuf.Buffer in project nhin-d by DirectProject.

the class ConcurrentJPAAggregationRepository method recover.

/**
	 * {@inheritDoc}
	 * This specific implementation locks the recovered exchange for a period of time specified by the DAO.
	 * If the exchange is locked by the DAO, then null is returned.
	 */
@Override
public Exchange recover(CamelContext camelContext, String exchangeId) {
    Exchange retVal = null;
    try {
        // recover the exchnage from the repository
        final AggregationCompleted agg = dao.getAggregationCompleted(exchangeId, true);
        // not found or is locked... return null
        if (agg == null)
            return null;
        // deserialize exchange 
        retVal = codec.unmarshallExchange(camelContext, new Buffer(agg.getExchangeBlob()));
        // set the version number of the exchange
        retVal.setProperty(AGGREGATION_COMPLETE_ENTITY_VERSON, agg.getVersion());
    } catch (Exception e) {
        // wrap exception in a runtime exception
        throw new RuntimeException("Error recovering exchange from repository with exchangeId " + exchangeId, e);
    }
    return retVal;
}
Also used : Exchange(org.apache.camel.Exchange) Buffer(org.fusesource.hawtbuf.Buffer) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted)

Example 4 with Buffer

use of org.fusesource.hawtbuf.Buffer in project nhin-d by DirectProject.

the class ConcurrentJPAAggregationRepository method get.

/**
	 * {@inheritDoc}
	 */
@Override
public Exchange get(CamelContext camelContext, String key) {
    Exchange retVal = null;
    try {
        // get the aggregation
        final Aggregation agg = dao.getAggregation(key);
        if (agg == null)
            return null;
        // deserialized to an exchange object
        retVal = codec.unmarshallExchange(camelContext, new Buffer(agg.getExchangeBlob()));
        // set the version of the exchange for later consistency checking
        retVal.setProperty(AGGREGATION_ENTITY_VERSON, agg.getVersion());
    } catch (Exception e) {
        // wrap exception in a runtime exception
        throw new RuntimeException("Error retrieving from repository aggregation with key " + key, e);
    }
    return retVal;
}
Also used : Exchange(org.apache.camel.Exchange) Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) Buffer(org.fusesource.hawtbuf.Buffer)

Example 5 with Buffer

use of org.fusesource.hawtbuf.Buffer 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);
                }
            });
        }
    });
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) Exchange(org.apache.camel.Exchange) Listener(org.fusesource.mqtt.client.Listener) Callback(org.fusesource.mqtt.client.Callback) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer)

Aggregations

Buffer (org.fusesource.hawtbuf.Buffer)45 IOException (java.io.IOException)17 ByteBuffer (java.nio.ByteBuffer)10 Exchange (org.apache.camel.Exchange)9 Test (org.junit.Test)9 Transaction (org.fusesource.hawtdb.api.Transaction)8 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)6 SortedIndex (org.fusesource.hawtdb.api.SortedIndex)6 InflaterInputStream (java.util.zip.InflaterInputStream)5 ByteArrayInputStream (org.fusesource.hawtbuf.ByteArrayInputStream)5 BufferState (io.fabric8.dosgi.io.ProtocolCodec.BufferState)4 OpenwireException (io.fabric8.gateway.handlers.detecting.protocol.openwire.support.OpenwireException)3 DataStructure (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.DataStructure)2 RandomAccessFile (java.io.RandomAccessFile)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 BufferEditor (org.fusesource.hawtbuf.BufferEditor)2 ByteArrayOutputStream (org.fusesource.hawtbuf.ByteArrayOutputStream)2 DataByteArrayOutputStream (org.fusesource.hawtbuf.DataByteArrayOutputStream)2 Index (org.fusesource.hawtdb.api.Index)2 ObjectSerializationStrategy (io.fabric8.dosgi.api.ObjectSerializationStrategy)1