You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Alex Soto <as...@gmail.com> on 2015/06/02 12:16:38 UTC

AbstractRouter and CDI

Hi I am developing a Dynamic datasource routing component. I am using
OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are not CDI
aware.  Basically I have tried to inject a simple POJO but the instance is
null. I have the beans.xml in src/main/resources/META-INF so CDI is
enabled.

Maybe this inner components are no CDI by default, any ideas?

Re: AbstractRouter and CDI

Posted by Romain Manni-Bucau <rm...@gmail.com>.
the instance is not a cdi bean but get cdi injections (like JBatch
components for instance).

You can do this:

public class Router ... {
  @Inject Listener listener;

    @PostConstruct
    public void init() {
        listener.triggerMe(this);
    }

  public void listen(Sthg sthg) {
  }

  @AppScoped
  public static class Listener {
       private AtomicRef<Router> enclosing = new AtomicRef<>();
       public void listen(@Observes Sthg sthg) {
              enclosing.get().onEvent(sthg);
       }

      public void triggerMe(Router router) {
            enclosing.set(router);
      }
  }
}



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-06-03 8:43 GMT-07:00 Alex Soto <as...@gmail.com>:

> Last problem before I can send a summary of what I have done for
> implementing multitenancy. I have created an event which should be used for
> registering new tenants dynamically. And I have created an @Observes method
> in Router. The surprise is that the instance that is registered initially
> and the once it receives the event are different. I have annotated the
> router as @ApplicationScope but no luck.
> Any idea?
> Alex.
>
> El dc., 3 juny 2015 a les 14:55, Alex Soto (<as...@gmail.com>) va
> escriure:
>
> > Now it works :D Now I will be able to send CDI events from one jar and
> > observes here the event and register a new tenant :D Thank you so much.
> >
> > El dc., 3 juny 2015 a les 14:37, Romain Manni-Bucau (<
> > rmannibucau@gmail.com>) va escriure:
> >
> >> just qdd InitializeAfterDeployment = true in the router definition
> >>
> >>
> >> Romain Manni-Bucau
> >> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> >> <http://rmannibucau.wordpress.com> | Github <
> >> https://github.com/rmannibucau> |
> >> LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> >> <http://www.tomitribe.com>
> >>
> >> 2015-06-03 13:55 GMT+02:00 Alex Soto <as...@gmail.com>:
> >>
> >> > Ok I have created a resources.xml and everything works perfectly, but
> >> > Person instance is still not injected and its value is null.
> >> >
> >> > Latest code is here: https://github.com/lordofthejars/multi
> >> >
> >> >
> >> > El dt., 2 juny 2015 a les 16:50, Romain Manni-Bucau (<
> >> > rmannibucau@gmail.com>)
> >> > va escriure:
> >> >
> >> > > 3 notes:
> >> > >
> >> > > 1. if you let hibernate use the metadata at startup then ensure you
> >> have
> >> > a
> >> > > default datasource or just switch it off: <property
> >> > > name="hibernate.temp.use_jdbc_metadata_defaults"
> >> > > value="false" />
> >> > > 2. the injections are not working cause you use a container resource
> >> (no
> >> > > application), use resources.xml to get it activated
> >> > > 3. you dont need a server-jar.xml: properties.setProperty("My
> Router",
> >> > > "new://Resource?class-name=com.scytl.multi.DeterminedRouter");
> >> > >
> >> > >
> >> > > Romain Manni-Bucau
> >> > > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> >> > > <http://rmannibucau.wordpress.com> | Github <
> >> > > https://github.com/rmannibucau> |
> >> > > LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> >> > > <http://www.tomitribe.com>
> >> > >
> >> > > 2015-06-02 16:35 GMT+02:00 Alex Soto <as...@gmail.com>:
> >> > >
> >> > > > Yes I am using 4.7.2, but it is not working. You can try it, the
> >> > project
> >> > > is
> >> > > > here: https://github.com/lordofthejars/multi
> >> > > >
> >> > > > El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<
> >> > > > rmannibucau@gmail.com>)
> >> > > > va escriure:
> >> > > >
> >> > > > > Hi Alex
> >> > > > >
> >> > > > > Use 4.7.2
> >> > > > > Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
> >> > > > >
> >> > > > > > Hi I am developing a Dynamic datasource routing component. I
> am
> >> > using
> >> > > > > > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes
> >> are
> >> > not
> >> > > > CDI
> >> > > > > > aware.  Basically I have tried to inject a simple POJO but the
> >> > > instance
> >> > > > > is
> >> > > > > > null. I have the beans.xml in src/main/resources/META-INF so
> >> CDI is
> >> > > > > > enabled.
> >> > > > > >
> >> > > > > > Maybe this inner components are no CDI by default, any ideas?
> >> > > > > >
> >> > > > >
> >> > > >
> >> > >
> >> >
> >>
> >
>

Re: AbstractRouter and CDI

Posted by Alex Soto <as...@gmail.com>.
Last problem before I can send a summary of what I have done for
implementing multitenancy. I have created an event which should be used for
registering new tenants dynamically. And I have created an @Observes method
in Router. The surprise is that the instance that is registered initially
and the once it receives the event are different. I have annotated the
router as @ApplicationScope but no luck.
Any idea?
Alex.

El dc., 3 juny 2015 a les 14:55, Alex Soto (<as...@gmail.com>) va
escriure:

> Now it works :D Now I will be able to send CDI events from one jar and
> observes here the event and register a new tenant :D Thank you so much.
>
> El dc., 3 juny 2015 a les 14:37, Romain Manni-Bucau (<
> rmannibucau@gmail.com>) va escriure:
>
>> just qdd InitializeAfterDeployment = true in the router definition
>>
>>
>> Romain Manni-Bucau
>> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
>> <http://rmannibucau.wordpress.com> | Github <
>> https://github.com/rmannibucau> |
>> LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
>> <http://www.tomitribe.com>
>>
>> 2015-06-03 13:55 GMT+02:00 Alex Soto <as...@gmail.com>:
>>
>> > Ok I have created a resources.xml and everything works perfectly, but
>> > Person instance is still not injected and its value is null.
>> >
>> > Latest code is here: https://github.com/lordofthejars/multi
>> >
>> >
>> > El dt., 2 juny 2015 a les 16:50, Romain Manni-Bucau (<
>> > rmannibucau@gmail.com>)
>> > va escriure:
>> >
>> > > 3 notes:
>> > >
>> > > 1. if you let hibernate use the metadata at startup then ensure you
>> have
>> > a
>> > > default datasource or just switch it off: <property
>> > > name="hibernate.temp.use_jdbc_metadata_defaults"
>> > > value="false" />
>> > > 2. the injections are not working cause you use a container resource
>> (no
>> > > application), use resources.xml to get it activated
>> > > 3. you dont need a server-jar.xml: properties.setProperty("My Router",
>> > > "new://Resource?class-name=com.scytl.multi.DeterminedRouter");
>> > >
>> > >
>> > > Romain Manni-Bucau
>> > > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
>> > > <http://rmannibucau.wordpress.com> | Github <
>> > > https://github.com/rmannibucau> |
>> > > LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
>> > > <http://www.tomitribe.com>
>> > >
>> > > 2015-06-02 16:35 GMT+02:00 Alex Soto <as...@gmail.com>:
>> > >
>> > > > Yes I am using 4.7.2, but it is not working. You can try it, the
>> > project
>> > > is
>> > > > here: https://github.com/lordofthejars/multi
>> > > >
>> > > > El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<
>> > > > rmannibucau@gmail.com>)
>> > > > va escriure:
>> > > >
>> > > > > Hi Alex
>> > > > >
>> > > > > Use 4.7.2
>> > > > > Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
>> > > > >
>> > > > > > Hi I am developing a Dynamic datasource routing component. I am
>> > using
>> > > > > > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes
>> are
>> > not
>> > > > CDI
>> > > > > > aware.  Basically I have tried to inject a simple POJO but the
>> > > instance
>> > > > > is
>> > > > > > null. I have the beans.xml in src/main/resources/META-INF so
>> CDI is
>> > > > > > enabled.
>> > > > > >
>> > > > > > Maybe this inner components are no CDI by default, any ideas?
>> > > > > >
>> > > > >
>> > > >
>> > >
>> >
>>
>

Re: AbstractRouter and CDI

Posted by Alex Soto <as...@gmail.com>.
Now it works :D Now I will be able to send CDI events from one jar and
observes here the event and register a new tenant :D Thank you so much.

El dc., 3 juny 2015 a les 14:37, Romain Manni-Bucau (<rm...@gmail.com>)
va escriure:

> just qdd InitializeAfterDeployment = true in the router definition
>
>
> Romain Manni-Bucau
> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> <http://rmannibucau.wordpress.com> | Github <
> https://github.com/rmannibucau> |
> LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> <http://www.tomitribe.com>
>
> 2015-06-03 13:55 GMT+02:00 Alex Soto <as...@gmail.com>:
>
> > Ok I have created a resources.xml and everything works perfectly, but
> > Person instance is still not injected and its value is null.
> >
> > Latest code is here: https://github.com/lordofthejars/multi
> >
> >
> > El dt., 2 juny 2015 a les 16:50, Romain Manni-Bucau (<
> > rmannibucau@gmail.com>)
> > va escriure:
> >
> > > 3 notes:
> > >
> > > 1. if you let hibernate use the metadata at startup then ensure you
> have
> > a
> > > default datasource or just switch it off: <property
> > > name="hibernate.temp.use_jdbc_metadata_defaults"
> > > value="false" />
> > > 2. the injections are not working cause you use a container resource
> (no
> > > application), use resources.xml to get it activated
> > > 3. you dont need a server-jar.xml: properties.setProperty("My Router",
> > > "new://Resource?class-name=com.scytl.multi.DeterminedRouter");
> > >
> > >
> > > Romain Manni-Bucau
> > > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> > > <http://rmannibucau.wordpress.com> | Github <
> > > https://github.com/rmannibucau> |
> > > LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> > > <http://www.tomitribe.com>
> > >
> > > 2015-06-02 16:35 GMT+02:00 Alex Soto <as...@gmail.com>:
> > >
> > > > Yes I am using 4.7.2, but it is not working. You can try it, the
> > project
> > > is
> > > > here: https://github.com/lordofthejars/multi
> > > >
> > > > El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<
> > > > rmannibucau@gmail.com>)
> > > > va escriure:
> > > >
> > > > > Hi Alex
> > > > >
> > > > > Use 4.7.2
> > > > > Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
> > > > >
> > > > > > Hi I am developing a Dynamic datasource routing component. I am
> > using
> > > > > > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are
> > not
> > > > CDI
> > > > > > aware.  Basically I have tried to inject a simple POJO but the
> > > instance
> > > > > is
> > > > > > null. I have the beans.xml in src/main/resources/META-INF so CDI
> is
> > > > > > enabled.
> > > > > >
> > > > > > Maybe this inner components are no CDI by default, any ideas?
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: AbstractRouter and CDI

Posted by Romain Manni-Bucau <rm...@gmail.com>.
just qdd InitializeAfterDeployment = true in the router definition


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-06-03 13:55 GMT+02:00 Alex Soto <as...@gmail.com>:

> Ok I have created a resources.xml and everything works perfectly, but
> Person instance is still not injected and its value is null.
>
> Latest code is here: https://github.com/lordofthejars/multi
>
>
> El dt., 2 juny 2015 a les 16:50, Romain Manni-Bucau (<
> rmannibucau@gmail.com>)
> va escriure:
>
> > 3 notes:
> >
> > 1. if you let hibernate use the metadata at startup then ensure you have
> a
> > default datasource or just switch it off: <property
> > name="hibernate.temp.use_jdbc_metadata_defaults"
> > value="false" />
> > 2. the injections are not working cause you use a container resource (no
> > application), use resources.xml to get it activated
> > 3. you dont need a server-jar.xml: properties.setProperty("My Router",
> > "new://Resource?class-name=com.scytl.multi.DeterminedRouter");
> >
> >
> > Romain Manni-Bucau
> > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> > <http://rmannibucau.wordpress.com> | Github <
> > https://github.com/rmannibucau> |
> > LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> > <http://www.tomitribe.com>
> >
> > 2015-06-02 16:35 GMT+02:00 Alex Soto <as...@gmail.com>:
> >
> > > Yes I am using 4.7.2, but it is not working. You can try it, the
> project
> > is
> > > here: https://github.com/lordofthejars/multi
> > >
> > > El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<
> > > rmannibucau@gmail.com>)
> > > va escriure:
> > >
> > > > Hi Alex
> > > >
> > > > Use 4.7.2
> > > > Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
> > > >
> > > > > Hi I am developing a Dynamic datasource routing component. I am
> using
> > > > > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are
> not
> > > CDI
> > > > > aware.  Basically I have tried to inject a simple POJO but the
> > instance
> > > > is
> > > > > null. I have the beans.xml in src/main/resources/META-INF so CDI is
> > > > > enabled.
> > > > >
> > > > > Maybe this inner components are no CDI by default, any ideas?
> > > > >
> > > >
> > >
> >
>

Re: AbstractRouter and CDI

Posted by Alex Soto <as...@gmail.com>.
Ok I have created a resources.xml and everything works perfectly, but
Person instance is still not injected and its value is null.

Latest code is here: https://github.com/lordofthejars/multi


El dt., 2 juny 2015 a les 16:50, Romain Manni-Bucau (<rm...@gmail.com>)
va escriure:

> 3 notes:
>
> 1. if you let hibernate use the metadata at startup then ensure you have a
> default datasource or just switch it off: <property
> name="hibernate.temp.use_jdbc_metadata_defaults"
> value="false" />
> 2. the injections are not working cause you use a container resource (no
> application), use resources.xml to get it activated
> 3. you dont need a server-jar.xml: properties.setProperty("My Router",
> "new://Resource?class-name=com.scytl.multi.DeterminedRouter");
>
>
> Romain Manni-Bucau
> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> <http://rmannibucau.wordpress.com> | Github <
> https://github.com/rmannibucau> |
> LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> <http://www.tomitribe.com>
>
> 2015-06-02 16:35 GMT+02:00 Alex Soto <as...@gmail.com>:
>
> > Yes I am using 4.7.2, but it is not working. You can try it, the project
> is
> > here: https://github.com/lordofthejars/multi
> >
> > El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<
> > rmannibucau@gmail.com>)
> > va escriure:
> >
> > > Hi Alex
> > >
> > > Use 4.7.2
> > > Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
> > >
> > > > Hi I am developing a Dynamic datasource routing component. I am using
> > > > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are not
> > CDI
> > > > aware.  Basically I have tried to inject a simple POJO but the
> instance
> > > is
> > > > null. I have the beans.xml in src/main/resources/META-INF so CDI is
> > > > enabled.
> > > >
> > > > Maybe this inner components are no CDI by default, any ideas?
> > > >
> > >
> >
>

Re: AbstractRouter and CDI

Posted by Romain Manni-Bucau <rm...@gmail.com>.
3 notes:

1. if you let hibernate use the metadata at startup then ensure you have a
default datasource or just switch it off: <property
name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
2. the injections are not working cause you use a container resource (no
application), use resources.xml to get it activated
3. you dont need a server-jar.xml: properties.setProperty("My Router",
"new://Resource?class-name=com.scytl.multi.DeterminedRouter");


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-06-02 16:35 GMT+02:00 Alex Soto <as...@gmail.com>:

> Yes I am using 4.7.2, but it is not working. You can try it, the project is
> here: https://github.com/lordofthejars/multi
>
> El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<
> rmannibucau@gmail.com>)
> va escriure:
>
> > Hi Alex
> >
> > Use 4.7.2
> > Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
> >
> > > Hi I am developing a Dynamic datasource routing component. I am using
> > > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are not
> CDI
> > > aware.  Basically I have tried to inject a simple POJO but the instance
> > is
> > > null. I have the beans.xml in src/main/resources/META-INF so CDI is
> > > enabled.
> > >
> > > Maybe this inner components are no CDI by default, any ideas?
> > >
> >
>

Re: AbstractRouter and CDI

Posted by Alex Soto <as...@gmail.com>.
Yes I am using 4.7.2, but it is not working. You can try it, the project is
here: https://github.com/lordofthejars/multi

El dt., 2 juny 2015 a les 16:13, Romain Manni-Bucau (<rm...@gmail.com>)
va escriure:

> Hi Alex
>
> Use 4.7.2
> Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :
>
> > Hi I am developing a Dynamic datasource routing component. I am using
> > OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are not CDI
> > aware.  Basically I have tried to inject a simple POJO but the instance
> is
> > null. I have the beans.xml in src/main/resources/META-INF so CDI is
> > enabled.
> >
> > Maybe this inner components are no CDI by default, any ideas?
> >
>

Re: AbstractRouter and CDI

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi Alex

Use 4.7.2
Le 2 juin 2015 03:16, "Alex Soto" <as...@gmail.com> a écrit :

> Hi I am developing a Dynamic datasource routing component. I am using
> OpenEJB 4.7.1 for testing and I have seen that AbstractRoutes are not CDI
> aware.  Basically I have tried to inject a simple POJO but the instance is
> null. I have the beans.xml in src/main/resources/META-INF so CDI is
> enabled.
>
> Maybe this inner components are no CDI by default, any ideas?
>