Created
February 27, 2014 16:00
-
-
Save nyilmaz/9252970 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pipe.web.common.config; | |
import org.springframework.beans.BeansException; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.config.BeanPostProcessor; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Import; | |
import org.springframework.scheduling.TaskScheduler; | |
import org.springframework.scheduling.config.IntervalTask; | |
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | |
import org.springframework.util.ReflectionUtils; | |
import javax.annotation.PostConstruct; | |
import java.lang.annotation.*; | |
import java.lang.reflect.Method; | |
import java.util.Map; | |
/** | |
* @author nyilmaz | |
*/ | |
@Target(ElementType.TYPE) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Documented | |
@Import(EnableReloading.ReloadingConfiguration.class) | |
public @interface EnableReloading { | |
public long rate() default 0L; | |
@Configuration | |
static class ReloadingConfiguration implements BeanPostProcessor { | |
@Autowired | |
ApplicationContext applicationContext; | |
@Autowired | |
TaskScheduler taskScheduler; | |
private ScheduledTaskRegistrar registrar; | |
@PostConstruct | |
public void setup () { | |
registrar = new ScheduledTaskRegistrar(); | |
registrar.setScheduler(taskScheduler); | |
} | |
@Override | |
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
return bean; | |
} | |
@Override | |
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
EnableReloading annotationOnBean = applicationContext.findAnnotationOnBean(beanName, EnableReloading.class); | |
if(annotationOnBean != null && bean instanceof Reloadable) { | |
final Reloadable reloadable = (Reloadable) bean; | |
registrar.addFixedRateTask(new Runnable() { | |
@Override | |
public void run() { | |
reloadable.reload(); | |
} | |
}, annotationOnBean.rate()); | |
} | |
} | |
} | |
public interface Reloadable { | |
public Boolean isReloadRequired(); | |
public void reload(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment