diff --git a/src/main/java/org/hisp/hieboot/camel/MessageRepositoryConfiguration.java b/src/main/java/org/hisp/hieboot/camel/MessageRepositoryConfiguration.java index 464d029..0cb8a8f 100644 --- a/src/main/java/org/hisp/hieboot/camel/MessageRepositoryConfiguration.java +++ b/src/main/java/org/hisp/hieboot/camel/MessageRepositoryConfiguration.java @@ -18,12 +18,9 @@ abstract class MessageRepositoryConfiguration { matchIfMissing = true) static class Jdbc { - @Value("${camel.messageRepository.datasourceName}") - private String datasourceName; - @Bean public MessageRepository jdbcMessageRepository() { - return new JdbcMessageRepository(datasourceName); + return new JdbcMessageRepository(); } } diff --git a/src/main/java/org/hisp/hieboot/camel/impl/JdbcMessageRepository.java b/src/main/java/org/hisp/hieboot/camel/impl/JdbcMessageRepository.java index bb19547..fc33fd6 100644 --- a/src/main/java/org/hisp/hieboot/camel/impl/JdbcMessageRepository.java +++ b/src/main/java/org/hisp/hieboot/camel/impl/JdbcMessageRepository.java @@ -11,9 +11,11 @@ import org.apache.camel.support.DefaultMessage; import org.apache.camel.support.MessageHelper; import org.apache.camel.support.service.ServiceSupport; +import org.hisp.hieboot.camel.RuntimeCamelHieBootException; import org.hisp.hieboot.camel.spi.MessageRepository; import org.hisp.hieboot.camel.spi.RepositoryMessage; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import java.io.IOException; import java.util.ArrayList; @@ -32,9 +34,26 @@ public class JdbcMessageRepository extends ServiceSupport implements MessageRepo @Autowired private CamelContext camelContext; + @Value("${camel.messageRepository.datasourceName}") private String dataSourceName; - public JdbcMessageRepository(String dataSourceName) { + public ProducerTemplate getProducerTemplate() { + return producerTemplate; + } + + public void setProducerTemplate(ProducerTemplate producerTemplate) { + this.producerTemplate = producerTemplate; + } + + public CamelContext getCamelContext() { + return camelContext; + } + + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + public void setDataSourceName(String dataSourceName) { this.dataSourceName = dataSourceName; } @@ -102,12 +121,22 @@ protected List toRepositoryMessages(List> Message message = new DefaultMessage(camelContext); message.setBody(row.get("body")); - byte[] headersAsBytes = (byte[]) row.get("headers"); + Object headers = row.get("headers"); Map headersAsMap; - try { - headersAsMap = OBJECT_MAPPER.readValue(headersAsBytes, Map.class); - } catch (IOException e) { - throw new RuntimeException(e); + if (headers instanceof byte[]) { + try { + headersAsMap = OBJECT_MAPPER.readValue( (byte[]) headers, Map.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + } else if (headers instanceof String) { + try { + headersAsMap = OBJECT_MAPPER.readValue((String) headers, Map.class); + } catch (IOException e) { + throw new RuntimeCamelHieBootException(e); + } + } else { + throw new RuntimeCamelHieBootException(String.format("Unsupported data type when attempting to unmarshal Camel message headers from database: [%s]. Hint: are you sure you are using a supported database?", headers.getClass().getName())); } message.setHeaders(headersAsMap); @@ -148,4 +177,5 @@ protected List doRetrieve(Exchange jdbcExchange) { } return toRepositoryMessages(replyJdbcExchange.getMessage().getBody(List.class)); } + } diff --git a/src/test/java/org/hisp/hieboot/camel/impl/JdbcMessageRepositoryTestCase.java b/src/test/java/org/hisp/hieboot/camel/impl/JdbcMessageRepositoryTestCase.java new file mode 100644 index 0000000..14c489f --- /dev/null +++ b/src/test/java/org/hisp/hieboot/camel/impl/JdbcMessageRepositoryTestCase.java @@ -0,0 +1,22 @@ +package org.hisp.hieboot.camel.impl; + +import org.apache.camel.impl.DefaultCamelContext; +import org.hisp.hieboot.camel.RuntimeCamelHieBootException; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JdbcMessageRepositoryTestCase { + + @Test + public void testToRepositoryMessagesGivenUnsupportedHeadersDataType() { + JdbcMessageRepository jdbcMessageRepository = new JdbcMessageRepository(); + jdbcMessageRepository.setCamelContext(new DefaultCamelContext()); + RuntimeCamelHieBootException runtimeCamelHieBootException = assertThrows(RuntimeCamelHieBootException.class, () -> jdbcMessageRepository.toRepositoryMessages(List.of(Map.of("headers", 1)))); + assertEquals("Unsupported data type when attempting to unmarshal Camel message headers from database: [java.lang.Integer]. Hint: are you sure you are using a supported database?", runtimeCamelHieBootException.getMessage()); + } +}