29fac33ad2c60399cc6656672dda04c4d185f4fc
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / config / 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.config;
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.JMSConsumer;
26 import org.onap.aai.dmaap.JMSProducer;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.context.annotation.Profile;
31 import org.springframework.jms.connection.CachingConnectionFactory;
32 import org.springframework.jms.core.JmsTemplate;
33 import org.springframework.jms.listener.DefaultMessageListenerContainer;
34
35 import javax.annotation.PostConstruct;
36
37 @Profile("dmaap")
38 @Configuration
39 public class DmaapConfig {
40
41     @Value("${jms.bind.address}")
42     private String bindAddress;
43
44     @PostConstruct
45     public void init(){
46         System.setProperty("activemq.tcp.url", bindAddress);
47     }
48
49     @Bean(destroyMethod = "stop")
50     public BrokerService brokerService() throws Exception {
51
52         BrokerService broker = new BrokerService();
53         broker.addConnector(bindAddress);
54         broker.setPersistent(false);
55         broker.setUseJmx(false);
56         broker.setSchedulerSupport(false);
57         broker.start();
58
59         return broker;
60     }
61
62     @Bean(name = "connectionFactory")
63     public ActiveMQConnectionFactory activeMQConnectionFactory(){
64         return new ActiveMQConnectionFactory(bindAddress);
65     }
66
67     @Bean
68     public CachingConnectionFactory cachingConnectionFactory(){
69         return new CachingConnectionFactory(activeMQConnectionFactory());
70     }
71
72     @Bean(name = "destinationQueue")
73     public ActiveMQQueue activeMQQueue(){
74         return new ActiveMQQueue("IN_QUEUE");
75     }
76
77     @Bean
78     public JmsTemplate jmsTemplate(){
79         JmsTemplate jmsTemplate = new JmsTemplate();
80
81         jmsTemplate.setConnectionFactory(activeMQConnectionFactory());
82         jmsTemplate.setDefaultDestination(activeMQQueue());
83
84         return jmsTemplate;
85     }
86
87     @Bean
88     public JMSProducer jmsProducer(){
89         return new JMSProducer();
90     }
91
92     @Bean
93     public JMSConsumer jmsConsumer() throws Exception {
94         return new JMSConsumer();
95     }
96
97     @Bean
98     public DefaultMessageListenerContainer defaultMessageListenerContainer() throws Exception {
99
100         DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
101
102         messageListenerContainer.setConnectionFactory(cachingConnectionFactory());
103         messageListenerContainer.setDestinationName("IN_QUEUE");
104         messageListenerContainer.setMessageListener(jmsConsumer());
105
106         return messageListenerContainer;
107     }
108 }