You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by James Strachan <ja...@gmail.com> on 2007/06/06 20:09:47 UTC

Re: use of splitter component to split serialised JaxB

On 5/31/07, Nick Outram <ap...@nickoutram.name> wrote:
>
> I've been struggling to use the splitter component to split an incoming JaxB
> representation of an order message into its constituent transactions; the
> message is coming from an activemq endpoint. After hours of wandering
> through twisty little generics all alike... I think I've done my brain some
> damage and could do with an example if somebody knows if it's possible any
> takers? :)

LOL

So I guess the main thing is to write the Expression to extract the
items from the order. Since these are Jaxb POJOs you might wanna use
something like ruby/groovy/jxpath or something. It might be easiest to
start off with just writing a Java Expression....

from("activemq:someQueue").splitter(new Expression<Exchange>() {
  public Object evaluate(Exchange exchange) {
     Order order = exchange.getIn().getBody(Order.class);
     return order..getLineItems();
  }
}).to("activemq:anotherQueue");

you could wrap this expression up in a helper method if you find it
useful in multiple rules. Once thats working, you could try a
groovy/beanshell/ruby alternative maybe?

-- 
James
-------
http://macstrac.blogspot.com/

Re: use of splitter component to split serialised JaxB

Posted by cmdr <sp...@yahoo.fr>.
I found the problem :jumping:

I was using the spring SingleConnectionDataSource.
The connection seems to be close after the first message.

In fact when messages are redirected to a file after the split, they are
push in one unique ligne.
That why I thought the first message was the only one to be send.

=)

So how to split using xml, this example code is obsolate
recipients seems to be invalids tags

<camelContext id="buildSplitter"
xmlns="http://activemq.apache.org/camel/schema/spring">
    <route>
      <from uri="seda:a"/>
      <splitter>
        <recipients>
          <bodyAs class="java.lang.String"/>
          <tokenize token=""/>
        </recipients>
      </splitter>
      <to uri="seda:b"/>
    </route>
  </camelContext>



-- 
View this message in context: http://www.nabble.com/use-of-splitter-component-to-split-serialised-JaxB-tp10899172s22882p15231835.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: use of splitter component to split serialised JaxB

Posted by Aaron Crickenberger <aa...@intalgent.com>.
On Jan 31, 2008, at 11:33 AM, cmdr wrote:

> Look at this:
>
> from("file:request.sql").splitter(body(String.class).tokenize("\n"))
> .process(new Processor()
> {
> 	public void process(Exchange exchange) throws Exception
> 	{
> 		System.out.println(exchange.getIn());
> 	}
> })
> .to("jdbc:dataSource")
> .process(new Processor()
> {
> 	public void process(Exchange exchange) throws Exception
> 	{
> 		System.err.println(exchange.getIn());
> 	}
> });
>
> and
>
> from("file:request.sql").splitter(body(String.class).tokenize("\n"))
> .process(new Processor()
> {
> 	public void process(Exchange exchange) throws Exception
> 	{
> 		System.out.println(exchange.getIn());
> 	}
> })
> .process(new Processor()
> {
> 	public void process(Exchange exchange) throws Exception
> 	{
> 		System.err.println(exchange.getIn());
> 	}
> });
>
> In the first example I get 1 output in the System.err.println
> In the second example (whithout the to("jdbc:...")) I get all requests

Just so we're clear, the first route will take the output of running  
each SQL query, and send that on to your second Processor.  The second  
route is just a straight pass-through, because your Processors don't  
set the exchange's Out message anywhere.

Does the first query successfully execute when you have this problem?

Below is an example that worked for me locally (Mac OS X, MySQL 5),  
with a few simple count queries in the "statements.sql" file.  Does  
this work for you at all?

