You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Thomas Bender <Th...@flatironssolutions.com> on 2016/04/01 13:12:41 UTC

[CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty

Hi,

I have an issue with the camel sql-component.

I use the options outputHeader=myHeader and outputType=SelectOne. When the query returns no result, the body of the Exchange is set to null instead of preserved what I would expect when using outputHeader.

In my opinion the problem is in lines 175-185 of SqlProducer.java that does not set the body in case the data is null (missing else block). As I am new to Camel, I don't know if this was intended for a special case.

// Code section

if (data != null) { // <--- Missing the else
// for noop=true we still want to enrich with the row count header
  if (getEndpoint().isNoop()) {
    exchange.getOut().setBody(exchange.getIn().getBody());
  } else if (getEndpoint().getOutputHeader() != null) {
    exchange.getOut().setBody(exchange.getIn().getBody());
    exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
  } else {
    exchange.getOut().setBody(data);
  }
  exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 1);
}


--------
following could be added (untested)

else { // if data == null
if (getEndpoint().isNoop()) {
                                        exchange.getOut().setBody(exchange.getIn().getBody());
                                    } else if (getEndpoint().getOutputHeader() != null) {
                                        exchange.getOut().setBody(exchange.getIn().getBody());
                                    }
 exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 0);
}



--------------------------
Following testcase:

import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader;
import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { CamelSqlComponentTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class)
public class CamelSqlComponentTest {

        @EndpointInject(uri = "mock:result")
        protected MockEndpoint resultEndpoint;

        @Produce(uri = "direct:start")
        protected ProducerTemplate template;

        @Test
        @DirtiesContext
        public void testSqlEndpoint() throws Exception {

                String expectedBody = "body";
                resultEndpoint.expectedBodiesReceived(expectedBody);
                template.sendBody(expectedBody);
                resultEndpoint.assertIsSatisfied();

        }

        @Configuration
        public static class TestConfig extends SingleRouteCamelConfiguration {

                @Bean(destroyMethod = "shutdown", name = "dataSource")
                public EmbeddedDatabase dataSource() {
                        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
                                        .addScript("db-camel-schema.sql").addScript("db-camel-data.sql").build();
                }

                @Bean
                @Override
                public RouteBuilder route() {
                        return new RouteBuilder() {
                                @Override
                                public void configure() throws Exception {
                                        from("direct:start")
                                                        .to("sql:select id from mytable where 1 = 2?dataSource=dataSource&outputHeader=myHeader&outputType=SelectOne")
                                                        .log("${body}").to("mock:result");
                                }
                        };
                }
        }
}

--------------------------
db-camel-data.sql

insert into MYTABLE (ID, SUBJECT) VALUES (1, 'subject');

--------------------------
db-camel-schema.sql

CREATE TABLE MYTABLE (
        ID  INT PRIMARY KEY,
        SUBJECT         VARCHAR(255)
);

Have a nice weekend.
Thomas


