Search in sources :

Example 6 with Buffer

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

the class HawtDBAggregationRepository method confirm.

public void confirm(final CamelContext camelContext, final String exchangeId) {
    LOG.debug("Confirming exchangeId [{}]", exchangeId);
    try {
        final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
        hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> indexCompleted = hawtDBFile.getRepositoryIndex(tx, getRepositoryNameCompleted(), true);
                Buffer buffer = indexCompleted.remove(confirmKeyBuffer);
                LOG.trace("Removed confirm index {} -> {}", confirmKeyBuffer, buffer);
                return buffer;
            }

            @Override
            public String toString() {
                return "Confirming exchangeId [" + exchangeId + "]";
            }
        });
    } catch (IOException e) {
        throw new RuntimeException("Error confirming exchangeId " + exchangeId + " from repository " + repositoryName, e);
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) IOException(java.io.IOException)

Example 7 with Buffer

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

the class HawtDBAggregationRepository method recover.

public Exchange recover(CamelContext camelContext, final String exchangeId) {
    Exchange answer = null;
    try {
        final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
        Buffer rc = hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> indexCompleted = hawtDBFile.getRepositoryIndex(tx, getRepositoryNameCompleted(), false);
                if (indexCompleted == null) {
                    return null;
                }
                return indexCompleted.get(confirmKeyBuffer);
            }

            @Override
            public String toString() {
                return "Recovering exchangeId [" + exchangeId + "]";
            }
        });
        if (rc != null) {
            answer = codec.unmarshallExchange(camelContext, rc);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error recovering exchangeId " + exchangeId + " from repository " + repositoryName, e);
    }
    LOG.debug("Recovering exchangeId [{}] -> {}", exchangeId, answer);
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) IOException(java.io.IOException)

Example 8 with Buffer

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

the class HawtDBAggregationRepository method doAdd.

protected Exchange doAdd(final CamelContext camelContext, final String key, final Exchange exchange, final boolean handleOptimisticLockingException) {
    LOG.debug("Adding key [{}] -> {}", key, exchange);
    try {
        // If we could guarantee that the key and exchange are immutable,
        // then we could have stuck them directly into the index, 
        // HawtDB could then eliminate the need to marshal and un-marshal  
        // in some cases.  But since we can't.. we are going to force
        // early marshaling.
        final Buffer keyBuffer = codec.marshallKey(key);
        final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
        Buffer rc = hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, repositoryName, true);
                Buffer buffer = index.put(keyBuffer, exchangeBuffer);
                LOG.trace("Added key index {}", keyBuffer);
                return buffer;
            }

            @Override
            public String toString() {
                return "Adding key [" + key + "]";
            }
        }, handleOptimisticLockingException);
        if (rc == null) {
            return null;
        }
        // only return old exchange if enabled
        if (isReturnOldExchange()) {
            return codec.unmarshallExchange(camelContext, 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) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) IOException(java.io.IOException)

Example 9 with Buffer

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

the class HawtDBAggregateNotLostRemovedWhenConfirmedTest method testHawtDBAggregateNotLostRemovedWhenConfirmed.

@Test
public void testHawtDBAggregateNotLostRemovedWhenConfirmed() throws Exception {
    getMockEndpoint("mock:result").expectedBodiesReceived("ABCDE");
    template.sendBodyAndHeader("direct:start", "A", "id", 123);
    template.sendBodyAndHeader("direct:start", "B", "id", 123);
    template.sendBodyAndHeader("direct:start", "C", "id", 123);
    template.sendBodyAndHeader("direct:start", "D", "id", 123);
    template.sendBodyAndHeader("direct:start", "E", "id", 123);
    assertMockEndpointsSatisfied(30, TimeUnit.SECONDS);
    Thread.sleep(1000);
    String exchangeId = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getExchangeId();
    // the exchange should NOT be in the completed repo as it was confirmed
    final HawtDBFile hawtDBFile = repo.getHawtDBFile();
    final HawtDBCamelCodec codec = new HawtDBCamelCodec();
    final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
    Buffer bf = hawtDBFile.execute(new Work<Buffer>() {

        public Buffer execute(Transaction tx) {
            Index<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo1-completed", false);
            return index.get(confirmKeyBuffer);
        }
    });
    // assert the exchange was deleted
    assertNull(bf);
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) Index(org.fusesource.hawtdb.api.Index) Test(org.junit.Test)

Example 10 with Buffer

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

the class HawtDBAggregateNotLostTest method testHawtDBAggregateNotLost.

@Test
public void testHawtDBAggregateNotLost() throws Exception {
    getMockEndpoint("mock:aggregated").expectedBodiesReceived("ABCDE");
    getMockEndpoint("mock:result").expectedMessageCount(0);
    template.sendBodyAndHeader("direct:start", "A", "id", 123);
    template.sendBodyAndHeader("direct:start", "B", "id", 123);
    template.sendBodyAndHeader("direct:start", "C", "id", 123);
    template.sendBodyAndHeader("direct:start", "D", "id", 123);
    template.sendBodyAndHeader("direct:start", "E", "id", 123);
    assertMockEndpointsSatisfied(30, TimeUnit.SECONDS);
    Thread.sleep(1000);
    String exchangeId = getMockEndpoint("mock:aggregated").getReceivedExchanges().get(0).getExchangeId();
    // the exchange should be in the completed repo where we should be able to find it
    final HawtDBFile hawtDBFile = repo.getHawtDBFile();
    final HawtDBCamelCodec codec = new HawtDBCamelCodec();
    final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
    Buffer bf = hawtDBFile.execute(new Work<Buffer>() {

        public Buffer execute(Transaction tx) {
            Index<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo1-completed", false);
            return index.get(confirmKeyBuffer);
        }
    });
    // assert the exchange was not lost and we got all the information still
    assertNotNull(bf);
    Exchange completed = codec.unmarshallExchange(context, bf);
    assertNotNull(completed);
    // should retain the exchange id
    assertEquals(exchangeId, completed.getExchangeId());
    assertEquals("ABCDE", completed.getIn().getBody());
    assertEquals(123, completed.getIn().getHeader("id"));
    assertEquals("size", completed.getProperty(Exchange.AGGREGATED_COMPLETED_BY));
    assertEquals(5, completed.getProperty(Exchange.AGGREGATED_SIZE));
    // will store correlation keys as String
    assertEquals("123", completed.getProperty(Exchange.AGGREGATED_CORRELATION_KEY));
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Exchange(org.apache.camel.Exchange) Transaction(org.fusesource.hawtdb.api.Transaction) Index(org.fusesource.hawtdb.api.Index) Test(org.junit.Test)

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