You sqlEndpoint is not bond to the sql component that already has the reference of the data source.

Please use below code to inject the sql endpoint.

@EndpointInject(uri = "sql://xxx")
protected Endpoint sql;

Willem

On 3/23/11 4:25 PM, Charles Moulliard wrote:
Willem,

I can send something using the template with following code but
nothing is inserted into the DB ....

So my question is : How my SqlEndpoint will be linked to the
DataSource which has been defined like that in the spring xml file ?

1) Spring

     <bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
         <property name="dataSource" ref="reportdb"/>
     </bean>

2) Code

     private ProducerTemplate template;

     private static SqlEndpoint sqlEndpoint = new SqlEndpoint();

     public JdbcReportIncident() {
         sqlEndpoint.setEndpointUriIfNotSpecified("sql");
     }

     public void insertRecord(@Body String ref) {

         StringBuilder queryBuilder = new StringBuilder();
         queryBuilder.append("INSERT INTO REPORT.T_INCIDENT (INCIDENT_REF,\n" +
                 "
INCIDENT_DATE,GIVEN_NAME,FAMILY_NAME,SUMMARY,DETAILS,EMAIL,PHONE)\n" +
                 "     VALUES (");
         queryBuilder.append("'" + ref + "'");
         queryBuilder.append("'2011-03-21','Charles','Moulliard','Incident
Webinar','This is a\n" +
                 "      report incident for
webinar-001','[email protected]','+111 10 20 300'");


         String query = queryBuilder.toString();
         System.out.println(">>>  Query created : " + query );

         sqlEndpoint.setQuery(query);
         template.send(sqlEndpoint,(Exchange) null) ;


     }


Regards,

Charles

On Wed, Mar 23, 2011 at 8:48 AM, Willem Jiang<[email protected]>  wrote:
You can set query on the sql endpoint by calling setQuery() method of sql
endpoint. You can update your query just before call the template.send()

Willem

On 3/23/11 3:32 PM, Charles Moulliard wrote:

Hi Willem,

Do you think that I can create dynamically the XXX part of the URI
instead of hard coded it in the @EndpointInject(uri = "sql://xxx") ?

1) Normally, the SQL component will be used using this syntax

<to uri="sql:select * from REPORT where incidentRef = '#'"/>

and # symbol will be used by the component to replace the value from
the Body message send to this endpoint

2) But I would like to define it in a more dynamically way using a
ProducerTemplate and Bean

         StringBuilder query = new StringBuilder();
         query.append("sql:INSERT INTO REPORT.T_INCIDENT (INCIDENT_REF,\n"
+
                 "
INCIDENT_DATE,GIVEN_NAME,FAMILY_NAME,SUMMARY,DETAILS,EMAIL,PHONE)\n" +
                 "       VALUES (");
         query.append("'" + ref + "'");
         query.append("'2011-03-21','Charles','Moulliard','Incident
Webinar','This is a\n" +
                 "                    report incident for
webinar-001','[email protected]','+111 10 20 300'");

         template.send(query.toString(), (Exchange) null);

Regards,

Charles Moulliard

Sr. Principal Solution Architect - FuseSource
Apache Committer

Blog : http://cmoulliard.blogspot.com
Twitter : http://twitter.com/cmoulliard
Linkedin : http://www.linkedin.com/in/charlesmoulliard
Skype: cmoulliard



On Wed, Mar 23, 2011 at 8:05 AM, Willem Jiang<[email protected]>
  wrote:

Hi Charles,

I think you can add the endpoint in your Camel bean like this
@EndpointInject(uri = "sql://xxx")
protected Endpoint sql;
and use template.sendBody(endpoint, body) to send the message.

or use call template.sendBody("sql:xxx", body) to send the request.

Willem

On 3/23/11 2:11 PM, Charles Moulliard wrote:

Hi Willem,

The producer has been defined like that :

1) Spring XML

<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="reportdb"/>
</bean>

<bean id="reportdb"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost/reportdb"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>

2) Camel bean

private ProducerTemplate template;

public void insertRecord(@Body String ref) {

StringBuilder query = new StringBuilder();
query.append("INSERT INTO REPORT.T_INCIDENT (INCIDENT_REF,\n" +
"
INCIDENT_DATE,GIVEN_NAME,FAMILY_NAME,SUMMARY,DETAILS,EMAIL,PHONE)\n" +
" VALUES (");
query.append("'" + ref + "'");
query.append("'2011-03-21','Charles','Moulliard','Incident
Webinar','This is a\n" +
" report incident for
webinar-001','[email protected]
<mailto:[email protected]>','+111 10 20 300'");

template.sendBody("sql", query.toString());


}

Should I define an endpoint ?

Regards,

Charles


On 23/03/11 05:52, Willem Jiang wrote:

Hi Charles,

How did you setup the default endpoint of the producerTemplate?
It looks the query is used be resolved as an CamelEndpoint, that could
explain why this line of code is called.
at


org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:57)


Willem

On 3/23/11 1:05 AM, Charles Moulliard wrote:

I use a ProducerTemplate to create the SQL query send to sql endpoint.
When I launch camel, I get this error :

Caused by: java.net.URISyntaxException: Illegal character in opaque
part at index 54:
sql:INSERT%20INTO%20REPORT.T_INCIDENT%20(INCIDENT_REF,


%20%20%20%20%20%20%20INCIDENT_DATE,GIVEN_NAME,FAMILY_NAME,SUMMARY,DETAILS,EMAIL,PHONE)



%20%20%20%20%20%20%20VALUES%20('099','2011-03-21','Charles','Moulliard','Incident%20Webinar','This%20is%20a



%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20report%20incident%20for%20webinar-001','[email protected]','+111%2010%2020%20300'

at java.net.URI$Parser.fail(URI.java:2809)
at java.net.URI$Parser.checkChars(URI.java:2982)
at java.net.URI$Parser.parse(URI.java:3019)
at java.net.URI.<init>(URI.java:578)
at


org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:57)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at


sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at


sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

Is there a way to avoid that ?

Here is the code :

public void insertRecord(@Body String ref) {

StringBuilder query = new StringBuilder();
query.append("sql:INSERT INTO REPORT.T_INCIDENT (INCIDENT_REF,\n" +
"
INCIDENT_DATE,GIVEN_NAME,FAMILY_NAME,SUMMARY,DETAILS,EMAIL,PHONE)\n" +
" VALUES (");
query.append("'" + ref + "'");
query.append("'2011-03-21','Charles','Moulliard','Incident
Webinar','This is a\n" +
" report incident for
webinar-001','[email protected]','+111 10 20 300'");

template.sendBody(query.toString());


}

Regards,


Charles Moulliard

Sr. Principal Solution Architect - FuseSource
Apache Committer

Blog : http://cmoulliard.blogspot.com
Twitter : http://twitter.com/cmoulliard
Linkedin : http://www.linkedin.com/in/charlesmoulliard
Skype: cmoulliard






--
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
         http://jnn.javaeye.com (Chinese)
Twitter: willemjiang




--
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
         http://jnn.javaeye.com (Chinese)
Twitter: willemjiang




--
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
         http://jnn.javaeye.com (Chinese)
Twitter: willemjiang

Reply via email to