Search in sources :

Example 1 with Advice

use of org.aopalliance.aop.Advice in project spring-framework by spring-projects.

the class DefaultAdvisorAdapterRegistry method getInterceptors.

@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
    List<MethodInterceptor> interceptors = new ArrayList<>(3);
    Advice advice = advisor.getAdvice();
    if (advice instanceof MethodInterceptor) {
        interceptors.add((MethodInterceptor) advice);
    }
    for (AdvisorAdapter adapter : this.adapters) {
        if (adapter.supportsAdvice(advice)) {
            interceptors.add(adapter.getInterceptor(advisor));
        }
    }
    if (interceptors.isEmpty()) {
        throw new UnknownAdviceTypeException(advisor.getAdvice());
    }
    return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ArrayList(java.util.ArrayList) Advice(org.aopalliance.aop.Advice)

Example 2 with Advice

use of org.aopalliance.aop.Advice in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testSerializationAdviceNotSerializable.

@Test
public void testSerializationAdviceNotSerializable() throws Exception {
    SerializablePerson sp = new SerializablePerson();
    assertTrue(SerializationTestUtils.isSerializable(sp));
    ProxyFactory pf = new ProxyFactory(sp);
    // This isn't serializable
    Advice i = new NopInterceptor();
    pf.addAdvice(i);
    assertFalse(SerializationTestUtils.isSerializable(i));
    Object proxy = createAopProxy(pf).getProxy();
    assertFalse(SerializationTestUtils.isSerializable(proxy));
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) Advice(org.aopalliance.aop.Advice) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) CountingAfterReturningAdvice(org.springframework.tests.aop.advice.CountingAfterReturningAdvice) Test(org.junit.Test)

Example 3 with Advice

use of org.aopalliance.aop.Advice in project spring-boot by spring-projects.

the class RabbitAutoConfigurationTests method testRabbitListenerContainerFactoryBackOff.

