tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Goldsmith <sgj...@gmail.com>
Subject Re: JCache annotations in web app test
Date Tue, 01 Sep 2015 16:11:28 GMT
OK, I saw a ehcache servlet as well. I don't mind using a bean since this
decouples the logic from the service any ways. Plus it can also be used in
the stand alone EJBContainer.

On Tue, Sep 1, 2015 at 12:06 PM, Romain Manni-Bucau <rmannibucau@gmail.com>
wrote:

> they can if they are cdi beans ;)
>
> side note: jcs extra module has a cache filter for servlet, avoids even the
> jaxrs stack to be triggered once setup ;)
>
>
> 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-09-01 18:04 GMT+02:00 Steve Goldsmith <sgjava@gmail.com>:
>
> > So can services have JCache annotations? It would be cool to cache
> > responses too, but using just an entity wouldn't provide a proper key?
> >
> > On Tue, Sep 1, 2015 at 11:54 AM, Romain Manni-Bucau <
> rmannibucau@gmail.com
> > >
> > wrote:
> >
> > > Ah get it, if your jaxrs endpoint a CDI bean? Think if it is @Dependent
> > > then it will not be considered as a CDI bean but a plain pojo with
> > > injections
> > >
> > >
> > > 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-09-01 17:53 GMT+02:00 Steve Goldsmith <sgjava@gmail.com>:
> > >
> > > > No, in the actual service. If I inject a bean in the service and use
> > the
> > > > bean for caching it works. If I add an annotated method to the
> service
> > I
> > > > get the NPE trying to get the cache.
> > > >
> > > > On Tue, Sep 1, 2015 at 11:50 AM, Romain Manni-Bucau <
> > > rmannibucau@gmail.com
> > > > >
> > > > wrote:
> > > >
> > > > > this code is in the test? classloader is not the webapp one then.
> > > > >
> > > > >
> > > > > 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-09-01 16:59 GMT+02:00 Steve Goldsmith <sgjava@gmail.com>:
> > > > >
> > > > > > OK, I have JCache annotations working with tomee-embedded using
> an
> > > > > injected
> > > > > > bean. If I try to cache something in the service:
> > > > > >
> > > > > >     @CacheResult(cacheName = "testCache")
> > > > > >     public String slowMethod(@CacheKey String key, String value)
> {
> > > > > >         log.info(String.format("Key: %s, value: %s added to
> > cache",
> > > > key,
> > > > > > value));
> > > > > >         return value;
> > > > > >     }
> > > > > >
> > > > > > Then in the service if I try to access the cache:
> > > > > >
> > > > > >         final Cache<String, String> testCache =
> > > > > > cacheBean.getCacheManager().
> > > > > >                 getCache("testCache");
> > > > > >
> > > > > > I get an NPE. The same code works if I use an EJB vs. a JAX-RS
> > > service.
> > > > > >
> > > > > >
> > > > > >
> > > > > > On Tue, Sep 1, 2015 at 2:24 AM, Romain Manni-Bucau <
> > > > > rmannibucau@gmail.com>
> > > > > > wrote:
> > > > > >
> > > > > > > Hi
> > > > > > >
> > > > > > > Le 1 sept. 2015 04:01, "sgjava" <sgjava@gmail.com>
a écrit :
> > > > > > > >
> > > > > > > > I have JCache annotations working with a unit test:
> > > > > > > >
> > > > > > > > KeyValueBean using @CacheDefaults(cacheName = "testCache")
> > > > > > > >
> > > > > > > >     @CacheResult
> > > > > > > >     public String add(@CacheKey String key, String
value) {
> > > > > > > >         log.info(String.format("Adding key: %s, value:
%s",
> > key,
> > > > > > > value));
> > > > > > > >         return value;
> > > > > > > >     }
> > > > > > > >
> > > > > > > > Test:
> > > > > > > >
> > > > > > > >     /**
> > > > > > > >      * Injected cache bean.
> > > > > > > >      */
> > > > > > > >     @EJB
> > > > > > > >     private CacheBean cacheBean;
> > > > > > > >     /**
> > > > > > > >      * Our key/value bean.
> > > > > > > >      */
> > > > > > > >     @EJB
> > > > > > > >     private KeyValueBean keyValueBean;
> > > > > > > >
> > > > > > > >         container = EJBContainer.createEJBContainer();
> > > > > > > >         container.getContext().bind("inject", this);
> > > > > > > >
> > > > > > > >     keyValueBean.add("key1", "value1");
> > > > > > > >
> > > > > > > > INFO: Key:
> > > > org.jsr107.ri.annotations.DefaultGeneratedCacheKey@322dd1
> > > > > ,
> > > > > > > Value:
> > > > > > > > value1
> > > > > > > >
> > > > > > > > But using a web test:
> > > > > > > >
> > > > > > > >         final Map p = new HashMap();
> > > > > > > >         p.put(Context.INITIAL_CONTEXT_FACTORY,
> > > > > > > >
> > > > >  "org.apache.openejb.core.LocalInitialContextFactory");
> > > > > > > >         p.put("openejb.embedded.initialcontext.close
",
> > > "DESTROY");
> > > > > > > >         p.put("openejb.embedded.remotable", "true");
> > > > > > >
> > > > > > > The 3 previous props are for openejb embedded and not
> > > tomee-embedded
> > > > > > >
> > > > > > > >         p.put(EJBContainer.APP_NAME, "my-jaxrs-test");
> > > > > > > >         p.put(EJBContainer.PROVIDER, "tomee-embedded");
> > > > > > > >         // Add WAR and MDB modules
> > > > > > > >         p.put(EJBContainer.MODULES, new
> > > > > > File[]{Archive.archive().copyTo(
> > > > > > > >             "WEB-INF/classes",
> > > > > > jarLocation(UserService.class)).asDir()});
> > > > > > >
> > > > > > > Dont you miss a beans.xml?
> > > > > > >
> > > > > > > >         // Random port
> > > > > > > >
> > >  p.put(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT,
> > > > > > "-1");
> > > > > > > >         container = EJBContainer.createEJBContainer(p);
> > > > > > > >
> > > > > > > > And a service:
> > > > > > > >
> > > > > > > >     /**
> > > > > > > >      * Injected cache bean.
> > > > > > > >      */
> > > > > > > >     @EJB
> > > > > > > >     private CacheBean cacheBean;
> > > > > > > >     /**
> > > > > > > >      * Our key/value bean.
> > > > > > > >      */
> > > > > > > >     @EJB
> > > > > > > >     private KeyValueBean keyValueBean;
> > > > > > > >
> > > > > > > > The annotations do not work, but I can see cache inside
the
> > > service
> > > > > > > class:
> > > > > > > >
> > > > > > > >         log.info(String.format("Cache names: %s",
> > > > > > > > cacheBean.getCacheManager().
> > > > > > > >                 getCacheNames()));
> > > > > > > >
> > > > > > > > INFO: Cache names: [testCache]
> > > > > > > >
> > > > > > > > Is this a scope or class loader issue with the test
> container?
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > --
> > > > > > > > View this message in context:
> > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
> http://tomee-openejb.979440.n4.nabble.com/JCache-annotations-in-web-app-test-tp4676034.html
> > > > > > > > Sent from the TomEE Users mailing list archive at
Nabble.com.
> > > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Steven P. Goldsmith
> > > > > >
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Steven P. Goldsmith
> > > >
> > >
> >
> >
> >
> > --
> > Steven P. Goldsmith
> >
>



-- 
Steven P. Goldsmith

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message