657656c4843ad3fb6bf6745d31d9c103f1a89ce5
[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 package org.onap.aai.web;
21
22 import org.apache.activemq.ActiveMQConnectionFactory;
23 import org.apache.activemq.broker.BrokerService;
24 import org.apache.activemq.command.ActiveMQQueue;
25 import org.onap.aai.dmaap.AAIDmaapEventJMSConsumer;
26 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Qualifier;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.context.annotation.Profile;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.jms.connection.CachingConnectionFactory;
36 import org.springframework.jms.core.JmsTemplate;
37 import org.springframework.jms.listener.DefaultMessageListenerContainer;
38
39 import javax.annotation.PostConstruct;
40 import org.springframework.web.client.RestTemplate;
41
42 @Profile("dmaap")
43 @Configuration
44 public class DmaapConfig {
45
46     @Autowired
47     private ApplicationContext ctx;
48
49     @Autowired
50     @Qualifier("dmaapRestTemplate")
51     private RestTemplate dmaapRestTemplate;
52
53     @Autowired
54     @Qualifier("dmaapHeaders")
55     private HttpHeaders dmaapHeaders;
56
57     @Value("${jms.bind.address}")
58     private String bindAddress;
59
60     @PostConstruct
61     public void init(){
62         System.setProperty("activemq.tcp.url", bindAddress);
63     }
64
65     @Bean(destroyMethod = "stop")
66     public BrokerService brokerService() throws Exception {
67
68         BrokerService broker = new BrokerService();
69         broker.addConnector(bindAddress);
70         broker.setPersistent(false);
71         broker.setUseJmx(false);
72         broker.setSchedulerSupport(false);
73         broker.start();
74
75         return broker;
76     }
77
78     @Bean(name = "connectionFactory")
79     public ActiveMQConnectionFactory activeMQConnectionFactory(){
80         return new ActiveMQConnectionFactory(bindAddress);
81     }
82
83     @Bean
84     public CachingConnectionFactory cachingConnectionFactory(){
85         return new CachingConnectionFactory(activeMQConnectionFactory());
86     }
87
88     @Bean(name = "destinationQueue")
89     public ActiveMQQueue activeMQQueue(){
90         return new ActiveMQQueue("IN_QUEUE");
91     }
92
93     @Bean
94     public JmsTemplate jmsTemplate(){
95         JmsTemplate jmsTemplate = new JmsTemplate();
96
97         jmsTemplate.setConnectionFactory(activeMQConnectionFactory());
98         jmsTemplate.setDefaultDestination(activeMQQueue());
99
100         return jmsTemplate;
101     }
102
103     @Bean
104     public AAIDmaapEventJMSProducer jmsProducer(){
105         return new AAIDmaapEventJMSProducer();
106     }
107
108     @Bean(name="jmsConsumer")
109     public AAIDmaapEventJMSConsumer jmsConsumer() throws Exception {
110         return new AAIDmaapEventJMSConsumer(ctx.getEnvironment(), dmaapRestTemplate, dmaapHeaders);
111     }
112
113     @Bean
114     public DefaultMessageListenerContainer defaultMessageListenerContainer() throws Exception {
115
116         DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
117
118         messageListenerContainer.setConnectionFactory(cachingConnectionFactory());
119         messageListenerContainer.setDestinationName("IN_QUEUE");
120         messageListenerContainer.setMessageListener(jmsConsumer());
121
122         return messageListenerContainer;
123     }
124 }