2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.client.impl.protocol;
27 import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
28 import com.att.nsa.cambria.client.CambriaConsumer;
30 import java.io.IOException;
31 import java.lang.reflect.Field;
32 import java.net.MalformedURLException;
33 import java.security.GeneralSecurityException;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
38 class ConsumerImpl implements Consumer {
40 private static final int DEFAULT_LIMIT = 1000;
42 private Collection<String> hosts;
45 private String groupId;
48 private String authKey;
49 private String authSecret;
51 private CambriaConsumer consumer = null;
61 public ConsumerImpl(Collection<String> urls, String topicName, String consumerName, String consumerId, Integer timeout, String apiKey, String apiSecret) throws MalformedURLException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
63 this.topic = topicName;
64 this.group = consumerName;
65 this.groupId = consumerId;
66 this.authKey = apiKey;
67 this.authSecret = apiSecret;
68 this.timeout = timeout;
69 consumer = getConsumer();
73 public List<String> fetch() throws IOException {
75 return fetch(DEFAULT_LIMIT);
78 public List<String> fetch(int limit) throws IOException {
80 List<String> out = new ArrayList<String>();
82 for(String msg : consumer.fetch(timeout,limit)){
85 } catch (IOException e) {
91 public void registerForRead() throws IOException {
93 int waitForRegisteration = 1; //return from fetch after 1ms, no need to read any messages
94 consumer.fetch(waitForRegisteration, 1);
98 * init cambria consumer
99 * @return CambriaConsumer
101 private CambriaConsumer getConsumer() throws MalformedURLException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
103 ConsumerBuilder builder = new ConsumerBuilder();
105 builder.usingHosts(hosts).onTopic(topic).knownAs(group, groupId);
106 builder.withSocketTimeout(timeout + 5000).waitAtServer(timeout);
107 builder.receivingAtMost(DEFAULT_LIMIT);
109 // Add credentials if provided
110 if (authKey != null && authSecret != null) {
112 Field apiKeyField = ConsumerBuilder.class.getDeclaredField("fApiKey");
113 apiKeyField.setAccessible(true);
114 apiKeyField.set(builder, "");
115 builder.authenticatedBy(authKey, authSecret);
118 return builder.build();
122 public void close() {