Re: [CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Ah yeah that seems resonable. You are welcome to log a JIRA and attach
a patch or do a github PR

On Fri, Apr 1, 2016 at 2:01 PM, Thomas Bender
<Th...@flatironssolutions.com> wrote:
> Hi Claus,
>
> Sorry about missing that out:
> Camel 2.17.0
> Spring 4.2.5.RELEASE
>
> Thomas
>
>
>
> ________________________________________
> Von: Claus Ibsen [claus.ibsen@gmail.com]
> Gesendet: Freitag, 1. April 2016 13:45
> An: users@camel.apache.org
> Betreff: Re: [CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty
>
> What version of Camel do you use?
>
> On Fri, Apr 1, 2016 at 1:12 PM, Thomas Bender
> <Th...@flatironssolutions.com> wrote:
>> Hi,
>>
>> I have an issue with the camel sql-component.
>>
>> I use the options outputHeader=myHeader and outputType=SelectOne. When the query returns no result, the body of the Exchange is set to null instead of preserved what I would expect when using outputHeader.
>>
>> In my opinion the problem is in lines 175-185 of SqlProducer.java that does not set the body in case the data is null (missing else block). As I am new to Camel, I don't know if this was intended for a special case.
>>
>> // Code section
>>
>> if (data != null) { // <--- Missing the else
>> // for noop=true we still want to enrich with the row count header
>>   if (getEndpoint().isNoop()) {
>>     exchange.getOut().setBody(exchange.getIn().getBody());
>>   } else if (getEndpoint().getOutputHeader() != null) {
>>     exchange.getOut().setBody(exchange.getIn().getBody());
>>     exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
>>   } else {
>>     exchange.getOut().setBody(data);
>>   }
>>   exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 1);
>> }
>>
>>
>> --------
>> following could be added (untested)
>>
>> else { // if data == null
>> if (getEndpoint().isNoop()) {
>>                                         exchange.getOut().setBody(exchange.getIn().getBody());
>>                                     } else if (getEndpoint().getOutputHeader() != null) {
>>                                         exchange.getOut().setBody(exchange.getIn().getBody());
>>                                     }
>>  exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 0);
>> }
>>
>>
>>
>> --------------------------
>> Following testcase:
>>
>> import org.apache.camel.EndpointInject;
>> import org.apache.camel.Produce;
>> import org.apache.camel.ProducerTemplate;
>> import org.apache.camel.builder.RouteBuilder;
>> import org.apache.camel.component.mock.MockEndpoint;
>> import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
>> import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader;
>> import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
>> import org.junit.Test;
>> import org.junit.runner.RunWith;
>> import org.springframework.context.annotation.Bean;
>> import org.springframework.context.annotation.Configuration;
>> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
>> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
>> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
>> import org.springframework.test.annotation.DirtiesContext;
>> import org.springframework.test.context.ContextConfiguration;
>>
>> @RunWith(CamelSpringJUnit4ClassRunner.class)
>> @ContextConfiguration(classes = { CamelSqlComponentTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class)
>> public class CamelSqlComponentTest {
>>
>>         @EndpointInject(uri = "mock:result")
>>         protected MockEndpoint resultEndpoint;
>>
>>         @Produce(uri = "direct:start")
>>         protected ProducerTemplate template;
>>
>>         @Test
>>         @DirtiesContext
>>         public void testSqlEndpoint() throws Exception {
>>
>>                 String expectedBody = "body";
>>                 resultEndpoint.expectedBodiesReceived(expectedBody);
>>                 template.sendBody(expectedBody);
>>                 resultEndpoint.assertIsSatisfied();
>>
>>         }
>>
>>         @Configuration
>>         public static class TestConfig extends SingleRouteCamelConfiguration {
>>
>>                 @Bean(destroyMethod = "shutdown", name = "dataSource")
>>                 public EmbeddedDatabase dataSource() {
>>                         return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
>>                                         .addScript("db-camel-schema.sql").addScript("db-camel-data.sql").build();
>>                 }
>>
>>                 @Bean
>>                 @Override
>>                 public RouteBuilder route() {
>>                         return new RouteBuilder() {
>>                                 @Override
>>                                 public void configure() throws Exception {
>>                                         from("direct:start")
>>                                                         .to("sql:select id from mytable where 1 = 2?dataSource=dataSource&outputHeader=myHeader&outputType=SelectOne")
>>                                                         .log("${body}").to("mock:result");
>>                                 }
>>                         };
>>                 }
>>         }
>> }
>>
>> --------------------------
>> db-camel-data.sql
>>
>> insert into MYTABLE (ID, SUBJECT) VALUES (1, 'subject');
>>
>> --------------------------
>> db-camel-schema.sql
>>
>> CREATE TABLE MYTABLE (
>>         ID  INT PRIMARY KEY,
>>         SUBJECT         VARCHAR(255)
>> );
>>
>> Have a nice weekend.
>> Thomas
>>
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

AW: [CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty

Posted by Thomas Bender <Th...@flatironssolutions.com>.
Hi Claus,

Sorry about missing that out:
Camel 2.17.0
Spring 4.2.5.RELEASE

Thomas



________________________________________
Von: Claus Ibsen [claus.ibsen@gmail.com]
Gesendet: Freitag, 1. April 2016 13:45
An: users@camel.apache.org
Betreff: Re: [CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty

What version of Camel do you use?

On Fri, Apr 1, 2016 at 1:12 PM, Thomas Bender
<Th...@flatironssolutions.com> wrote:
> Hi,
>
> I have an issue with the camel sql-component.
>
> I use the options outputHeader=myHeader and outputType=SelectOne. When the query returns no result, the body of the Exchange is set to null instead of preserved what I would expect when using outputHeader.
>
> In my opinion the problem is in lines 175-185 of SqlProducer.java that does not set the body in case the data is null (missing else block). As I am new to Camel, I don't know if this was intended for a special case.
>
> // Code section
>
> if (data != null) { // <--- Missing the else
> // for noop=true we still want to enrich with the row count header
>   if (getEndpoint().isNoop()) {
>     exchange.getOut().setBody(exchange.getIn().getBody());
>   } else if (getEndpoint().getOutputHeader() != null) {
>     exchange.getOut().setBody(exchange.getIn().getBody());
>     exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
>   } else {
>     exchange.getOut().setBody(data);
>   }
>   exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 1);
> }
>
>
> --------
> following could be added (untested)
>
> else { // if data == null
> if (getEndpoint().isNoop()) {
>                                         exchange.getOut().setBody(exchange.getIn().getBody());
>                                     } else if (getEndpoint().getOutputHeader() != null) {
>                                         exchange.getOut().setBody(exchange.getIn().getBody());
>                                     }
>  exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 0);
> }
>
>
>
> --------------------------
> Following testcase:
>
> import org.apache.camel.EndpointInject;
> import org.apache.camel.Produce;
> import org.apache.camel.ProducerTemplate;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.component.mock.MockEndpoint;
> import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
> import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader;
> import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
> import org.junit.Test;
> import org.junit.runner.RunWith;
> import org.springframework.context.annotation.Bean;
> import org.springframework.context.annotation.Configuration;
> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
> import org.springframework.test.annotation.DirtiesContext;
> import org.springframework.test.context.ContextConfiguration;
>
> @RunWith(CamelSpringJUnit4ClassRunner.class)
> @ContextConfiguration(classes = { CamelSqlComponentTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class)
> public class CamelSqlComponentTest {
>
>         @EndpointInject(uri = "mock:result")
>         protected MockEndpoint resultEndpoint;
>
>         @Produce(uri = "direct:start")
>         protected ProducerTemplate template;
>
>         @Test
>         @DirtiesContext
>         public void testSqlEndpoint() throws Exception {
>
>                 String expectedBody = "body";
>                 resultEndpoint.expectedBodiesReceived(expectedBody);
>                 template.sendBody(expectedBody);
>                 resultEndpoint.assertIsSatisfied();
>
>         }
>
>         @Configuration
>         public static class TestConfig extends SingleRouteCamelConfiguration {
>
>                 @Bean(destroyMethod = "shutdown", name = "dataSource")
>                 public EmbeddedDatabase dataSource() {
>                         return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
>                                         .addScript("db-camel-schema.sql").addScript("db-camel-data.sql").build();
>                 }
>
>                 @Bean
>                 @Override
>                 public RouteBuilder route() {
>                         return new RouteBuilder() {
>                                 @Override
>                                 public void configure() throws Exception {
>                                         from("direct:start")
>                                                         .to("sql:select id from mytable where 1 = 2?dataSource=dataSource&outputHeader=myHeader&outputType=SelectOne")
>                                                         .log("${body}").to("mock:result");
>                                 }
>                         };
>                 }
>         }
> }
>
> --------------------------
> db-camel-data.sql
>
> insert into MYTABLE (ID, SUBJECT) VALUES (1, 'subject');
>
> --------------------------
> db-camel-schema.sql
>
> CREATE TABLE MYTABLE (
>         ID  INT PRIMARY KEY,
>         SUBJECT         VARCHAR(255)
> );
>
> Have a nice weekend.
> Thomas
>



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: [CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty

Posted by Claus Ibsen <cl...@gmail.com>.
What version of Camel do you use?

On Fri, Apr 1, 2016 at 1:12 PM, Thomas Bender
<Th...@flatironssolutions.com> wrote:
> Hi,
>
> I have an issue with the camel sql-component.
>
> I use the options outputHeader=myHeader and outputType=SelectOne. When the query returns no result, the body of the Exchange is set to null instead of preserved what I would expect when using outputHeader.
>
> In my opinion the problem is in lines 175-185 of SqlProducer.java that does not set the body in case the data is null (missing else block). As I am new to Camel, I don't know if this was intended for a special case.
>
> // Code section
>
> if (data != null) { // <--- Missing the else
> // for noop=true we still want to enrich with the row count header
>   if (getEndpoint().isNoop()) {
>     exchange.getOut().setBody(exchange.getIn().getBody());
>   } else if (getEndpoint().getOutputHeader() != null) {
>     exchange.getOut().setBody(exchange.getIn().getBody());
>     exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
>   } else {
>     exchange.getOut().setBody(data);
>   }
>   exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 1);
> }
>
>
> --------
> following could be added (untested)
>
> else { // if data == null
> if (getEndpoint().isNoop()) {
>                                         exchange.getOut().setBody(exchange.getIn().getBody());
>                                     } else if (getEndpoint().getOutputHeader() != null) {
>                                         exchange.getOut().setBody(exchange.getIn().getBody());
>                                     }
>  exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 0);
> }
>
>
>
> --------------------------
> Following testcase:
>
> import org.apache.camel.EndpointInject;
> import org.apache.camel.Produce;
> import org.apache.camel.ProducerTemplate;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.component.mock.MockEndpoint;
> import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
> import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader;
> import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
> import org.junit.Test;
> import org.junit.runner.RunWith;
> import org.springframework.context.annotation.Bean;
> import org.springframework.context.annotation.Configuration;
> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
> import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
> import org.springframework.test.annotation.DirtiesContext;
> import org.springframework.test.context.ContextConfiguration;
>
> @RunWith(CamelSpringJUnit4ClassRunner.class)
> @ContextConfiguration(classes = { CamelSqlComponentTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class)
> public class CamelSqlComponentTest {
>
>         @EndpointInject(uri = "mock:result")
>         protected MockEndpoint resultEndpoint;
>
>         @Produce(uri = "direct:start")
>         protected ProducerTemplate template;
>
>         @Test
>         @DirtiesContext
>         public void testSqlEndpoint() throws Exception {
>
>                 String expectedBody = "body";
>                 resultEndpoint.expectedBodiesReceived(expectedBody);
>                 template.sendBody(expectedBody);
>                 resultEndpoint.assertIsSatisfied();
>
>         }
>
>         @Configuration
>         public static class TestConfig extends SingleRouteCamelConfiguration {
>
>                 @Bean(destroyMethod = "shutdown", name = "dataSource")
>                 public EmbeddedDatabase dataSource() {
>                         return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
>                                         .addScript("db-camel-schema.sql").addScript("db-camel-data.sql").build();
>                 }
>
>                 @Bean
>                 @Override
>                 public RouteBuilder route() {
>                         return new RouteBuilder() {
>                                 @Override
>                                 public void configure() throws Exception {
>                                         from("direct:start")
>                                                         .to("sql:select id from mytable where 1 = 2?dataSource=dataSource&outputHeader=myHeader&outputType=SelectOne")
>                                                         .log("${body}").to("mock:result");
>                                 }
>                         };
>                 }
>         }
> }
>
> --------------------------
> db-camel-data.sql
>
> insert into MYTABLE (ID, SUBJECT) VALUES (1, 'subject');
>
> --------------------------
> db-camel-schema.sql
>
> CREATE TABLE MYTABLE (
>         ID  INT PRIMARY KEY,
>         SUBJECT         VARCHAR(255)
> );
>
> Have a nice weekend.
> Thomas
>



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2