Last active
December 13, 2022 19:03
-
-
Save garyrussell/8560401 to your computer and use it in GitHub Desktop.
Spring AMQP Container Retry Example - uses the default retry template (3 attempts and no recovery - the failed message is just logged at WARN level). Inject a `RetryTemplate` into the factory bean for more options.
This file contains 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
<?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:rabbit="http://www.springframework.org/schema/rabbit" | |
xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.2.xsd | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<rabbit:connection-factory id="connectionFactory" /> | |
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" | |
routing-key="foo" exchange="retry.exchange" /> | |
<rabbit:admin connection-factory="connectionFactory" /> | |
<rabbit:queue name="retry.queue" /> | |
<rabbit:direct-exchange name="retry.exchange"> | |
<rabbit:bindings> | |
<rabbit:binding queue="retry.queue" key="foo" /> | |
</rabbit:bindings> | |
</rabbit:direct-exchange> | |
<rabbit:listener-container connection-factory="connectionFactory" advice-chain="retryAdvice"> | |
<rabbit:listener ref="listener" method="handle" queues="retry.queue" /> | |
</rabbit:listener-container> | |
<bean id="listener" class="org.springframework.integration.amqp.inbound.ContainerRetryTests$Listener" /> | |
<bean id="retryAdvice" class="org.springframework.amqp.rabbit.config.StatefulRetryOperationsInterceptorFactoryBean" /> | |
</beans> |
This file contains 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
/* | |
* Copyright 2014 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package foo; | |
import static org.junit.Assert.assertTrue; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
import org.springframework.amqp.support.converter.SimpleMessageConverter; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
/** | |
* @author Gary Russell | |
*/ | |
@ContextConfiguration | |
@RunWith(SpringJUnit4ClassRunner.class) | |
public class ContainerRetryTests { | |
@Autowired | |
private RabbitTemplate template; | |
private static final CountDownLatch latch = new CountDownLatch(3); | |
@Test | |
public void test() throws Exception { | |
// Messages need an ID for stateful retry, otherwise use stateless and handle the retries on the current thread | |
((SimpleMessageConverter) template.getMessageConverter()).setCreateMessageIds(true); | |
template.convertAndSend("bar"); | |
assertTrue(latch.await(10, TimeUnit.SECONDS)); | |
} | |
public static class Listener { | |
public void handle(String message) { | |
latch.countDown(); | |
throw new RuntimeException("foo"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment