Release 1.13.5 docker artifact
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / web / DmaapConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.web;
22
23 import javax.annotation.PostConstruct;
24
25 import org.apache.activemq.ActiveMQConnectionFactory;
26 import org.apache.activemq.broker.BrokerService;
27 import org.apache.activemq.command.ActiveMQQueue;
28 import org.onap.aai.dmaap.AAIDmaapEventJMSConsumer;
29 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Qualifier;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.annotation.Bean;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.context.annotation.Profile;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.jms.connection.CachingConnectionFactory;
39 import org.springframework.jms.core.JmsTemplate;
40 import org.springframework.jms.listener.DefaultMessageListenerContainer;
41 import org.springframework.web.client.RestTemplate;
42
43 @Profile("dmaap")
44 @Configuration
45 public class DmaapConfig {
46
47     @Autowired
48     private ApplicationContext ctx;
49
50     @Autowired
51     @Qualifier("dmaapRestTemplate")
52     private RestTemplate dmaapRestTemplate;
53
54     @Autowired
55     @Qualifier("dmaapHeaders")
56     private HttpHeaders dmaapHeaders;
57
58     @Value("${jms.bind.address}")
59     private String bindAddress;
60
61     @PostConstruct
62     public void init() {
63         System.setProperty("activemq.tcp.url", bindAddress);
64     }
65
66     @Bean(destroyMethod = "stop")
67     public BrokerService brokerService() throws Exception {
68
69         BrokerService broker = new BrokerService();
70         broker.addConnector(bindAddress);
71         broker.setPersistent(false);
72         broker.setUseJmx(false);
73         broker.setSchedulerSupport(false);
74         broker.start();
75
76         return broker;
77     }
78
79     @Bean(name = "connectionFactory")
80     public ActiveMQConnectionFactory activeMQConnectionFactory() {
81         return new ActiveMQConnectionFactory(bindAddress);
82     }
83
84     @Bean
85     public CachingConnectionFactory cachingConnectionFactory() {
86         return new CachingConnectionFactory(activeMQConnectionFactory());
87     }
88
89     @Bean(name = "destinationQueue")
90     public ActiveMQQueue activeMQQueue() {
91         return new ActiveMQQueue("IN_QUEUE");
92     }
93
94     @Bean
95     public JmsTemplate jmsTemplate() {
96         JmsTemplate jmsTemplate = new JmsTemplate();
97
98         jmsTemplate.setConnectionFactory(activeMQConnectionFactory());
99         jmsTemplate.setDefaultDestination(activeMQQueue());
100
101         return jmsTemplate;
102     }
103
104     @Bean
105     public AAIDmaapEventJMSProducer jmsProducer() {
106         return new AAIDmaapEventJMSProducer();
107     }
108
109     @Bean(name = "jmsConsumer")
110     public AAIDmaapEventJMSConsumer jmsConsumer() throws Exception {
111         return new AAIDmaapEventJMSConsumer(ctx.getEnvironment(), dmaapRestTemplate, dmaapHeaders);
112     }
113
114     @Bean
115     public DefaultMessageListenerContainer defaultMessageListenerContainer() throws Exception {
116
117         DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
118
119         messageListenerContainer.setConnectionFactory(cachingConnectionFactory());
120         messageListenerContainer.setDestinationName("IN_QUEUE");
121         messageListenerContainer.setMessageListener(jmsConsumer());
122
123         return messageListenerContainer;
124     }
125 }