Nerot
Scheduled Caching Made Simple
Add this dependency to your pom.xml:<dependency> <groupId>nerot</groupId> <artifactId>nerot</artifactId> <version>3.4</version> </dependency>Add this repository to your pom.xml:
<repository> <id>nerot-repository</id> <name>Nerot Repository</name> <url>http://garysweaver.github.com/nerot/m2/releases</url> </repository>Add the bean definitions in nerot.xml to your application's context:
<bean name="nerotScheduleFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="waitForJobsToCompleteOnShutdown" value="false"/> </bean> <bean name="nerotStore" class="nerot.store.MemoryStore" /> <bean name="nerot" class="nerot.Nerot"> <property name="scheduler" ref="nerotScheduleFactory"/> <property name="store" ref="nerotStore"/> </bean>Add Nerot to your class:
import nerot.Nerot; public class SomeClass { private Nerot nerot; public void setNerot(Nerot nerot) { this.nerot = nerot; } }
<bean name="someClass" class="SomeClass"> <property name="nerot" ref="nerot"/> </bean>
Defining a Schedule is Simple and Flexible
You can schedule refresh of the cache either via interval in milliseconds or Quartz CronTrigger syntax (similar to cron syntax).
RSS Feed Retrieval
// Get RSS feed every 15 seconds and cache if no error nerot.scheduleRss(url, "0/15 * * * * ?"); // Retrieve the result from in-memory cache without delay SyndFeed feed = nerot.getRssFromStore(url);
Webpage Retrieval
// Get webpage every 60 seconds and cache if no error nerot.scheduleHttpGet(url, 60000L); // Retrieve the result from in-memory cache without delay String page = nerot.getHttpResponseBodyFromStore(url);
Static Methods
// Define Math.random() GenericTask task = new GenericTask(); task.setStoreKey(key); task.setActor(Math.class); task.setMethod("random"); // Call Math.random() every 15 seconds nerot.schedule("myJob", task, 15000L); // Retrieve the result from in-memory cache without delay Double random = (Double)nerot.getResultFromStore(key);
Instance Methods
// Define reporter.generate(...) GenericTask task = new GenericTask(); task.setStoreKey(key); task.setActor(new Reporter()); task.setMethod("generate"); task.setArgs(new Object[]{"s", y, true, 'c', 1.0D}); // Call report.generate(...) at 9:15pm every day nerot.schedule("myJob", task, "0 15 21 * * ?"); // Retrieve the result from in-memory cache without delay Report stats = (Report)nerot.getResultFromStore(key);
Schedule Methods via Spring Beans
Nerot includes classes for each type of schedule comment that schedule Nerot when the Spring bean is configured in the context. Just define a bean in the root application context and then get the result programmatically in the child context.<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <bean name="rssCronScheduler" class="nerot.spring.RssCronScheduler"> <property name="url" value="http://news.acme.com/rss"/> <property name="cronSchedule" value="0/1 * * * * ?"/> </bean> </beans>
@Autowired Nerot nerot; // Schedulers are Storers, so they provide a getStoreKey() method @Autowired Storer storer; // Retrieve the result from in-memory cache without delay SyndFeed feed = nerot.getRssFromStore(storer.getStoreKey());