@Test
public void testRabbitListenerContainerFactoryBackOff() {
    load(TestConfiguration5.class);
    SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context.getBean("rabbitListenerContainerFactory", SimpleRabbitListenerContainerFactory.class);
    rabbitListenerContainerFactory.setTxSize(10);
    verify(rabbitListenerContainerFactory).setTxSize(10);
    DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
    Advice[] adviceChain = (Advice[]) dfa.getPropertyValue("adviceChain");
    assertThat(adviceChain).isNull();
}
Also used : SimpleRabbitListenerContainerFactory(org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 4 with Advice

use of org.aopalliance.aop.Advice in project spring-boot by spring-projects.

the class RabbitAutoConfigurationTests method testRabbitListenerContainerFactoryWithCustomSettings.

@Test
public void testRabbitListenerContainerFactoryWithCustomSettings() {
    load(new Class<?>[] { MessageConvertersConfiguration.class, MessageRecoverersConfiguration.class }, "spring.rabbitmq.listener.retry.enabled:true", "spring.rabbitmq.listener.retry.maxAttempts:4", "spring.rabbitmq.listener.retry.initialInterval:2000", "spring.rabbitmq.listener.retry.multiplier:1.5", "spring.rabbitmq.listener.retry.maxInterval:5000", "spring.rabbitmq.listener.autoStartup:false", "spring.rabbitmq.listener.acknowledgeMode:manual", "spring.rabbitmq.listener.concurrency:5", "spring.rabbitmq.listener.maxConcurrency:10", "spring.rabbitmq.listener.prefetch:40", "spring.rabbitmq.listener.defaultRequeueRejected:false", "spring.rabbitmq.listener.idleEventInterval:5", "spring.rabbitmq.listener.transactionSize:20");
    SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context.getBean("rabbitListenerContainerFactory", SimpleRabbitListenerContainerFactory.class);
    DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
    assertThat(dfa.getPropertyValue("autoStartup")).isEqualTo(Boolean.FALSE);
    assertThat(dfa.getPropertyValue("acknowledgeMode")).isEqualTo(AcknowledgeMode.MANUAL);
    assertThat(dfa.getPropertyValue("concurrentConsumers")).isEqualTo(5);
    assertThat(dfa.getPropertyValue("maxConcurrentConsumers")).isEqualTo(10);
    assertThat(dfa.getPropertyValue("prefetchCount")).isEqualTo(40);
    assertThat(dfa.getPropertyValue("txSize")).isEqualTo(20);
    assertThat(dfa.getPropertyValue("messageConverter")).isSameAs(this.context.getBean("myMessageConverter"));
    assertThat(dfa.getPropertyValue("defaultRequeueRejected")).isEqualTo(Boolean.FALSE);
    assertThat(dfa.getPropertyValue("idleEventInterval")).isEqualTo(5L);
    Advice[] adviceChain = (Advice[]) dfa.getPropertyValue("adviceChain");
    assertThat(adviceChain).isNotNull();
    assertThat(adviceChain.length).isEqualTo(1);
    dfa = new DirectFieldAccessor(adviceChain[0]);
    MessageRecoverer messageRecoverer = this.context.getBean("myMessageRecoverer", MessageRecoverer.class);
    MethodInvocationRecoverer<?> mir = (MethodInvocationRecoverer<?>) dfa.getPropertyValue("recoverer");
    Message message = mock(Message.class);
    Exception ex = new Exception("test");
    mir.recover(new Object[] { "foo", message }, ex);
    verify(messageRecoverer).recover(message, ex);
    RetryTemplate retryTemplate = (RetryTemplate) dfa.getPropertyValue("retryOperations");
    assertThat(retryTemplate).isNotNull();
    dfa = new DirectFieldAccessor(retryTemplate);
    SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) dfa.getPropertyValue("retryPolicy");
    ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) dfa.getPropertyValue("backOffPolicy");
    assertThat(retryPolicy.getMaxAttempts()).isEqualTo(4);
    assertThat(backOffPolicy.getInitialInterval()).isEqualTo(2000);
    assertThat(backOffPolicy.getMultiplier()).isEqualTo(1.5);
    assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000);
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageRecoverer(org.springframework.amqp.rabbit.retry.MessageRecoverer) Message(org.springframework.amqp.core.Message) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) SimpleRabbitListenerContainerFactory(org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MethodInvocationRecoverer(org.springframework.retry.interceptor.MethodInvocationRecoverer) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Advice(org.aopalliance.aop.Advice) ExpectedException(org.junit.rules.ExpectedException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 5 with Advice

use of org.aopalliance.aop.Advice in project spring-boot by spring-projects.

the class MockitoAopProxyTargetInterceptor method applyTo.

@Autowired
public static void applyTo(Object source) {
    Assert.state(AopUtils.isAopProxy(source), "Source must be an AOP proxy");
    try {
        Advised advised = (Advised) source;
        for (Advisor advisor : advised.getAdvisors()) {
            if (advisor instanceof MockitoAopProxyTargetInterceptor) {
                return;
            }
        }
        Object target = AopTestUtils.getUltimateTargetObject(source);
        Advice advice = new MockitoAopProxyTargetInterceptor(source, target);
        advised.addAdvice(0, advice);
    } catch (Exception ex) {
        throw new IllegalStateException("Unable to apply Mockito AOP support", ex);
    }
}
Also used : Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Advice(org.aopalliance.aop.Advice) Autowired(org.springframework.beans.factory.annotation.Autowired)

Aggregations

Advice (org.aopalliance.aop.Advice)8 Test (org.junit.Test)4 SimpleRabbitListenerContainerFactory (org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)2 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)2 ArrayList (java.util.ArrayList)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 ExpectedException (org.junit.rules.ExpectedException)1 Message (org.springframework.amqp.core.Message)1 MessageRecoverer (org.springframework.amqp.rabbit.retry.MessageRecoverer)1 Advisor (org.springframework.aop.Advisor)1 AfterReturningAdvice (org.springframework.aop.AfterReturningAdvice)1 DynamicIntroductionAdvice (org.springframework.aop.DynamicIntroductionAdvice)1 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)1 ThrowsAdvice (org.springframework.aop.ThrowsAdvice)1 AspectJProxyFactory (org.springframework.aop.aspectj.annotation.AspectJProxyFactory)1 Advised (org.springframework.aop.framework.Advised)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1