我创建了一个Spring方面来处理Retry机制。我还创建了一个Retry注释。以下是重试注释的代码以及处理此注释的方面。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Retry {
/**
* List of exceptions for which we need to retry method invocation.
*
* @return Array of classes.
*/
Class<?>[] exceptions();
/**
* Number of retries. Default is 3.
*
* @return Number of retires.
*/
int retries() default 3;
/**
* Back of period in ms. Default is 1000 ms.
*
* @return Back off Period.
*/
int backOffPeriod() default 1000;
}
@Aspect
public class RetryInterceptor implements Ordered {
private static final RetryInterceptor instance = new RetryInterceptor();
private RetryInterceptor() {
}
private static final Log logger = LogFactory.getLog(RetryInterceptor.class);
private int order = 100;
@Around("@annotation(retry)")
public Object performOperation(ProceedingJoinPoint pjp, Retry retry) throws Throwable {
Class<?>[] exceptionClasses = retry.exceptions();
Assert.notEmpty(exceptionClasses, "Exception classes cannot be empty.");
int retries = retry.retries();
if (logger.isInfoEnabled()) {
logger.info("Attempting to call " + pjp.toShortString() + " with potential for " + getExceptionClasses(exceptionClasses)
+ " with maximum " + retries + " retries");
}
int numAttempts = 0;
do {
try {
return pjp.proceed();
} catch (Throwable ex) {
// if the exception is not what we're looking for, pass it through
boolean canThrowException = true;
for (Class<?> exceptionClass : exceptionClasses) {
if (exceptionClass.isAssignableFrom(ex.getClass())) {
canThrowException = false;
break;
}
}
// A non-configured exception was found.
if (canThrowException) {
throw ex;
}
// we caught the configured exception, retry unless we've reached the maximum
if (++numAttempts > retries) {
logger.warn("Caught " + ex.getClass().getCanonicalName() + " and exceeded maximum retries (" + retries
+ "), rethrowing.");
throw ex;
}
if (logger.isInfoEnabled()) {
logger.info("Caught " + ex.getClass().getCanonicalName() + " and will retry, attempts: " + numAttempts);
}
}
sleep(retry.backOffPeriod());
} while (numAttempts <= retries);
// this will never execute - we will have either successfully returned or re-thrown an
// exception
return null;
}
@Override
public int getOrder() {
return order;
}
private String getExceptionClasses(Class<?>[] classes) {
StringBuilder builder = new StringBuilder();
builder.append(classes[0].getCanonicalName());
for (int i = 1; i < classes.length; i++) {
builder.append(", ").append(classes[i].getCanonicalName());
}
return builder.toString();
}
public static RetryInterceptor getInstance() {
return instance;
}
// Better than Thread.sleep().
public void sleep(long backOffPeriod) throws InterruptedException {
Object mutex = new Object();
synchronized (mutex) {
mutex.wait(backOffPeriod);
}
}
}
要启用注释,我需要实例化RetryInterceptor类。我要确保对于给定的上下文,只有该对象的一个实例。如果由于某种原因创建了多个对象,那么我的建议将被应用多次。我如何才能完全确保始终有1个实例?
我找到了一种方法:)引用:超越DI
我在我的根上下文中注册了BeanDefinitionRegistryPostProcessor,这将确保只有我想要的类的BeanDefinition。
package test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import com.xx.xx.xx.xx.xx.RetryInterceptor;
public class TestBeanFacotryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
String[] definitionNames = registry.getBeanDefinitionNames();
for (int i = 0, j = 0; i < definitionNames.length; i++) {
Class<?> clazz;
try {
clazz = Class.forName(registry.getBeanDefinition(definitionNames[i]).getBeanClassName());
if (RetryInterceptor.class == clazz && j++ > 0) {
registry.removeBeanDefinition(definitionNames[i]);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
我使用JSF和托管Beans创建了一个简单的应用程序。从托管bean,我试图实例化一个将数据存储到数据库的spring bean。然而,“@autowired”注释似乎不起作用,因为我得到了一个nullpointerexcpetion。我阅读了关于如何在Spring中使用JSF和托管bean的所有相关主题,但不能解决这个问题。也许有人可以看看下面的代码,给我一个提示? 服务Bean faces.c
主要内容:实例,运行测试结果以下代码显示了如何进行bean注入。 我们先定义一个消息bean,它有一个字符串属性来存储消息。 然后我们再定义另一个托管bean,并使用注解注入。 实例 打开 NetBean8.2,创建一个名为: InjectManagedBeans 的工程,并加入以下文件代码。 以下是文件:UserBean.java 中的代码 - 以下是是文件:index.xhtml 中的代码 - 以下是文件:Message
问题内容: 我通过定义一个托管属性将一个托管bean注入另一个托管bean遇到了一些麻烦。我正在谷歌搜索和stackoverflow 3天,但没有结果… 我正在使用Eclipse 4.2进行开发并将其部署到集成的Tomcat 7中 那么,谁能告诉我,为什么我的财产为空? pom.xml web.xml 我已经在applicationContext中设置了可扫描@Autowired注释的bean。(
我在spring项目中使用基于注释的配置。我使用@Autowired注入类,这些类用适当的注释进行注释,比如@Service、@Component、@Repository、@Controller。我这里有两个问题: 不知怎的,我是否能得到一个spring BeanFactory的实例
托管bean它是一个纯Java类,它包含一组属性和一组,方法。 以下是托管bean方法执行的常见功能: 验证组件的数据 处理组件触发的事件 执行处理以确定应用程序必须导航的下一页 它也可以作为JFS框架的模型。 JSF托管Bean示例 请看看下面一段示例代码 - 您可以通过以下方式使用此。 通过配置成XML文件。 通过使用注释。 通过XML文件配置托管Bean 在xml文件配置是比较旧方法。 在这
在执行CRUD时,我有一个RequestScope票据bean。 我有一个XHTML页面,直到现在我一直在使用它来创建新的票据;票证字段直接填充(等等) 现在我有了一个搜索表单,它列出了票证,每个票证都有一个以票证id为参数的链接。我希望支持bean从EJB/JPA(已经完成)检索票证bean并将其放入请求中。我认为有三种方法可以做到这一点: 将从JPA检索到的bean中的数据复制到注入提供的be