This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathCloudRabbitConfig.java
58 lines (51 loc) · 2.44 KB
/
CloudRabbitConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.sap.bulletinboard.ads.config;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cloud.config.java.AbstractCloudConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("cloud")
public class CloudRabbitConfig extends AbstractCloudConfig {
public static final String STATISTICS_ROUTING_KEY = "statistics.adIsShown";
/**
* Parses the local environment variable VCAP_SERVICES (containing cloud information) and provides a
* ConnectionFactory. The superclass {@link AbstractCloudConfig}, part of the Spring Cloud plugin, is used for this.
*/
@Bean
public ConnectionFactory rabbitConnectionFactory() {
CachingConnectionFactory factory = (CachingConnectionFactory) (connectionFactory().rabbitConnectionFactory());
factory.setPublisherConfirms(true);
factory.setPublisherReturns(true);
// When using publisher confirms, the cache size needs to be large enough
// otherwise channels can be closed before confirms are received.
factory.setChannelCacheSize(100);
return factory;
}
/**
* Using the ConnectionFactory, provide an AmqpAdmin implementation. This can be used, for example, to declare new
* queues.
*/
@Bean
public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
rabbitAdmin.declareQueue(new Queue(STATISTICS_ROUTING_KEY)); // creates queue, if not existing
return rabbitAdmin;
}
/**
* Using the ConnectionFactory, provide an AmqpTemplate implementation. This can be used, for example, to send
* messages.
*/
@Bean
public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true); // otherwise we get no info whether message could not be routed
return rabbitTemplate;
}
}