Guest User

Untitled

a guest
Mar 20th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. spring
  2. rabbitmq:
  3. host: rabbitmq.service.consul
  4. port: 5672
  5. virtualHost: /
  6. template:
  7. retry:
  8. enabled: true
  9.  
  10. @Autowired private RabbitTemplate rt; // From RabbitAutoConfiguration
  11.  
  12. @Bean
  13. public DirectExchange emailExchange() {
  14. return new DirectExchange("email");
  15. }
  16.  
  17. public void sendEmail() {
  18. this.rt.send("email", "email.send", "test payload");
  19. }
  20.  
  21. spring.rabbitmq.username=test
  22. spring.rabbitmq.password=test
  23. spring.rabbitmq.template.retry.enabled=true
  24. spring.rabbitmq.template.retry.initial-interval=1ms
  25. logging.level.org.springframework.retry=DEBUG
  26.  
  27. @SpringBootApplication
  28. public class So49155945Application {
  29.  
  30. public static void main(String[] args) {
  31. ConfigurableApplicationContext applicationContext = SpringApplication.run(So49155945Application.class, args);
  32. RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
  33.  
  34. try {
  35. rabbitTemplate.convertAndSend("foo", "foo");
  36. }
  37. catch (AmqpException e) {
  38. System.err.println("Error during sending: " + e.getCause().getCause().getMessage());
  39. }
  40. }
  41.  
  42. }
  43.  
  44. Error during sending: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
  45.  
  46. spring.rabbitmq.publisher-confirms=true
  47. spring.rabbitmq.template.mandatory=true
  48.  
  49. import org.slf4j.Logger;
  50. import org.slf4j.LoggerFactory;
  51. import org.springframework.amqp.rabbit.annotation.*;
  52. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
  55. import org.springframework.cloud.endpoint.RefreshEndpoint;
  56. import org.springframework.messaging.MessageHeaders;
  57. import org.springframework.stereotype.Component;
  58.  
  59. import static org.springframework.amqp.core.ExchangeTypes.TOPIC;
  60.  
  61. @Component
  62. public class ReuathenticationListener {
  63.  
  64. private static Logger log = LoggerFactory.getLogger(ReuathenticationListener.class);
  65.  
  66. @Autowired private RabbitProperties rabbitProperties;
  67. @Autowired private RefreshEndpoint refreshEndpoint;
  68. @Autowired private CachingConnectionFactory connectionFactory;
  69.  
  70. @RabbitListener(
  71. id = "credential_expiry_listener",
  72. bindings = @QueueBinding(value = @Queue(value="credentials.expiry", autoDelete="true", durable="false"),
  73. exchange = @Exchange(value="amq.rabbitmq.event", type=TOPIC, internal="true", durable="true"),
  74. key = "user.#")
  75. )
  76. public void expiryHandler(final MessageHeaders headers) {
  77. final String key = (String) headers.get("amqp_receivedRoutingKey");
  78. // See: https://www.rabbitmq.com/event-exchange.html
  79. if (!key.equals("user.deleted") &&
  80. !key.equals("user.authentication.failure")) {
  81. return;
  82. }
  83.  
  84. final String failedName = (String) headers.get("name");
  85. final String prevUsername = rabbitProperties.getUsername();
  86.  
  87. if (!failedName.equals(prevUsername)) {
  88. log.debug("Ignore expiry of unrelated user: " + failedName);
  89. return;
  90. }
  91.  
  92. log.info("Refreshing Rabbit credentials...");
  93. refreshEndpoint.refresh();
  94. log.info("Refreshed username: '" + prevUsername + "' => '" + rabbitProperties.getUsername() + "'");
  95.  
  96. connectionFactory.setUsername(rabbitProperties.getUsername());
  97. connectionFactory.setPassword(rabbitProperties.getPassword());
  98. connectionFactory.resetConnection();
  99.  
  100. log.info("CachingConnectionFactory reset, reconnection should now begin.");
  101. }
  102. }
Add Comment
Please, Sign In to add comment