looks good, did you encounter any issue or did you ask only for a review? Note: beans.xml should be empty if already provided by ri jar.
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-08-27 15:41 GMT+02:00 sgjava <[email protected]>: > Your examples all have invokers, etc. but none of the other example I've > seen require this. Since you don't have time to look, maybe you can see if > my structure looks good: > > pom.xml: > > <dependency> > <groupId>org.jsr107.ri</groupId> > <artifactId>cache-annotations-ri-cdi</artifactId> > <version>1.0.0</version> > </dependency> > <dependency> > <groupId>org.ehcache</groupId> > <artifactId>jcache</artifactId> > <version>1.0.1</version> > <scope>compile</scope> > </dependency> > > beans.xml: > > <beans> > <interceptors> > <class>org.jsr107.ri.annotations.cdi.CacheResultInterceptor</class> > > <class>org.jsr107.ri.annotations.cdi.CacheRemoveEntryInterceptor</class> > > <class>org.jsr107.ri.annotations.cdi.CacheRemoveAllInterceptor</class> > <class>org.jsr107.ri.annotations.cdi.CachePutInterceptor</class> > </interceptors> > </beans> > > Cache bean: > > @Singleton > @Startup > @Lock(READ) > public class CacheBean { > > private static final Logger log = Logger. > getLogger(CacheBean.class.getName()); > > /** > * Caching provider. > */ > private CachingProvider cachingProvider; > /** > * Cache manager. > */ > private CacheManager cacheManager; > > public CacheManager getCacheManager() { > return cacheManager; > } > > /** > * Using EHCache provider configured via ehcache.xml in classpath. > */ > //CHECKSTYLE:OFF DesignForExtension > @PostConstruct > //CHECKSTYLE:ON DesignForExtension > public void init() { > log.info("PostConstruct"); > cachingProvider = Caching.getCachingProvider(); > cacheManager = cachingProvider.getCacheManager(); > } > > /** > * Destroy cache. > */ > //CHECKSTYLE:OFF DesignForExtension > @PreDestroy > //CHECKSTYLE:ON DesignForExtension > public void destroy() { > log.info("PreDestroy"); > cacheManager.close(); > cachingProvider.close(); > } > } > > Bean to test annotations: > > @Stateless > @CacheDefaults(cacheName = "testCache") > public class KeyValueBean { > > private static final Logger log = Logger. > getLogger(KeyValueBean.class.getName()); > > @CacheResult > public String add(@CacheKey String key, String value) { > log.info(String.format("Adding key: %s, value: %s", key, value)); > return value; > } > > @CacheRemoveAll > public void invalidateCache() { > log.info("Cache invalidated"); > } > } > > Unit test: > > public class CacheBeanTest { > > /** > * Logger. > */ > private static final Logger log = Logger.getLogger(CacheBeanTest.class. > getName()); > /** > * Injected cache bean. > */ > @EJB > private CacheBean cacheBean; > /** > * Our key/value bean. > */ > @EJB > private KeyValueBean keyValueBean; > > /** > * EJB container. > */ > private static EJBContainer container; > > /** > * Start EJB container. > */ > @BeforeClass > public static void setUpClass() { > } > > /** > * Close caching provider/manager and EJB container. > */ > @AfterClass > public static void tearDownClass() { > } > > /** > * Set up. > */ > @Before > public void setUp() throws NamingException { > log.info("setUp()"); > container = EJBContainer.createEJBContainer(); > container.getContext().bind("inject", this); > } > > /** > * Tear down. > */ > @After > public void tearDown() throws NamingException { > container.getContext().unbind("inject"); > container.close(); > } > > /** > * Test JSR-107 cache using EHCache provider. > */ > @Test > public final void testCache() { > log.info("testCache"); > assertNotNull(cacheBean); > log.info(String.format("Cache names: %s", > cacheBean.getCacheManager(). > getCacheNames())); > keyValueBean.add("key1", "value1"); > final Cache<String, String> testCache = > cacheBean.getCacheManager(). > getCache("testCache"); > log.info(String.format("Cache name: %s", testCache.getName())); > // This should display value1, but it doesn't > log.info(String.format("Value: %s", testCache.get("key1"))); > testCache.close(); > } > } > > > > > > On Thu, Aug 20, 2015 at 8:47 PM, Romain Manni-Bucau [via TomEE & OpenEJB] < > [email protected]> wrote: > > > dont have time to look now but here a sample i wrote which is green > there: > > https://gist.github.com/rmannibucau/323f3de99309f856fe81 > > > > > > 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-08-20 17:43 GMT-07:00 sgjava <[hidden email] > > <http:///user/SendEmail.jtp?type=node&node=4675949&i=0>>: > > > > > I added my JCache attempt in my test project > > > https://github.com/sgjava/my-jaxrs-test > > > > > > I'm not sure the <interceptors> in beans.xml are being loaded or that I > > > have > > > the container set up right for the unit test > > > > > > > > > https://github.com/sgjava/my-jaxrs-test/blob/master/src/test/java/com/codeferm/services/jaxrs/CacheBeanTest.java > > > > > > I'm using the EHCache provider and it fires up fine and can interact > > with > > > it, but CDI doesn't appear to be working. Any help getting the working > > in > > > TomEE 7 is appreciated. > > > > > > > > > > > > > > > -- > > > View this message in context: > > > > > > http://tomee-openejb.979440.n4.nabble.com/JCache-CDI-tp4675943p4675948.html > > > Sent from the TomEE Users mailing list archive at Nabble.com. > > > > > > > > > ------------------------------ > > If you reply to this email, your message will be added to the discussion > > below: > > > http://tomee-openejb.979440.n4.nabble.com/JCache-CDI-tp4675943p4675949.html > > To unsubscribe from JCache CDI, click here > > < > http://tomee-openejb.979440.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4675943&code=c2dqYXZhQGdtYWlsLmNvbXw0Njc1OTQzfC0xNzc0MzgwNjE5 > > > > . > > NAML > > < > http://tomee-openejb.979440.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml > > > > > > > > -- > Steven P. Goldsmith > > > > > -- > View this message in context: > http://tomee-openejb.979440.n4.nabble.com/JCache-CDI-tp4675943p4675981.html > Sent from the TomEE Users mailing list archive at Nabble.com. >
