Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.fasterxml.jackson.databind.ObjectMapper
- import org.fissore.slf4j.FluentLoggerFactory
- import org.springframework.amqp.rabbit.listener.ListenerContainerConsumerFailedEvent
- import org.springframework.beans.factory.annotation.Autowired
- import org.springframework.context.ApplicationListener
- import org.springframework.context.annotation.Bean
- import org.springframework.context.annotation.Configuration
- import java.util.concurrent.ExecutorService
- import java.util.concurrent.LinkedBlockingQueue
- import java.util.concurrent.ThreadPoolExecutor
- import java.util.concurrent.TimeUnit
- import com.google.common.util.concurrent.ThreadFactoryBuilder
- import org.springframework.amqp.rabbit.connection.ConnectionFactory
- import org.springframework.amqp.rabbit.core.RabbitTemplate
- import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
- import org.springframework.amqp.support.converter.MessageConverter
- /**
- * Configuration
- */
- @Configuration
- class GlobalConfig {
- companion object {
- private val Log = FluentLoggerFactory.getLogger(GlobalConfig::class.java)
- }
- @Autowired
- lateinit var objectMapper: ObjectMapper
- @Bean
- fun consumerFailedListener(): ApplicationListener<ListenerContainerConsumerFailedEvent> {
- return ApplicationListener {
- Log.error().withCause(it.throwable) .log("Rabbit consumer {}failure from {}: {}",
- if (it.isFatal) "fatal " else "",
- it.source,
- it.reason)
- }
- }
- // This executor is configured in the CachingConnectionFactory, passed into the RabbitMQ Client
- // when creating the connection, and its threads are used to deliver new messages to the listener container
- @Bean(destroyMethod = "shutdown")
- fun connectionExecutorService(): ExecutorService {
- val threadFactory = ThreadFactoryBuilder()
- .setNameFormat("rabbitConn-%s").setDaemon(true).build()
- return ThreadPoolExecutor(
- 1,
- 4,
- 60L,
- TimeUnit.SECONDS,
- LinkedBlockingQueue(),
- threadFactory,
- ThreadPoolExecutor.CallerRunsPolicy()
- )
- }
- @Bean
- fun proxyMessageConverter(): MessageConverter {
- return Jackson2JsonMessageConverter(this.objectMapper)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement