You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by chege <ch...@programmer.net> on 2019/07/08 07:34:44 UTC
Bootstraping Apache Camel - Main Method
Hi,
I've boostraped apache camel like below.
public static void main(String... args) throws Exception {
Main main = new Main();
main.run(args);
ProducerTemplate template = main.getCamelTemplate();
template.sendBody("direct://k", 2);
}
I have another bean with a method annotated with @Consumes which is
never invoked.
public class MyBean {
@Consume(context = "camel-1",uri="direct://k")
public void k(@Body Integer k) {
System.out.println("This is k " + k);
}
}
How do I get pojo consumer to work?
Thanks,
Chege.
Re: Bootstraping Apache Camel - Main Method
Posted by chege <ch...@programmer.net>.
Works fine :-)
On Mon, 2019-07-08 at 11:19 +0200, Claus Ibsen wrote:
> Just add to the registry after you have created camel context, and
> use
> its injector to create the bean, before you start camel
>
> On Mon, Jul 8, 2019 at 11:15 AM chege <ch...@programmer.net> wrote:
> >
> > Using the injector works fine, what modification is needed to make
> > below code work with the registry only.
> >
> > ...
> > public static void main(String[] args) throws NamingException,
> > Exception {
> >
> >
> > DefaultRegistry registry = new DefaultRegistry();
> > registry.bind("myBean", new MyBean());
> >
> > CamelContext cc = new DefaultCamelContext(registry);
> > cc.start();
> > ProducerTemplate pt = cc.createProducerTemplate();
> > pt.sendBody("direct://k", 19876652);
> > cc.stop();
> > }
> > ...
> >
> >
> > public class MyBean {
> >
> > @Consume(uri="direct://k")
> > public void k(@Body Integer k) {
> > System.out.println("This is k " + k);
> > }
> >
> > }
> >
> > On Mon, 2019-07-08 at 10:31 +0200, Claus Ibsen wrote:
> > > On Mon, Jul 8, 2019 at 10:06 AM chege <ch...@programmer.net>
> > > wrote:
> > > >
> > > > I tried below code but it didn't work.
> > > >
> > > > public class MyConfig {
> > > >
> > > > @BindToRegistry("myBean")
> > > > public MyBean myBean() {
> > > > return new MyBean();
> > > > }
> > > >
> > > > }
> > > >
> > >
> > > Try with
> > >
> > > @...
> > > public MyBean myBean(CamelContext context) {
> > > return context.getInjector().newInstance(MyBean.class);
> > > }
> > >
> > > a)
> > > We could also consider letting Camel Main bean post process these
> > > automatic, so you can just do as you did, and Camel does its
> > > other
> > > magic to detect those @Conume annotations and whatnot
> > >
> > > b)
> > > We could consider having this on fields so you can do
> > >
> > > @BindToRegistry
> > > private MyBean myBean;
> > >
> > > And if it has a default no-arg ctr we create and inject it
> > > automatic
> > > (I frankly can't remember if we dont already have that) if you
> > > add
> > > this to config classes or route builders via main
> > >
> > >
> > > > On Mon, 2019-07-08 at 09:50 +0200, Claus Ibsen wrote:
> > > > > How do you create that bean?
> > > > >
> > > > > You need to create it via Camel to have Camel detect those
> > > > > annotations
> > > > >
> > > > > There is an Injector on CamelContext you can use to create a
> > > > > new
> > > > > instance of the bean
> > > > >
> > > > > On Mon, Jul 8, 2019 at 9:35 AM chege <ch...@programmer.net>
> > > > > wrote:
> > > > > >
> > > > > > Hi,
> > > > > >
> > > > > > I've boostraped apache camel like below.
> > > > > >
> > > > > > public static void main(String... args) throws Exception {
> > > > > > Main main = new Main();
> > > > > > main.run(args);
> > > > > > ProducerTemplate template =
> > > > > > main.getCamelTemplate();
> > > > > > template.sendBody("direct://k", 2);
> > > > > > }
> > > > > >
> > > > > > I have another bean with a method annotated with @Consumes
> > > > > > which is
> > > > > > never invoked.
> > > > > >
> > > > > > public class MyBean {
> > > > > >
> > > > > > @Consume(context = "camel-1",uri="direct://k")
> > > > > > public void k(@Body Integer k) {
> > > > > > System.out.println("This is k " + k);
> > > > > > }
> > > > > >
> > > > > > }
> > > > > >
> > > > > > How do I get pojo consumer to work?
> > > > > >
> > > > > > Thanks,
> > > > > > Chege.
> > > > > >
> > > > >
> > > > >
> > >
> > >
>
>
Re: Bootstraping Apache Camel - Main Method
Posted by Claus Ibsen <cl...@gmail.com>.
Just add to the registry after you have created camel context, and use
its injector to create the bean, before you start camel
On Mon, Jul 8, 2019 at 11:15 AM chege <ch...@programmer.net> wrote:
>
> Using the injector works fine, what modification is needed to make
> below code work with the registry only.
>
> ...
> public static void main(String[] args) throws NamingException,
> Exception {
>
>
> DefaultRegistry registry = new DefaultRegistry();
> registry.bind("myBean", new MyBean());
>
> CamelContext cc = new DefaultCamelContext(registry);
> cc.start();
> ProducerTemplate pt = cc.createProducerTemplate();
> pt.sendBody("direct://k", 19876652);
> cc.stop();
> }
> ...
>
>
> public class MyBean {
>
> @Consume(uri="direct://k")
> public void k(@Body Integer k) {
> System.out.println("This is k " + k);
> }
>
> }
>
> On Mon, 2019-07-08 at 10:31 +0200, Claus Ibsen wrote:
> > On Mon, Jul 8, 2019 at 10:06 AM chege <ch...@programmer.net> wrote:
> > >
> > > I tried below code but it didn't work.
> > >
> > > public class MyConfig {
> > >
> > > @BindToRegistry("myBean")
> > > public MyBean myBean() {
> > > return new MyBean();
> > > }
> > >
> > > }
> > >
> >
> > Try with
> >
> > @...
> > public MyBean myBean(CamelContext context) {
> > return context.getInjector().newInstance(MyBean.class);
> > }
> >
> > a)
> > We could also consider letting Camel Main bean post process these
> > automatic, so you can just do as you did, and Camel does its other
> > magic to detect those @Conume annotations and whatnot
> >
> > b)
> > We could consider having this on fields so you can do
> >
> > @BindToRegistry
> > private MyBean myBean;
> >
> > And if it has a default no-arg ctr we create and inject it automatic
> > (I frankly can't remember if we dont already have that) if you add
> > this to config classes or route builders via main
> >
> >
> > > On Mon, 2019-07-08 at 09:50 +0200, Claus Ibsen wrote:
> > > > How do you create that bean?
> > > >
> > > > You need to create it via Camel to have Camel detect those
> > > > annotations
> > > >
> > > > There is an Injector on CamelContext you can use to create a new
> > > > instance of the bean
> > > >
> > > > On Mon, Jul 8, 2019 at 9:35 AM chege <ch...@programmer.net>
> > > > wrote:
> > > > >
> > > > > Hi,
> > > > >
> > > > > I've boostraped apache camel like below.
> > > > >
> > > > > public static void main(String... args) throws Exception {
> > > > > Main main = new Main();
> > > > > main.run(args);
> > > > > ProducerTemplate template = main.getCamelTemplate();
> > > > > template.sendBody("direct://k", 2);
> > > > > }
> > > > >
> > > > > I have another bean with a method annotated with @Consumes
> > > > > which is
> > > > > never invoked.
> > > > >
> > > > > public class MyBean {
> > > > >
> > > > > @Consume(context = "camel-1",uri="direct://k")
> > > > > public void k(@Body Integer k) {
> > > > > System.out.println("This is k " + k);
> > > > > }
> > > > >
> > > > > }
> > > > >
> > > > > How do I get pojo consumer to work?
> > > > >
> > > > > Thanks,
> > > > > Chege.
> > > > >
> > > >
> > > >
> >
> >
>
--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Re: Bootstraping Apache Camel - Main Method
Posted by chege <ch...@programmer.net>.
Using the injector works fine, what modification is needed to make
below code work with the registry only.
...
public static void main(String[] args) throws NamingException,
Exception {
DefaultRegistry registry = new DefaultRegistry();
registry.bind("myBean", new MyBean());
CamelContext cc = new DefaultCamelContext(registry);
cc.start();
ProducerTemplate pt = cc.createProducerTemplate();
pt.sendBody("direct://k", 19876652);
cc.stop();
}
...
public class MyBean {
@Consume(uri="direct://k")
public void k(@Body Integer k) {
System.out.println("This is k " + k);
}
}
On Mon, 2019-07-08 at 10:31 +0200, Claus Ibsen wrote:
> On Mon, Jul 8, 2019 at 10:06 AM chege <ch...@programmer.net> wrote:
> >
> > I tried below code but it didn't work.
> >
> > public class MyConfig {
> >
> > @BindToRegistry("myBean")
> > public MyBean myBean() {
> > return new MyBean();
> > }
> >
> > }
> >
>
> Try with
>
> @...
> public MyBean myBean(CamelContext context) {
> return context.getInjector().newInstance(MyBean.class);
> }
>
> a)
> We could also consider letting Camel Main bean post process these
> automatic, so you can just do as you did, and Camel does its other
> magic to detect those @Conume annotations and whatnot
>
> b)
> We could consider having this on fields so you can do
>
> @BindToRegistry
> private MyBean myBean;
>
> And if it has a default no-arg ctr we create and inject it automatic
> (I frankly can't remember if we dont already have that) if you add
> this to config classes or route builders via main
>
>
> > On Mon, 2019-07-08 at 09:50 +0200, Claus Ibsen wrote:
> > > How do you create that bean?
> > >
> > > You need to create it via Camel to have Camel detect those
> > > annotations
> > >
> > > There is an Injector on CamelContext you can use to create a new
> > > instance of the bean
> > >
> > > On Mon, Jul 8, 2019 at 9:35 AM chege <ch...@programmer.net>
> > > wrote:
> > > >
> > > > Hi,
> > > >
> > > > I've boostraped apache camel like below.
> > > >
> > > > public static void main(String... args) throws Exception {
> > > > Main main = new Main();
> > > > main.run(args);
> > > > ProducerTemplate template = main.getCamelTemplate();
> > > > template.sendBody("direct://k", 2);
> > > > }
> > > >
> > > > I have another bean with a method annotated with @Consumes
> > > > which is
> > > > never invoked.
> > > >
> > > > public class MyBean {
> > > >
> > > > @Consume(context = "camel-1",uri="direct://k")
> > > > public void k(@Body Integer k) {
> > > > System.out.println("This is k " + k);
> > > > }
> > > >
> > > > }
> > > >
> > > > How do I get pojo consumer to work?
> > > >
> > > > Thanks,
> > > > Chege.
> > > >
> > >
> > >
>
>
Re: Bootstraping Apache Camel - Main Method
Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Jul 8, 2019 at 10:06 AM chege <ch...@programmer.net> wrote:
>
> I tried below code but it didn't work.
>
> public class MyConfig {
>
> @BindToRegistry("myBean")
> public MyBean myBean() {
> return new MyBean();
> }
>
> }
>
Try with
@...
public MyBean myBean(CamelContext context) {
return context.getInjector().newInstance(MyBean.class);
}
a)
We could also consider letting Camel Main bean post process these
automatic, so you can just do as you did, and Camel does its other
magic to detect those @Conume annotations and whatnot
b)
We could consider having this on fields so you can do
@BindToRegistry
private MyBean myBean;
And if it has a default no-arg ctr we create and inject it automatic
(I frankly can't remember if we dont already have that) if you add
this to config classes or route builders via main
> On Mon, 2019-07-08 at 09:50 +0200, Claus Ibsen wrote:
> > How do you create that bean?
> >
> > You need to create it via Camel to have Camel detect those
> > annotations
> >
> > There is an Injector on CamelContext you can use to create a new
> > instance of the bean
> >
> > On Mon, Jul 8, 2019 at 9:35 AM chege <ch...@programmer.net> wrote:
> > >
> > > Hi,
> > >
> > > I've boostraped apache camel like below.
> > >
> > > public static void main(String... args) throws Exception {
> > > Main main = new Main();
> > > main.run(args);
> > > ProducerTemplate template = main.getCamelTemplate();
> > > template.sendBody("direct://k", 2);
> > > }
> > >
> > > I have another bean with a method annotated with @Consumes which is
> > > never invoked.
> > >
> > > public class MyBean {
> > >
> > > @Consume(context = "camel-1",uri="direct://k")
> > > public void k(@Body Integer k) {
> > > System.out.println("This is k " + k);
> > > }
> > >
> > > }
> > >
> > > How do I get pojo consumer to work?
> > >
> > > Thanks,
> > > Chege.
> > >
> >
> >
>
--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Re: Bootstraping Apache Camel - Main Method
Posted by chege <ch...@programmer.net>.
I tried below code but it didn't work.
public class MyConfig {
@BindToRegistry("myBean")
public MyBean myBean() {
return new MyBean();
}
}
On Mon, 2019-07-08 at 09:50 +0200, Claus Ibsen wrote:
> How do you create that bean?
>
> You need to create it via Camel to have Camel detect those
> annotations
>
> There is an Injector on CamelContext you can use to create a new
> instance of the bean
>
> On Mon, Jul 8, 2019 at 9:35 AM chege <ch...@programmer.net> wrote:
> >
> > Hi,
> >
> > I've boostraped apache camel like below.
> >
> > public static void main(String... args) throws Exception {
> > Main main = new Main();
> > main.run(args);
> > ProducerTemplate template = main.getCamelTemplate();
> > template.sendBody("direct://k", 2);
> > }
> >
> > I have another bean with a method annotated with @Consumes which is
> > never invoked.
> >
> > public class MyBean {
> >
> > @Consume(context = "camel-1",uri="direct://k")
> > public void k(@Body Integer k) {
> > System.out.println("This is k " + k);
> > }
> >
> > }
> >
> > How do I get pojo consumer to work?
> >
> > Thanks,
> > Chege.
> >
>
>
Re: Bootstraping Apache Camel - Main Method
Posted by Claus Ibsen <cl...@gmail.com>.
How do you create that bean?
You need to create it via Camel to have Camel detect those annotations
There is an Injector on CamelContext you can use to create a new
instance of the bean
On Mon, Jul 8, 2019 at 9:35 AM chege <ch...@programmer.net> wrote:
>
> Hi,
>
> I've boostraped apache camel like below.
>
> public static void main(String... args) throws Exception {
> Main main = new Main();
> main.run(args);
> ProducerTemplate template = main.getCamelTemplate();
> template.sendBody("direct://k", 2);
> }
>
> I have another bean with a method annotated with @Consumes which is
> never invoked.
>
> public class MyBean {
>
> @Consume(context = "camel-1",uri="direct://k")
> public void k(@Body Integer k) {
> System.out.println("This is k " + k);
> }
>
> }
>
> How do I get pojo consumer to work?
>
> Thanks,
> Chege.
>
--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2