|
1 | 1 | package com.github.aznamier.keycloak.event.provider;
|
2 | 2 |
|
| 3 | +import com.rabbitmq.client.Channel; |
| 4 | +import com.rabbitmq.client.Connection; |
| 5 | +import com.rabbitmq.client.ConnectionFactory; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.concurrent.TimeoutException; |
| 8 | +import org.jboss.logging.Logger; |
3 | 9 | import org.keycloak.Config.Scope;
|
4 | 10 | import org.keycloak.events.EventListenerProvider;
|
5 | 11 | import org.keycloak.events.EventListenerProviderFactory;
|
|
8 | 14 |
|
9 | 15 | public class RabbitMqEventListenerProviderFactory implements EventListenerProviderFactory {
|
10 | 16 |
|
11 |
| - private RabbitMqConfig cfg; |
| 17 | + private static final Logger log = Logger.getLogger(RabbitMqEventListenerProviderFactory.class); |
| 18 | + private RabbitMqConfig cfg; |
| 19 | + private ConnectionFactory connectionFactory; |
| 20 | + private Connection connection; |
| 21 | + private Channel channel; |
12 | 22 |
|
13 |
| - @Override |
14 |
| - public EventListenerProvider create(KeycloakSession session) { |
15 |
| - return new RabbitMqEventListenerProvider(cfg, session); |
16 |
| - } |
| 23 | + @Override |
| 24 | + public EventListenerProvider create(KeycloakSession session) { |
| 25 | + checkConnectionAndChannel(); |
| 26 | + return new RabbitMqEventListenerProvider(channel, session, cfg); |
| 27 | + } |
17 | 28 |
|
18 |
| - @Override |
19 |
| - public void init(Scope config) { |
20 |
| - cfg = RabbitMqConfig.createFromScope(config); |
21 |
| - } |
| 29 | + private synchronized void checkConnectionAndChannel() { |
| 30 | + try { |
| 31 | + if (connection == null || !connection.isOpen()) { |
| 32 | + this.connection = connectionFactory.newConnection(); |
| 33 | + } |
| 34 | + if (channel == null || !channel.isOpen()) { |
| 35 | + channel = connection.createChannel(); |
| 36 | + } |
| 37 | + } |
| 38 | + catch (IOException | TimeoutException e) { |
| 39 | + log.error("keycloak-to-rabbitmq ERROR on connection to rabbitmq", e); |
| 40 | + } |
| 41 | + } |
22 | 42 |
|
23 |
| - @Override |
24 |
| - public void postInit(KeycloakSessionFactory factory) { |
| 43 | + @Override |
| 44 | + public void init(Scope config) { |
| 45 | + cfg = RabbitMqConfig.createFromScope(config); |
| 46 | + this.connectionFactory = new ConnectionFactory(); |
25 | 47 |
|
26 |
| - } |
| 48 | + this.connectionFactory.setUsername(cfg.getUsername()); |
| 49 | + this.connectionFactory.setPassword(cfg.getPassword()); |
| 50 | + this.connectionFactory.setVirtualHost(cfg.getVhost()); |
| 51 | + this.connectionFactory.setHost(cfg.getHostUrl()); |
| 52 | + this.connectionFactory.setPort(cfg.getPort()); |
| 53 | + this.connectionFactory.setAutomaticRecoveryEnabled(true); |
27 | 54 |
|
28 |
| - @Override |
29 |
| - public void close() { |
| 55 | + if (cfg.getUseTls()) { |
| 56 | + try { |
| 57 | + this.connectionFactory.useSslProtocol(); |
| 58 | + } |
| 59 | + catch (Exception e) { |
| 60 | + log.error("Could not use SSL protocol", e); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
30 | 64 |
|
31 |
| - } |
| 65 | + @Override |
| 66 | + public void postInit(KeycloakSessionFactory factory) { |
32 | 67 |
|
33 |
| - @Override |
34 |
| - public String getId() { |
35 |
| - return "keycloak-to-rabbitmq"; |
36 |
| - } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void close() { |
| 72 | + try { |
| 73 | + channel.close(); |
| 74 | + connection.close(); |
| 75 | + } |
| 76 | + catch (IOException | TimeoutException e) { |
| 77 | + log.error("keycloak-to-rabbitmq ERROR on close", e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getId() { |
| 83 | + return "keycloak-to-rabbitmq"; |
| 84 | + } |
37 | 85 |
|
38 | 86 | }
|
0 commit comments