You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by serega <se...@sybase.com> on 2010/11/15 22:04:05 UTC
ProducerTemplate with Spring and without
Hi.
I've tried examples from here
http://camel.apache.org/spring-testing.html
Two questions:
1. is how do I specify the producer template in spring?
I've tried the following line
<template id="test" defaultEndpoint="direct:start"/>
I got org.apache.camel.NoSuchEndpointException: No endpoint could be found
for: ref:direct:start
2. How to I write the following
@Produce(uri = "direct:start")
protected ProducerTemplate template;
without annotations?
Thanks,
Sergey.
--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-with-Spring-and-without-tp3266469p3266469.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: ProducerTemplate with Spring and without
Posted by Willem Jiang <wi...@gmail.com>.
On 11/16/10 9:46 PM, serega wrote:
>
> The fundamental question is how do I test my routes with mock endpoints on
> both ends?
> The following code works.
>
> public class CamelTest extends CamelTestSupport {
>
> @EndpointInject(uri = "mock:result")
> protected MockEndpoint resultEndpoint;
>
> @Produce(uri = "direct:start")
> protected ProducerTemplate template;
>
>
> public static class MyBean1 {
> public void doIt(Exchange exchange) {
> System.out.println(this.getClass());
> }
> }
>
> public static class MyBean2 {
> public void doIt(Exchange exchange) {
> System.out.println(this.getClass());
> }
> }
>
>
> @Override
> protected Context createJndiContext() throws Exception {
> Context context = super.createJndiContext();
> context.bind("bean1", new MyBean1());
> context.bind("bean2", new MyBean2());
> return context;
> }
>
> @Override
> protected RouteBuilder createRouteBuilder() {
> return new RouteBuilder() {
> public void configure() {
> from("direct:start")
> .beanRef("bean1")
> .beanRef("bean2")
> .to("mock:result");
> }
> };
> }
>
> @Test
> public void test1() throws InterruptedException {
> resultEndpoint.expectedBodiesReceived("Hello World");
> template.sendBody("direct:start", "Hello World");
> resultEndpoint.assertIsSatisfied();
> }
> }
>
> 1. How can I replicate the same test, with route configured using XML?
@Produce can work with or without the spring configured.
Here is spring configure example[1] that you can take a look.
>
> 2. I commented out line @Produce(uri = "direct:start")
> and added line
> template = this.context.getRegistry().lookup("template",
> ProducerTemplate.class);
> in createRouteBuilder(), but the template ended to be null.
>
oh, if you don't create the camel context from spring config, you need
create the template from camelcontext yourself.
template = context.createProducerTemplate();
[1]https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/produce/ProduceTemplateTest.java
[2]https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/produce/ProduceTemplateTest-context.xml
--
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.javaeye.com (Chinese)
Twitter: willemjiang
Re: ProducerTemplate with Spring and without
Posted by serega <se...@sybase.com>.
The fundamental question is how do I test my routes with mock endpoints on
both ends?
The following code works.
public class CamelTest extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
public static class MyBean1 {
public void doIt(Exchange exchange) {
System.out.println(this.getClass());
}
}
public static class MyBean2 {
public void doIt(Exchange exchange) {
System.out.println(this.getClass());
}
}
@Override
protected Context createJndiContext() throws Exception {
Context context = super.createJndiContext();
context.bind("bean1", new MyBean1());
context.bind("bean2", new MyBean2());
return context;
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start")
.beanRef("bean1")
.beanRef("bean2")
.to("mock:result");
}
};
}
@Test
public void test1() throws InterruptedException {
resultEndpoint.expectedBodiesReceived("Hello World");
template.sendBody("direct:start", "Hello World");
resultEndpoint.assertIsSatisfied();
}
}
1. How can I replicate the same test, with route configured using XML?
2. I commented out line @Produce(uri = "direct:start")
and added line
template = this.context.getRegistry().lookup("template",
ProducerTemplate.class);
in createRouteBuilder(), but the template ended to be null.
--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-with-Spring-and-without-tp3266469p3267325.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: ProducerTemplate with Spring and without
Posted by Willem Jiang <wi...@gmail.com>.
On 11/16/10 5:04 AM, serega wrote:
>
> Hi.
> I've tried examples from here
> http://camel.apache.org/spring-testing.html
>
> Two questions:
> 1. is how do I specify the producer template in spring?
> I've tried the following line
> <template id="test" defaultEndpoint="direct:start"/>
> I got org.apache.camel.NoSuchEndpointException: No endpoint could be found
> for: ref:direct:start
Can I have a look at your camel route?
If there is no consumer of the direct:start, the Exception is making
sense. Please make sure you have the
>
> 2. How to I write the following
>
> @Produce(uri = "direct:start")
> protected ProducerTemplate template;
>
> without annotations?
You can get the prodoucerTemplate from camelContext even you didn't
define it in the spring. Camel will register it automatically.
ProducerTemplate producerTemplate =
context.getRegistry().lookup("template", ProducerTemplate.class);
Then you can use the template to send the message to the endpoint that
you want.
>
> Thanks,
>
> Sergey.
--
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.javaeye.com (Chinese)
Twitter: willemjiang
Re: ProducerTemplate with Spring and without
Posted by Tarjei Huse <ta...@scanmine.com>.
Updating myself instead of anyone else working on this.
On 11/18/2010 11:58 AM, Tarjei Huse wrote:
> Hi, this is so related that I think it is not thread hijacking.
> On 11/18/2010 11:30 AM, Claus Ibsen wrote:
>>> @Produce(uri = "direct:start")
>>> protected ProducerTemplate template;
> What is required for Camel to find and add a proxy where this annotation
> is found?
>
> I got a class that is autowired in spring using the @Service annotation
> and where I use:
> @Produce(ref="requestLogger")
> private IRequestLogSender requestLogSender;
>
> Where the interface is:
>
> public interface IRequestLogSender {
> @InOnly
> public void notifyAnalyzer(byte[] requestDoneMessage) throws
> InvalidProtocolBufferException;
> }
>
> I tried to switch the @Produce annotation to use uri instead of ref.
> Also I tried adding @Autowired, but then I get No unique bean of type
> [com.scanmine.scheduler.queuehandler.IRequestLogSender] is defined:
> Unsatisfied dependency of type [interface
> com.scanmine.scheduler.queuehandler.IRequestLogSender]
>
> What are the requirements to get this to work?
The problem turned out to be a simple one:
- make sure you include camel-context.xml in your application context!
- the class must be wired by spring - either though component scanning
or as a bean deined in spring xml.
There might be other problems, the first one of these was mine.
Tarjei
>
--
Regards / Med vennlig hilsen
Tarjei Huse
Mobil: 920 63 413
Re: ProducerTemplate with Spring and without
Posted by Tarjei Huse <ta...@scanmine.com>.
Hi, this is so related that I think it is not thread hijacking.
On 11/18/2010 11:30 AM, Claus Ibsen wrote:
> > @Produce(uri = "direct:start")
> > protected ProducerTemplate template;
What is required for Camel to find and add a proxy where this annotation
is found?
I got a class that is autowired in spring using the @Service annotation
and where I use:
@Produce(ref="requestLogger")
private IRequestLogSender requestLogSender;
Where the interface is:
public interface IRequestLogSender {
@InOnly
public void notifyAnalyzer(byte[] requestDoneMessage) throws
InvalidProtocolBufferException;
}
I tried to switch the @Produce annotation to use uri instead of ref.
Also I tried adding @Autowired, but then I get No unique bean of type
[com.scanmine.scheduler.queuehandler.IRequestLogSender] is defined:
Unsatisfied dependency of type [interface
com.scanmine.scheduler.queuehandler.IRequestLogSender]
What are the requirements to get this to work?
--
Regards / Med vennlig hilsen
Tarjei Huse
Mobil: 920 63 413
Re: ProducerTemplate with Spring and without
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
What JDK, OS and runtime container are you using?
And what Camel version?
I just tried on Camel 2.5 in an unit test and I can set the default endpoint
<camelContext xmlns="http://camel.apache.org/schema/spring">
<template id="camelTemplate" defaultEndpoint="log:foo"/>
</camelContext>
@Autowired
protected ProducerTemplate template;
template.sendBody(body);
On Mon, Nov 15, 2010 at 10:04 PM, serega <se...@sybase.com> wrote:
>
> Hi.
> I've tried examples from here
> http://camel.apache.org/spring-testing.html
>
> Two questions:
> 1. is how do I specify the producer template in spring?
> I've tried the following line
> <template id="test" defaultEndpoint="direct:start"/>
> I got org.apache.camel.NoSuchEndpointException: No endpoint could be found
> for: ref:direct:start
>
> 2. How to I write the following
>
> @Produce(uri = "direct:start")
> protected ProducerTemplate template;
>
> without annotations?
>
> Thanks,
>
> Sergey.
> --
> View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-with-Spring-and-without-tp3266469p3266469.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/