Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import com.fasterxml.jackson.databind.ObjectMapper
  2. import org.fissore.slf4j.FluentLoggerFactory
  3. import org.springframework.amqp.rabbit.listener.ListenerContainerConsumerFailedEvent
  4. import org.springframework.beans.factory.annotation.Autowired
  5. import org.springframework.context.ApplicationListener
  6. import org.springframework.context.annotation.Bean
  7. import org.springframework.context.annotation.Configuration
  8. import java.util.concurrent.ExecutorService
  9. import java.util.concurrent.LinkedBlockingQueue
  10. import java.util.concurrent.ThreadPoolExecutor
  11. import java.util.concurrent.TimeUnit
  12. import com.google.common.util.concurrent.ThreadFactoryBuilder
  13. import org.springframework.amqp.rabbit.connection.ConnectionFactory
  14. import org.springframework.amqp.rabbit.core.RabbitTemplate
  15. import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
  16. import org.springframework.amqp.support.converter.MessageConverter
  17.  
  18. /**
  19. * Configuration
  20. */
  21. @Configuration
  22. class GlobalConfig {
  23. companion object {
  24. private val Log = FluentLoggerFactory.getLogger(GlobalConfig::class.java)
  25. }
  26.  
  27. @Autowired
  28. lateinit var objectMapper: ObjectMapper
  29.  
  30. @Bean
  31. fun consumerFailedListener(): ApplicationListener<ListenerContainerConsumerFailedEvent> {
  32. return ApplicationListener {
  33. Log.error().withCause(it.throwable) .log("Rabbit consumer {}failure from {}: {}",
  34. if (it.isFatal) "fatal " else "",
  35. it.source,
  36. it.reason)
  37. }
  38. }
  39.  
  40. // This executor is configured in the CachingConnectionFactory, passed into the RabbitMQ Client
  41. // when creating the connection, and its threads are used to deliver new messages to the listener container
  42. @Bean(destroyMethod = "shutdown")
  43. fun connectionExecutorService(): ExecutorService {
  44. val threadFactory = ThreadFactoryBuilder()
  45. .setNameFormat("rabbitConn-%s").setDaemon(true).build()
  46. return ThreadPoolExecutor(
  47. 1,
  48. 4,
  49. 60L,
  50. TimeUnit.SECONDS,
  51. LinkedBlockingQueue(),
  52. threadFactory,
  53. ThreadPoolExecutor.CallerRunsPolicy()
  54. )
  55. }
  56.  
  57. @Bean
  58. fun proxyMessageConverter(): MessageConverter {
  59. return Jackson2JsonMessageConverter(this.objectMapper)
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement