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