---SplitterExplorationTest.java---
import javax.naming.Context;
import javax.sql.DataSource;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.util.jndi.JndiTest;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class SplitterExplorationTest extends ContextTestSupport
{
     public void testSomethingOrOther() throws InterruptedException
     {
         final MockEndpoint mock =  
resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
         mock.expectedMessageCount(4);
         mock.assertIsSatisfied();
     }

     @Override
     protected Context createJndiContext() throws Exception
     {
         final DataSource ds = new  
DriverManagerDataSource("com.mysql.jdbc.Driver", "jdbc:mysql:// 
localhost/activemq", "dev", "dev");
         final Context context = JndiTest.createInitialContext();
         context.bind("myDataSource", ds);
         return context;
     }

     @Override
     protected RouteBuilder createRouteBuilder() throws Exception
     {
         return new RouteBuilder()
         {
             @Override
             public void configure()
             {
                 from("file:/tmp/camel/ 
statements 
sql 
").splitter 
(body 
(String 
class 
).tokenize 
("\n 
")).to 
("log:preJdbc 
").to("jdbc:myDataSource").to("log:postJdbc").to("mock:result");
             }
         };
     }
}

---statements.sql---
select count(*) from activemq_acks
select count(*) from activemq_locks
select count(*) from meddiusmq_msgs
select id from activemq_msgs limit 2

- aaron

Re: use of splitter component to split serialised JaxB

Posted by cmdr <sp...@yahoo.fr>.
Look at this:

from("file:request.sql").splitter(body(String.class).tokenize("\n"))
.process(new Processor()
{
	public void process(Exchange exchange) throws Exception
	{
		System.out.println(exchange.getIn());
	}
})
.to("jdbc:dataSource")
.process(new Processor()
{
	public void process(Exchange exchange) throws Exception
	{
		System.err.println(exchange.getIn());
	}
});

and

from("file:request.sql").splitter(body(String.class).tokenize("\n"))
.process(new Processor()
{
	public void process(Exchange exchange) throws Exception
	{
		System.out.println(exchange.getIn());
	}
})
.process(new Processor()
{
	public void process(Exchange exchange) throws Exception
	{
		System.err.println(exchange.getIn());
	}
});

In the first example I get 1 output in the System.err.println
In the second example (whithout the to("jdbc:...")) I get all requests
-- 
View this message in context: http://www.nabble.com/use-of-splitter-component-to-split-serialised-JaxB-tp10899172s22882p15208068.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: use of splitter component to split serialised JaxB

Posted by cmdr <sp...@yahoo.fr>.
I tried the last version of camel (1.3-SNAPSHOT) but that did not solve the
problem.

I tried this:

from("file:resultset2.sql")
.splitter(body(String.class).tokenize("\n"))
.process(new Processor(){
	public void process(Exchange exchange) throws Exception
	{
		System.out.println(exchange.getIn());
	}})
.to("jdbc:dataSource");

It seems that the splitting is done correctly
- the output message display my sql requests
- the exchange body type is FileMessage

There is a problem when with the to() after splitting.
When I try to("file:dataSource_atg.txt"), I get a file with only the first
message




-- 
View this message in context: http://www.nabble.com/use-of-splitter-component-to-split-serialised-JaxB-tp10899172s22882p15207368.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: use of splitter component to split serialised JaxB

Posted by Roman Kalukiewicz <ro...@gmail.com>.
2008/1/31, Roman Kalukiewicz <ro...@gmail.com>:
> 2008/1/30, cmdr <sp...@yahoo.fr>:
> >
> > Hi
> >
> > I have already tried
> > from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("jdbc:dataSource");
> > The problem is that only the first line is send as a message.:-(
> >
> > When I use
> > from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("file:dataSource.txt"),
> > I get only the first line in the output file.

BTW Could you try it with the newest 1.3-SNAPSHOT version (if you use
some older one)?

Roman

Re: use of splitter component to split serialised JaxB

Posted by Roman Kalukiewicz <ro...@gmail.com>.
2008/1/30, cmdr <sp...@yahoo.fr>:
>
> Hi
>
> I have already tried
> from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("jdbc:dataSource");
> The problem is that only the first line is send as a message.:-(
>
> When I use
> from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("file:dataSource.txt"),
> I get only the first line in the output file.

In case of the second file->file example I just suspect, that you
overwrite this destination file for every splitted part... There is
append parameter to the file endpoint that maybe could help.

Lets try to change file to log component at the very end to verify if
it actually works and splits your message.

Anyway this jdbc problem is strange..

Roman

Re: use of splitter component to split serialised JaxB

Posted by cmdr <sp...@yahoo.fr>.
Hi

I have already tried
from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("jdbc:dataSource");
The problem is that only the first line is send as a message.:-(

When I use
from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("file:dataSource.txt"),
I get only the first line in the output file.

cmdr

-- 
View this message in context: http://www.nabble.com/use-of-splitter-component-to-split-serialised-JaxB-tp10899172s22882p15189225.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: use of splitter component to split serialised JaxB

Posted by Roman Kalukiewicz <ro...@gmail.com>.
> I have the same problem, I want to split the content of a text file to
> others messages and send then to a queue. What is the type tu return with
> evaluate() method.

Basically what you should return from this evaluate method in case of
splitter should be an array or collection. Otherwise your message will
be splitted into.. one part ;)

> from("file:insert_request.sql").splitter(new Expression<Exchange>()
> {
>         public Object evaluate(Exchange exchange)
>         {
>                 return exchange.getIn().getBody(String.class).split("\n");
>         }
> }).to("jdbc:dataSource");

But instead of writing it the way you do, you can write something like

from("file:insert_request.sql").splitter(body(String.class).tokenize("\n")).to("jdbc:dataSource");

Should do the same thing, but definitely looks simpler ;)

Roman

Re: use of splitter component to split serialised JaxB

Posted by cmdr <sp...@yahoo.fr>.

James.Strachan wrote:
> 
> On 5/31/07, Nick Outram <ap...@nickoutram.name> wrote:
>>
>> I've been struggling to use the splitter component to split an incoming
>> JaxB
>> representation of an order message into its constituent transactions; the
>> message is coming from an activemq endpoint. After hours of wandering
>> through twisty little generics all alike... I think I've done my brain
>> some
>> damage and could do with an example if somebody knows if it's possible
>> any
>> takers? :)
> 
> LOL
> 
> So I guess the main thing is to write the Expression to extract the
> items from the order. Since these are Jaxb POJOs you might wanna use
> something like ruby/groovy/jxpath or something. It might be easiest to
> start off with just writing a Java Expression....
> 
> from("activemq:someQueue").splitter(new Expression<Exchange>() {
>   public Object evaluate(Exchange exchange) {
>      Order order = exchange.getIn().getBody(Order.class);
>      return order..getLineItems();
>   }
> }).to("activemq:anotherQueue");
> 
> you could wrap this expression up in a helper method if you find it
> useful in multiple rules. Once thats working, you could try a
> groovy/beanshell/ruby alternative maybe?
> 
> -- 
> James
> -------
> http://macstrac.blogspot.com/
> 
> 

I have the same problem, I want to split the content of a text file to
others messages and send then to a queue. What is the type tu return with
evaluate() method.

from("file:insert_request.sql").splitter(new Expression<Exchange>() 
{
	public Object evaluate(Exchange exchange)
	{
		return exchange.getIn().getBody(String.class).split("\n");
	}
}).to("jdbc:dataSource");

-- 
View this message in context: http://www.nabble.com/use-of-splitter-component-to-split-serialised-JaxB-tp10899172s22882p15183693.html
Sent from the Camel - Users mailing list archive at Nabble.com.