|
1 |
| -package com.ericsson.ei.rmq.consumer; |
| 1 | +package com.ericsson.ei.rmqhandler; |
2 | 2 |
|
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.amqp.core.Binding; |
3 | 6 | import org.springframework.amqp.core.BindingBuilder;
|
4 | 7 | import org.springframework.amqp.core.Queue;
|
5 | 8 | import org.springframework.amqp.core.TopicExchange;
|
6 | 9 | import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
7 | 10 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
8 |
| -import org.springframework.amqp.rabbit.core.RabbitAdmin; |
| 11 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 12 | +import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback; |
9 | 13 | import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
10 | 14 | import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
|
| 15 | +import org.springframework.amqp.rabbit.support.CorrelationData; |
11 | 16 | import org.springframework.beans.factory.annotation.Value;
|
12 | 17 | import org.springframework.context.annotation.Bean;
|
13 | 18 | import org.springframework.stereotype.Component;
|
14 | 19 |
|
15 | 20 | import com.ericsson.ei.handlers.EventHandler;
|
16 | 21 |
|
17 |
| - |
18 | 22 | @Component
|
19 |
| -public class RmqConsumer { |
20 |
| - |
21 |
| - @Value("${rabbitmq.queue.durable}") private Boolean queueDurable; |
22 |
| - @Value("${rabbitmq.host}") private String host; |
23 |
| - @Value("${rabbitmq.exchange.name}") private String exchangeName; |
24 |
| - @Value("${rabbitmq.port}") private Integer port; |
25 |
| - @Value("${rabbitmq.tls}") private String tlsVer; |
26 |
| - @Value("${rabbitmq.user}") private String user; |
27 |
| - @Value("${rabbitmq.password}") private String password; |
28 |
| - @Value("${rabbitmq.domainId}") private String domainId; |
29 |
| - @Value("${rabbitmq.componentName}") private String componentName; |
30 |
| - @Value("${rabbitmq.routing.key}") private String routingKey; |
31 |
| - @Value("${rabbitmq.consumerName}") private String consumerName; |
32 |
| - |
33 |
| -// SimpleMessageListenerContainer container; |
| 23 | +public class RmqHandler { |
| 24 | + |
| 25 | + @Value("${rabbitmq.queue.durable}") |
| 26 | + private Boolean queueDurable; |
| 27 | + @Value("${rabbitmq.host}") |
| 28 | + private String host; |
| 29 | + @Value("${rabbitmq.exchange.name}") |
| 30 | + private String exchangeName; |
| 31 | + @Value("${rabbitmq.port}") |
| 32 | + private Integer port; |
| 33 | + @Value("${rabbitmq.tls}") |
| 34 | + private String tlsVer; |
| 35 | + @Value("${rabbitmq.user}") |
| 36 | + private String user; |
| 37 | + @Value("${rabbitmq.password}") |
| 38 | + private String password; |
| 39 | + @Value("${rabbitmq.domainId}") |
| 40 | + private String domainId; |
| 41 | + @Value("${rabbitmq.componentName}") |
| 42 | + private String componentName; |
| 43 | + @Value("${rabbitmq.routing.key}") |
| 44 | + private String routingKey; |
| 45 | + @Value("${rabbitmq.consumerName}") |
| 46 | + private String consumerName; |
| 47 | + // SimpleMessageListenerContainer container; |
| 48 | + private RabbitTemplate rabbitTemplate; |
| 49 | + private CachingConnectionFactory factory; |
| 50 | + private SimpleMessageListenerContainer container; |
| 51 | + static Logger log = (Logger) LoggerFactory.getLogger(RmqHandler.class); |
34 | 52 |
|
35 | 53 | public Boolean getQueueDurable() {
|
36 | 54 | return queueDurable;
|
@@ -122,32 +140,76 @@ public void setConsumerName(String consumerName) {
|
122 | 140 |
|
123 | 141 | @Bean
|
124 | 142 | ConnectionFactory connectionFactory() {
|
125 |
| - CachingConnectionFactory factory = new CachingConnectionFactory(host,port); |
126 |
| -// factory.setUsername("guest"); |
127 |
| -// factory.setPassword("guest"); |
| 143 | + factory = new CachingConnectionFactory(host, port); |
| 144 | + factory.setPublisherConfirms(true); |
| 145 | + factory.setPublisherReturns(true); |
| 146 | + // factory.setUsername("guest"); |
| 147 | + // factory.setPassword("guest"); |
128 | 148 | return factory;
|
129 | 149 | }
|
130 | 150 |
|
| 151 | + @Bean |
| 152 | + Queue queue() { |
| 153 | + return new Queue(getQueueName(), true); |
| 154 | + } |
| 155 | + |
| 156 | + @Bean |
| 157 | + TopicExchange exchange() { |
| 158 | + return new TopicExchange(exchangeName); |
| 159 | + } |
| 160 | + |
| 161 | + @Bean |
| 162 | + Binding binding(Queue queue, TopicExchange exchange) { |
| 163 | + return BindingBuilder.bind(queue).to(exchange).with(routingKey); |
| 164 | + } |
| 165 | + |
131 | 166 | @Bean
|
132 | 167 | SimpleMessageListenerContainer bindToQueueForRecentEvents(ConnectionFactory factory, EventHandler eventHandler) {
|
133 | 168 | String queueName = getQueueName();
|
134 |
| - Queue queue = new Queue(queueName, queueDurable); |
135 |
| - TopicExchange topicExchange = new TopicExchange(exchangeName); |
136 |
| - RabbitAdmin rabbitAdmin = new RabbitAdmin(factory); |
137 |
| - rabbitAdmin.declareExchange(topicExchange); |
138 |
| - rabbitAdmin.declareQueue(queue); |
139 |
| - BindingBuilder.bind(queue).to(topicExchange).with(routingKey); |
140 | 169 | MessageListenerAdapter listenerAdapter = new MessageListenerAdapter(eventHandler, "eventReceived");
|
141 |
| - SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); |
| 170 | + container = new SimpleMessageListenerContainer(); |
142 | 171 | container.setConnectionFactory(factory);
|
143 | 172 | container.setQueueNames(queueName);
|
144 | 173 | container.setMessageListener(listenerAdapter);
|
145 | 174 | return container;
|
146 | 175 | }
|
147 | 176 |
|
148 |
| - |
149 | 177 | public String getQueueName() {
|
150 | 178 | String durableName = queueDurable ? "durable" : "transient";
|
151 | 179 | return domainId + "." + componentName + "." + consumerName + "." + durableName;
|
152 | 180 | }
|
| 181 | + |
| 182 | + @Bean |
| 183 | + public RabbitTemplate rabbitMqTemplate() { |
| 184 | + if (factory != null) { |
| 185 | + rabbitTemplate = new RabbitTemplate(factory); |
| 186 | + } else { |
| 187 | + rabbitTemplate = new RabbitTemplate(connectionFactory()); |
| 188 | + } |
| 189 | + rabbitTemplate.setExchange(exchangeName); |
| 190 | + rabbitTemplate.setRoutingKey(routingKey); |
| 191 | + rabbitTemplate.setQueue(getQueueName()); |
| 192 | + rabbitTemplate.setConfirmCallback(new ConfirmCallback() { |
| 193 | + @Override |
| 194 | + public void confirm(CorrelationData correlationData, boolean ack, String cause) { |
| 195 | + log.info("Received confirm with result : {}", ack); |
| 196 | + } |
| 197 | + }); |
| 198 | + return rabbitTemplate; |
| 199 | + } |
| 200 | + |
| 201 | + public void publishObjectToMessageBus(String message) { |
| 202 | + log.info("publishing message to message bus..."); |
| 203 | + rabbitTemplate.convertAndSend(message); |
| 204 | + } |
| 205 | + |
| 206 | + public void close() { |
| 207 | + try { |
| 208 | + container.destroy(); |
| 209 | + factory.destroy(); |
| 210 | + } catch (Exception e) { |
| 211 | + log.info("exception occured while closing connections"); |
| 212 | + log.info(e.getMessage(),e); |
| 213 | + } |
| 214 | + } |
153 | 215 | }
|
0 commit comments